Remote Session

This page explains how to gain access to a remote EJB 3 session bean from an Eclipse RCP client.
The following code has been tested with JBoss AS 4.2.2.

Obtaining a Context reference

Since this step is required for every remote operation, I obtain a context reference through a static method in a Session object:


public static Context getInitialContext() throws NamingException {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
    p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
    return new InitialContext(p);
}

The code above is specific to JBoss. Other application servers may require different properties. You will see a call to this method in the next step. In this example, I’m using localhost as my server.

Accessing the Remote Session Bean

The following method obtains a reference to the remote object:


private CompanyLocationRemoteSession getSession() {
    String resource = "oxb-server/CompanyLocationSessionEJB/remote";
    Context ctx;
    CompanyLocationRemoteSession session = null;
    try {
        ctx = Session.getInitialContext();
        Object ref = ctx.lookup(resource);
        Object remoteObj = PortableRemoteObject.narrow(ref, CompanyLocationRemoteSession.class);
        session = (CompanyLocationRemoteSession) remoteObj;
    }
    catch (Exception e) {
        MessageDialog.openError(null, "Communication Failure", "Unable to connect to resource: " + resource);
        e.printStackTrace();
    }
    return session;
}

In this example, CompanyLocationRemoteSession is the EJB 3.0 Remote interface — a plain old java interface with a @Remote annotation that was deployed on the server.

The ‘resource’ String corresponds to the Session bean JNDI reference in the server. In this case the Session class has the following annotations:


@Stateless(name = "CompanyLocationSessionEJB")
@SecurityDomain("oxb-server")
public class CompanyLocationSessionBean implements CompanyLocationRemoteSession {
    @PersistenceContext(unitName = "oxb-company")
    private EntityManager manager;

So, in JBoss the JNDI reference name is composed of: [Security Domain]/[EJB Session name from Stateless/Stateful annotation]/[remote or local, depending on the interface annotation].

The default security domain is the name of the EAR file used for deployment.

Calling a Method on the Remote Session Bean

The following method calls the getSession method described above and then calls a method defined in the session bean remote interface:


public CompanyLocation findLocation(int locationId) {
    CompanyLocation result = null;
    CompanyLocationRemoteSession session = this.getSession();
    try {
        result = session.findCompanyLocation(locationId);
    }
    catch (Exception e) {
        MessageDialog.openError(null, "Failure", "Unable to get company location data.");
        e.printStackTrace();
    }
    return result;
}
 
 
 

 

Summary

So the sequence of events is as follows:

  1. A service object in the client obtains a context by calling Session.getInitialContext().
  2. The service object then obtains a reference to the remote session bean.
  3. Once the remote session reference is acquired, the service object can call a method in the remote session bean.

In this case, the context is obtained through the Session object as a matter of convenience. This Session object is a singleton object used to track user and user permissions. It seemed to make practical sense to include the context in the Session object rather than repeating the code in each service object. A super class for the service classes, could also be a good place for this method.

You must be logged in to post a comment.