Question

I am trying out a simple EJB 2.1 HelloWorld using Jboss AS 6. I created an EJB jar file with the following structure:

HelloWorldEjb
    |-ex1
        |-ejb21
            |-ejb.jar.xml
            |-Hello.java
            |-HelloBean.java
            |-HelloHome.java
            |-HelloLocal.java
            |-HelloLocalHome.java

I also created a client standalone java class to test the above ejb. I deployed the jar file using the Jboss 6 admin console under Applications > EJB2 jars and clicked on "Add new resource". When i add the jar file it gives me this message so i assume it was deployed correctly.

Resource HelloWorldEjb.jar created successfully! 

Now when i run the client i get this error:

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
javax.naming.NameNotFoundException: HelloHome not bound
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
    at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
    at org.jnp.server.NamingServer.lookup(NamingServer.java:443)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
    at sun.rmi.transport.Transport$1.run(Transport.java:159)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
    at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:728)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at client.HelloClient.main(HelloClient.java:58)
Exception in thread "main" java.lang.NullPointerException
    at client.HelloClient.main(HelloClient.java:72)

Initially i thought that i had to configure the JNDI resource for which the client will use to connect to find the EJB on the application server. Is this not necessary? The example i am using is using the EJB name in the lookup statement. Here is the section of the client code for making the connection. :

    Properties env = new Properties();

env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory" );
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");

    //env = new InitialContext(env);

InitialContext ctx = null;

try {
    ctx = new InitialContext(env);
} catch (NamingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}       

Object obj = null;

