Tuesday Mar 23, 2010

Application-specified Portable JNDI Names

The EJB 3.1 specification defines portable global, application-scoped, and module-scoped JNDI names for all session bean components. This ensures a standard syntax by which session beans can be identified. Standard names are registered automatically during deployment, and are derived from application metadata such as module name, component name, etc.

While this solves the JNDI name portability issue, it doesn't address the case where developers prefer to choose JNDI names themselves. This application-specified approach can also be solved portably using some new Java EE 6 naming functionality for environment dependencies.

Here's a simple stateless session bean exposing a Remote business interface :

   1:  @Stateless
   2:  public class FooBean implements FooRemote { ... }

Assuming this component is deployed in a stand-alone ejb-jar module called foo.jar, its resulting portable JNDI names would be :

java:global/foo/FooBean

java:app/foo/FooBean

java:module/FooBean

The developer can select an additional JNDI name that resolves to a particular client view of a session bean by using the @EJB annotation. Starting with Java EE 6, the @EJB name() attribute value can be prefixed with any one of the three portable Java EE namespaces : java:global, java:app, java:module. This has the effect of exporting the dependency into the selected scope.

Using our example, to expose the Remote interface of FooBean within the global namespace under "examples/foo/remote", the following @EJB dependency could be added to the class-level of FooBean.java.

   1:  @EJB(name="java:global/examples/foo/remote",
   2:       beanInterface=FooRemote.class)

After deployment, any code with access to the application server's naming service can acquire an EJB reference to FooBean's remote interface as follows :

   1:  InitialContext ic = new InitialContext();
   2:  FooRemote fooRef  = (FooRemote) 
   3:      ic.lookup("java:global/examples/foo/remote");

Note that the fact that the developer chooses to expose a session bean under an additional JNDI name does not impact the registration of the default portable JNDI names in any way. It merely means there is one more string that resolves to the same target session bean.

A few other things to highlight :

  • The @EJB annotation does not have to be declared on the bean whose interface is being exposed. It can appear anywhere @EJB annotations are allowed.
  • The same EJB dependency could be declared as an ejb-ref within a standard deployment descriptor like ejb-jar.xml instead of as an @EJB annotation.
  • The additional name() attribute functionality can also be used with other Java EE environment annotations such as @Resource.
About

Ken Saks is the Spec Lead for Enterprise JavaBeans (EJB) 3.1 and a Senior Staff Engineer in the Java Platform, Enterprise Edition team at SUN.

Search

Categories
Archives
« March 2010 »
SunMonTueWedThuFriSat
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
24
25
26
27
28
29
30
31
   
       
Today