Accessing EJB3 without a home interface
nbhatia Jul 13, 2008 3:11 AMAccording to the EJB 3.0 Spec:
The requirement for Home interfaces has been eliminated. Session beans are no longer required to have home interfaces. A client may acquire a reference to a session bean through one of the mechanisms described in Chapter 8.
I am trying to do exactly that, i.e. to access an EJB 3 from a remote client without a home interface. However I am getting a NameNotFoundException. Can someone help me understand how to do this?
Here's my session bean interface:
public interface HelloWorld {
public String getHelloMessage();
}
Here's the implementation:
@Stateless @Remote
public class HelloWorldBean implements HelloWorld {
public String getHelloMessage() {
return "Hello World";
}
}
Here's the call from the client:
public class HelloWorldEjb3Client {
public static void main(String[] args) {
try {
// Look up the HelloWorld home interface
Object obj = getContext().lookup("HelloWorld");
HelloWorld helloWorld =
(HelloWorld)javax.rmi.PortableRemoteObject.narrow(obj, HelloWorld.class );
// Start using the HelloWorld object
System.out.println(helloWorld.getHelloMessage());
}
catch (Exception e) {
e.printStackTrace();
}
}
public static final Context getContext() throws javax.naming.NamingException {
System.out.println("Connecting to JBoss");
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
return new InitialContext(env);
}
}
The exception is:
javax.naming.NameNotFoundException: HelloWorld not bound
Here's my ejb-jar.xml. It is essentially empty. I created it just because the Maven EJB Plugin was complaining that it could not find a ejb-jar.xml. Since I am using annotations, I don't believe that I should require this file.
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0"> </ejb-jar>
Looking at the JNDI tree, I believe that the HelloWorld bean has been deployed:
Global JNDI Namespace +- helloworldejb3-app-1.0 (class: org.jnp.interfaces.NamingContext) | +- HelloWorldBean (class: org.jnp.interfaces.NamingContext) | | +- remote (proxy: $Proxy64 implements interface samples.helloworldejb3.HelloWorld,interface org.jboss.ejb3.JBossProxy)
So what is the right way to access this bean from a remote client?
Thanks.
Naresh