Running EJB (EJB 2) with jboss (jboss 4) requires some setup. I am trying to explain the steps one needs to for EJB to work with jboss .
File 1 :- MyTestSession.java
File 2 :- MyTestSessionHome.java
File 3 :- MyTestSessionBean.java
File 4 :- ejb-jar.xml
File 5 :- jboss.xml
File 1 :- MyTestSession.java
package ejbs.sessionBeans; import java.rmi.RemoteException; import javax.ejb.EJBObject; public interface MyTestSession extends EJBObject{ public String sayHello() throws RemoteException; }
File 2 :- MyTestSessionHome.java
package ejbs.sessionBeans; import java.rmi.RemoteException; import java.sql.SQLException; import javax.ejb.EJBException; import javax.naming.NamingException; public interface MyTestSessionHome extends javax.ejb.EJBHome{ public MyTestSession create() throws javax.ejb.CreateException,EJBException, RemoteException, NamingException; }
File 3 :- MyTestSessionBean.java
package ejbs.sessionBeans; import java.rmi.RemoteException; import javax.ejb.EJBException; import javax.ejb.EJBHome; import javax.ejb.EJBObject; import javax.ejb.Handle; import javax.ejb.RemoveException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.naming.InitialContext; import javax.naming.NamingException; public class MyTestSessionBean implements SessionBean { @Override public void ejbActivate() throws EJBException, RemoteException { System.out.println("In ejbActivate()"); } @Override public void ejbPassivate() throws EJBException, RemoteException { System.out.println("In ejbPassivate()"); } @Override public void ejbRemove() throws EJBException, RemoteException { System.out.println("In ejbRemove()"); } @Override public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException { System.out.println("In setSessionContext"); } public String sayHello() throws RemoteException{ return "Alll"; } public void ejbCreate() throws javax.ejb.CreateException,EJBException, RemoteException, NamingException { System.out.println("In ejbCreate()"); } }Now we require enter the details of this Session Bean in two XML files . One in the ejb-jar.xml and jboss.xml .
File 4 :- ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise
JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<display-name>MyFirstSessionBean</display-name>
<description>My First Session bean description</description>
<enterprise-beans>
<session>
<display-name>MySessionBean</display-name>
<ejb-name>ejbs/sessionBeans/MyTestSession</ejb-name>
<home>ejbs.sessionBeans.MyTestSessionHome</home>
<remote>ejbs.sessionBeans.MyTestSession</remote>
<ejb-class>ejbs.sessionBeans.MyTestSessionBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
File 5 :- jboss.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBoss//EN"
"http://www.jboss.org/j2ee/dtd/jboss.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>ejbs/sessionBeans/MyTestSession</ejb-name>
<jndi-name>ejb/MyTestSession</jndi-name>
</session>
</enterprise-beans>
</jboss>
Now in any of your servlet call the following java lines.
InitialContext ctx = new InitialContext(); Object objref = ctx.lookup("ejb/MyTestSession"); MyTestSessionHome myTestSessionHome = (MyTestSessionHome)PortableRemoteObject.narrow(objref,MyTestSessionHome.class); MyTestSession myTestSession= myTestSessionHome.create(); String receivedString = myTestSession.sayHello(); System.out.println(receivedString);Syntax Hightlighter test
Comments
Post a Comment