Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

InitialContext, JNDI-Lookup and EJB 3.0

843830Sep 11 2007 — edited Sep 11 2007
Hello,

Writing a tiny EJB 3.0 example here;
the application has 1 session bean acting as a facade, 1 entity bean matching a table within my database.
Creating a Application-client (main-method ) with a jndi.properties file which has the following content
'java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost'
I am able to fetch my Session-bean, with the code down below, which implements an Interface which is annotated with @Remote.
this is the code:
 Context jndiContext = new javax.naming.InitialContext();
            Object ref = jndiContext.lookup("EmployeeBean/remote");
TravelAgentRemote dao = (TravelAgentRemote) ref;
The example is working, and I am able to fetch the content of my database-table.

Wanting to perform the same operations using an Web-client.
For that task I created a servlet and within the doPost-method I want to fetch the reference to my EmployeeBean.
Should I not let my bean implement a Interface that uses the[b] @Local annoatation ? due to the fact that my bean and webclient-app is floating around within the same Application Server ?

After creating a interface that is local ( using @Local ) and implementing both interfaces on my bean ( @Local and @Remote ) I am able to fetch my bean through the Local interface.
			Context context = new InitialContext(); 
			Object refa= context.lookup("TravelAgentBean/local");
TravelAgent dao = (TravelAgent) refa;
But I am still using JNDI here, could I fetch the Local through an annotation ( the way I fetch the Datasource from within my session bean with the following code
@PersistenceContext(unitName="titan") 
    private EntityManager manager;
)

If you wanted to explain the InitialContext to me, not just referring me to the API but rather to an article or a book! S , how would you explain that - am I getting trhough to the containers-environment here ?


Best regards, Ink

Adding info on the @Local interface
Message was edited by:
InkiPin
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 9 2007
Added on Sep 11 2007
0 comments
252 views