BlazeDS Developer Guide

Accessing EJBs and other objects in JNDI

Access Enterprise JavaBeans (EJBs) and other objects stored in the Java Naming and Directory Interface (JNDI) by calling methods on a destination that is a service facade class that looks up an object in JNDI and calls its methods.

You can use stateless or stateful objects to call the methods of Enterprise JavaBeans and other objects that use JNDI. For an EJB, you can call a service facade class that returns the EJB object from JNDI and calls a method on the EJB.

In your Java class, you use the standard Java coding pattern, in which you create an initial context and perform a JNDI lookup. For an EJB, you also use the standard coding pattern in which your class contains methods that call the EJB home object's create() method and the resulting business methods of the EJB.

The following example uses a method called getHelloData() on a facade class destination:

<mx:RemoteObject id="Hello" destination="roDest">
    <mx:method name="getHelloData"/>
</mx:RemoteObject>

On the Java side, the getHelloData() method could easily encapsulate everything necessary to call a business method on an EJB. The Java method in the following example performs the following actions:

  • Creates new initial context for calling the EJB
  • Performs a JNDI lookup that gets an EJB home object
  • Calls the EJB home object's create() method
  • Calls the sayHello() method of the EJB
public void getHelloData() {
    try 
    {
        InitialContext ctx = new InitialContext();
        Object obj = ctx.lookup("/Hello");
        HelloHome ejbHome = (HelloHome)
        PortableRemoteObject.narrow(obj, HelloHome.class);
        HelloObject ejbObject = ejbHome.create();
        String message = ejbObject.sayHello();
    }
    catch (Exception e);
}


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/blazeds/1/blazeds_devguide/rpc_remoteobject_8.html