Need to call deployed EJB on JBOSS from a standalone java class
smithakutty Oct 25, 2012 3:26 AMIntention of my exercise is:-
To call EJB deployed as an ear on JBOSS from a remote client.
1] EJB application on JBOSS has the following structures:-
a)It has Meta-INF folder containign application.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN' 'http://java.sun.com/dtd/application_1_3.dtd'>
<application>
<display-name>MyFirstBean</display-name>
<description>Application description</description>
<module>
<ejb>ejb.jar</ejb>
</module>
</application>
b) it has ejb.jar
which has following files and meta-inf which has ejb.jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<description>A simple session bean</description>
<display-name>Simple Message EJB</display-name>
<enterprise-beans>
<session>
<ejb-name>SimpleMessenger</ejb-name>
<home>FirstEJBHome</home>
<remote>FirstEJBObject</remote>
<ejb-class>FirstEJBBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
And it has FirstEJBBean.class
FirstEJBHome.class
FirstEJBObject.class
It has successfully deployed my ejb on jboss.
2] To call this EJB from remote client.
I created a class FirstEJBClient with the following code:-
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EJBHome;
import javax.ejb.EJBObject;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public class FirstEJBClient {
public static void main(String args[])
{
try {
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
environment.put(Context.PROVIDER_URL, "jnp://172.16.84.66:1099"); // remote machine IP
InitialContext context = new InitialContext(environment);
Object obj = context.lookup("FirstEJBHome"); //ejb-name
System.out.println("-->> lookup object successfully");
FirstEJBHome ejbHome = (FirstEJBHome) PortableRemoteObject.narrow(obj,EJBHome.class);
try {
FirstEJBObject firstEJBObject =(FirstEJBObject) ejbHome.create();
firstEJBObject.firstHello();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CreateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Getting this exception while running the class:-
log4j:WARN Please initialize the log4j system properly.
javax.naming.NameNotFoundException: FirstEJBHome 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:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:726)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
at javax.naming.InitialContext.lookup(Unknown Source)
at FirstEJBClient.main(FirstEJBClient.java:28)
-
FirstBean.ear 2.1 KB