Accessing session beans using JNDI lookup

Although accessing EJB using dependency injection is the easiest way, it works only if the container manages the class that accesses the EJB. If you want to access EJB from a POJO that is not a managed bean, then dependency injection will not work. Another scenario where dependency injection does not work is when EJB is deployed in a separate JVM (could be on a remote server). In such cases, you will have to access the EJB using JNDI lookup (visit https://docs.oracle.com/javase/tutorial/jndi/ for more information on JNDI).

JEE applications could be packaged in Enterprise Application aRchive (EAR), which contains a .jar file for EJBs and a .war file for web applications (and a lib folder containing libraries required for both). If, for example, the name of the EAR file is CourseManagement.ear and the name of the EJB JAR in it is CourseManagementEJBs.jar, then the name of the application is CourseManagement (name of the EAR file) and the module name is CourseManagementEJBs. The EJB container uses these names to create JNDI URL for looking up EJBs. A global JNDI URL for EJB is created as follows:

"java:global/<application_name>/<module_name>/<bean_name>![<bean_interface>]" 

Let's have a look at the different parameters used in the preceding code snippets:

  • java:global: This indicates that it is a global JNDI URL.
  • <application_name>: This is typically the name of the EAR file.
  • <module_name>: This is the name of the EJB JAR.
  • <bean_name>: This is the name of the EJB bean class.
  • <bean_interface>: This is optional if EJB has a no-interface view, or if EJB implements only one business interface. Otherwise it is a fully qualified name of the business interface.

EJB containers are required to publish two more variations of JNDI URLs for each EJB. These are not global URLs, which means that they can't be used to access EJBs from clients that are not in the same JEE application (in the same EAR):

  • java:app/[<module_name>]/<bean_name>![<bean_interface>]
  • java:module/<bean_name>![<bean_interface>]

The first URL can be used if the EJB client is in the same application, and the second URL can be used if the client is in the same module (the same .jar file as the EJB).

Before you look up any URL in a JNDI server, you need to create InitialContext, which includes, among other things, information such as the hostname of the JNDI server and the port on which it is running. If you create InitialContext in the same server, then there is no need to specify these attributes:

InitialContext initCtx  = new InitialContext(); 
Object obj = initCtx.lookup("jndi_url"); 

We can use the following JNDI URLs to access a no-interface (LocalBean) Student EJB (assuming that the name of the EAR file is CourseManagement and the name of the .jar file for EJBs is CourseManagementEJBs):

URL

When to use

java:global/CourseManagement/ CourseManagementEJBs/Student

The client can be anywhere in the EAR file, because we use the global URL. Note that we haven't specified the interface name because we are assuming that the student bean provides a no-interface view in this example.

java:app/CourseManagementEJBs/Student

The client can be anywhere in the EAR. We skipped application name because the client is expected to be in the same application, because the namespace of the URL is java:app.

java:module/Student

The client must be in the same .jar file as EJB.

 

We can use the following JNDI URLs for accessing Student EJB that implemented a local interface called StudentLocal:

URL

When to use

java:global/CourseManagement/ CourseManagementEJBs/Student!packt.jee.book.ch6.StudentLocal

The client can be anywhere in the EAR file, because we use a global URL.

java:global/CourseManagement/ CourseManagementEJBs/Student

The client can be anywhere in the EAR. We skipped the interface name because the bean implements only one business interface. Note that the object returned from this call will be of StudentLocal type, and not Student type.

java:app/CourseManagementEJBs/Student

Or

java:app/CourseManagementEJBs/Student!packt.jee.book.ch6.StudentLocal

The client can be anywhere in the EAR. We skipped the application name because the JNDI namespace is java:app.

java:module/Student

Or

java:module/Student!packt.jee.book.ch6.StudentLocal

The client must be in the same EAR as the EJB.

Here is an example of how we can call the student bean with a local business interface from one of the objects (that is not managed by the web container) in our web application:

InitialContext ctx = new InitialContext(); 
StudentLocal student = (StudentLocal) ctx.loopup 
("java:app/CourseManagementEJBs/Student"); return student.getCourses(id) ; //get courses from Student EJB
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.217.187