Skip to content
Advertisement

How can I inject an EJB from another application on the same GlassFish Server?

I have two applications running on my local glassfish server. One to rent bicylces and one to buy train tickets. I now wanted to call a remote ejb from the train application to allow the rental of bicycles for the chosen time but I’m not really sure how to do it. I already tried a few different approaches. Both applications are in different packages and the train application has to know the bicycle remote interface but I don’t know how to accomplish that.

The remote interface (bicyle app):

JavaScript

Bicycle EJB I want to call:

JavaScript

Beginning of the train app:

JavaScript

Error I get is a class not found exception, which i can understand because it’s a different application and my train app doesn’t know “FahrradRemote” but how can I inject that EJB?

Advertisement

Answer

In your bicycle app you have to:

  • remove the @Remote annotation from your interface FahrradRemote
  • add the @Remote annotation to your FahrradService EJB

You can follow this snippet:

JavaScript

(if your are using EJB 3.X, there is no need for an EJB to explicitly implement the SessionBean interface)

In your train app:

JavaScript

(use name attribute instead of mappedName; and you cannot have static properties in a stateless EJB)

Finally you have to tell the container where to lookup for the EJB implementation: create the descriptor glassfish-ejb-jar.xml and, inside glassfish-ejb-jar tags, put this:

JavaScript

The portable JNDI name for your remote EJB (what I have called java:global/MyRemoteBeanModule/MyRemoteBean) is available in GlassFish logs when you deploy the bicycle application.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement