I'm using WildFly 8.1 application server and need to obtain EJB which is located on remote GlassFish server. What kind of options do I have? What I was thinking about is to manually create InitialContext, provide necessary properties like GlassFish addres etc. Is there any better solution? Can I obtain this EJB through annotation like @Resource?
2 Answers
I think this should answer your question:
Client access to an enterprise bean that implements a remote business interface is accomplished through either dependency injection or JNDI lookup.
To obtain a reference to the remote business interface of an enterprise bean through dependency injection, use the javax.ejb.EJB annotation and specify the enterprise bean’s remote business interface name:
@EJB Example example; To obtain a reference to a remote business interface of an enterprise bean through JNDI lookup, use the javax.naming.InitialContext interface’s lookup method:
ExampleRemote example = (ExampleRemote) InitialContext.lookup("java:global/myApp/ExampleRemote");

- 1,923
- 17
- 34
In addition to the first answer, it's important to note that the remote EJB must implement an @Remote
interface:
@Remote
public interface RemoteEJB
Given the above implementation on the remote EJB, you could just have the following in your remote client:
@EJB
RemoteEJB remoteEJB;
Can I obtain this EJB through annotation like @Resource?
I don't believe you can. @Resource
is best used for ancillary artifacts like javax.sql.DataSource
s, JMS message queues and JavaMail sessions.
Tip: Using the InitialContext
is more expensive in JNDI lookups. According to this answer by a member of the Java EE Expert Group, using the SessionContext
gives better performance in looking up context resources.