try {
    obj = ctx.lookup("HelloHome");
} catch (NamingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

HelloHome home = (HelloHome) javax.rmi.PortableRemoteObject.narrow(obj, HelloHome.class);

And here are the contents of the ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instalce"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
    version="2.1">

    <enterprise-beans>
        <session>
            <ejb-name>HelloWorldEJB</ejb-name>
            <home>ex1.ejb21.HelloHome</home>
            <remote>ex1.ejb21.Hello</remote>
            <local-home>ex1.ejb21.HelloLocalHome</local-home>
            <local>ex1.ejb21.HelloLocal</local>
            <ejb-class>ex1.ejb21.HelloBean</ejb-class>
            <session-type>Stateless</session-type>
            <transaction-type>Container</transaction-type>
        </session>  
    </enterprise-beans>

</ejb-jar>

One thing i found interesting is that when i compiled the client class, it refused to compile unless it had a reference to the HelloHome class. To resolve this i had to add the ejb jar file to the classpath of the client class. Is this how it is supposed to work? i.e. the ejb jar file needs to be on the classpath of the client?

In terms of the jndi name used for the lookup, do i need to create a new name? In the above example, it looks the EJB name is used as a lookup.

Edit

I am currently going throught a book about EJB 3.0. Part of the introduction to EBJ 3.0 is to introduce me to the 2.0 variant hence why i am trying out this example. I think it does make sense to at least get a small understanding of how they work even if i am not planning on using EJB2.

I had a look at the console for the list of deployed applications and i cant see my application on the EJB2.0 menu item. This i think means that the jar file has not deployed correctly. The Jboss logs do show that it was deployed (see 4th line below).

16:36:50,982 INFO  [org.rhq.core.pc.PluginContainer] Plugin Container initialized.
16:36:50,987 INFO  [org.jboss.on.embedded.manager.pc.PluginContainerResourceManager] Discovering Resources...
16:36:50,988 INFO  [org.rhq.core.pc.inventory.AutoDiscoveryExecutor] Executing server discovery scan...
16:36:53,920 INFO  [org.rhq.core.pc.inventory.InventoryManager] Detected new Server [Resource[id=-3, type=JBossAS Server, key=C:\bin\appservers\jboss-6.1.0.Final\server\default, name=JBoss AS 6 (default), parent=<null>, version=6.1.0.Final]] - adding to local inventory...
16:36:54,032 INFO  [org.rhq.core.pc.inventory.AutoDiscoveryExecutor] Found 0 servers.
16:36:54,032 INFO  [org.rhq.core.pc.inventory.RuntimeDiscoveryExecutor] Running runtime discovery scan rooted at [platform]
16:36:54,093 INFO  [org.rhq.core.pc.inventory.InventoryManager] Detected new Server [Resource[id=-4, type=JBoss AS JVM, key=JVM, name=JVM, parent=<null>, version=1.6.0_07]] - adding to local inventory...
16:36:54,097 INFO  [org.rhq.plugins.jmx.JMXServerComponent] Starting connection to JMX Server JVM
16:36:54,695 INFO  [org.rhq.core.pc.inventory.RuntimeDiscoveryExecutor] Scanned [0] servers and found [0] total descendant Resources.

Is there anything that looks wrong in the structure i described above?

Edit

Ok i managed to resolve it with the help from comments from richj. It looks like i had the ejb-jar.xml file in the wrong place. It was supposed to be in the META-INF folder. I copied it from ext1/ejb21 to /META-INF and jboss picked up the jar file.

17:21:44,376 INFO  [org.rhq.core.pc.inventory.RuntimeDiscoveryExecutor] Scanned [0] servers and found [0] total descendant Resources.
17:21:45,301 INFO  [org.jboss.ejb.plugins.local.BaseLocalProxyFactory] Unbind EJB LocalHome 'HelloWorldEJB' from jndi 'local/HelloWorldEJB@21777607'
17:21:45,349 INFO  [org.jboss.proxy.ejb.ProxyFactory] Unbind EJB Home 'HelloWorldEJB' from jndi 'HelloWorldEJB'
17:21:45,358 INFO  [org.jboss.ejb.EjbModule] Undeployed HelloWorldEJB
17:21:45,437 INFO  [org.jboss.ejb.deployers.EjbDeployer] installing bean: ejb/#HelloWorldEJB,uid10624357
17:21:45,438 INFO  [org.jboss.ejb.deployers.EjbDeployer]   with dependencies:
17:21:45,439 INFO  [org.jboss.ejb.deployers.EjbDeployer]   and supplies:
17:21:45,440 INFO  [org.jboss.ejb.deployers.EjbDeployer]        jndi:HelloWorldEJB/ex1.ejb21.Hello
17:21:45,441 INFO  [org.jboss.ejb.deployers.EjbDeployer]        jndi:local/HelloWorldEJB@28622485
17:21:45,442 INFO  [org.jboss.ejb.deployers.EjbDeployer]        jndi:HelloWorldEJB/ex1.ejb21.HelloLocal
17:21:45,443 INFO  [org.jboss.ejb.deployers.EjbDeployer]        jndi:HelloWorldEJB
17:21:45,466 INFO  [org.jboss.ejb.EjbModule] Deploying HelloWorldEJB
17:21:45,495 INFO  [org.jboss.ejb.plugins.local.BaseLocalProxyFactory] Bound EJB LocalHome 'HelloWorldEJB' to jndi 'local/HelloWorldEJB@28622485'
17:21:45,503 INFO  [org.jboss.proxy.ejb.ProxyFactory] Bound EJB Home 'HelloWorldEJB' to jndi 'HelloWorldEJB'

I also made a small change to the xml file as there was a typo in the namespaces used.

I used HelloWorldEJB as the jndi name for the client and it finally worked.

Thanks.

Was it helpful?

Solution

I think your exception means either:

  • the client code is using the wrong name to look up the EJB, or ...
  • the EJB isn't registered in JNDI

Does this work?

obj = ctx.lookup("HelloHomeEJB");

It is possible to define the JNDI name for the remote home using the <jndi-name> tag (and the JNDI name for the local home using the <local-jndi-name> tag.)

Is this how it is supposed to work? i.e. the ejb jar file needs to be on the classpath of the client?

The client needs access to the interface classes. The easiest way is to use the ejb jar file, but the cleanest way is to create a client version of the ejb jar file that doesn't contain any of the classes that relate only to the implementation of the EJB.

Edit - jar file not deployed

When the EJB is deployed something like this should be written to the logfile:

18:49:33,979 INFO  [EjbDeployer] installing bean: ejb/helloworld-ejb.jar#HelloWorld,uid181111540
18:49:33,979 INFO  [EjbDeployer]   with dependencies:
18:49:33,980 INFO  [EjbDeployer]   and supplies:
18:49:33,980 INFO  [EjbDeployer]        jndi:HelloWorldLocal
18:49:33,980 INFO  [EjbDeployer]        jndi:HelloWorld

The usual way to deploy an EJB jar file is to drop it into the deploy directory of the server that you are using (be careful, a standard JBoss installation usually provides more than one server configuration.) On my system the deploy directory is:

/usr/local/jboss/server/default/deploy

Sometimes multiple jar files are packaged in an ear file. The deployment method is the same but the jar file should be listed in the META-INF/application.xml deployment descriptor of the ear file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top