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

后端 未结 3 1324
Happy的楠姐
Happy的楠姐 2021-01-01 04:29

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

3条回答
  •  星月不相逢
    2021-01-01 04:59

    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:

    @Remote(FahrradRemote.class)
    @Stateless
    public class FahrradService implements FahrradRemote, Serializable {
        .... // your code 
    }
    

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

    In your train app:

    @EJB(name="FahrradService") 
    private FahrradRemote fahrradService;
    

    (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:

    
      
        BahnPM
        
          FahrradService
          java:global/MyRemoteBeanModule/MyRemoteBean
        
      
    
    

    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.

提交回复
热议问题