3.2.8. Remote Invocation Changes

3.2.8.1. Migrate JBoss Enterprise Application Platform 5 Deployed Applications That Make Remote Invocations to JBoss Enterprise Application Platform 6

Summary

In JBoss Enterprise Application Platform 6, there are two ways to make remote invocations to the server:

  • You can use the new JBoss specific EJB client API to do the invocation.
  • You can use JNDI to lookup a proxy for your bean and invoke on that returned proxy.
This section covers option 2: coding changes required for clients that use JNDI.

In JBoss Enterprise Application Platform 5, the EJB remote interface was bound in JNDI, by default, under the name "ejbName/local" for local interfaces, and "ejbName/remote" for the remote interfaces. The client application then looked up the bean using "ejbName/remote".
In JBoss Enterprise Application Platform 6, you use the ejb:NAMESPACE_NAME for remote access to EJBs with the following syntax: For stateless beans:
ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>
For stateful beans:
ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>?stateful
The values to be substituted in the above syntax are:
  • <app-name> - the application name of the deployed EJBs. This is typically the ear name without the .ear suffix, however, the name can be overridden in the application.xml file. If the application is not deployed as a .ear, this value is an empty string. Assume this example was not deployed as an EAR.
  • <module-name> - the module name of the deployed EJBs on the server. This is typically the jar name of the EJB deployment, without the .jar suffix, but can be overridden using the ejb-jar.xml. In this example, assume the EJBs were deployed in a jboss-as-ejb-remote-app.jar, so the module name is jboss-as-ejb-remote-app.
  • <distinct-name> - an optional distinct name for the EJB. This example doesn't use a distinct name, so it uses an empty string.
  • <bean-name> - by default, is the simple class name of the bean implementation class.
  • <fully-qualified-classname-of-the-remote-interface> - the remote view fully qualified class name.
Update the client code

Assume you have deployed the following stateless EJB to a JBoss Enterprise Application 6 server. Note that it exposes a remote view for the bean.

@Stateless
@Remote(RemoteCalculator.class)
public class CalculatorBean implements RemoteCalculator {
 
    @Override
    public int add(int a, int b) {
        return a + b;
    }
 
    @Override
    public int subtract(int a, int b) {
        return a - b;
    }
}
In JBoss Enterprise Application Platform 5, the client EJB lookup and invocation was coded something like this:
InitialContext ctx = new InitialContext();
RemoteCalculator calculator = (RemoteCalculator) ctx.lookup("CalculatorBean/remote");
int a = 204;
int b = 340;
int sum = calculator.add(a, b);
In JBoss Enterprise Application Platform 6, using the information described above, the client lookup and invocation is coded like this:
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
final String appName = "";
final String moduleName = "jboss-as-ejb-remote-app";
final String distinctName = "";
final String beanName = CalculatorBean.class.getSimpleName();
final String viewClassName = RemoteCalculator.class.getName();
final RemoteCalculator statelessRemoteCalculator =  (RemoteCalculator) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);
 
int a = 204;
int b = 340;
int sum = statelessRemoteCalculator.add(a, b);
If your client is accessing a stateful EJB, you must append “?stateful” to the end of the context lookup like this:
final RemoteCalculator statefulRemoteCalculator =  (RemoteCalculator) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName + "?stateful")

A complete working example, including both server and client code, can be found in the Quickstarts. For more information, see Review the quickstart tutorials in the Get Started Developing Applications chapter of the Development Guide for JBoss Enterprise Application Platform 6.
For more information on remote invocations using JNDI, see Invoke a Session Bean Remotely using JNDI in the Enterprise JavaBeans chapter of the Development Guide for JBoss Enterprise Application Platform 6.