Stateful session Bean 2.1 with remote interface. Follow the below steps to invoke the session bean.
Step 1:
Bean
Home interface
Remote interface
ejb-jar.xml
Application Client
First : 1
First : 2
First : 3
Second : 2
Second : 3
Second : 4
Bean
package com.bhuvan; import java.rmi.RemoteException; import javax.ejb.EJBException; import javax.ejb.SessionContext; public class HelloBean implements javax.ejb.SessionBean { public int val; public void ejbCreate(int val) throws EJBException, RemoteException { this.val=val; } public void ejbActivate() throws EJBException, RemoteException { } public void ejbPassivate() throws EJBException, RemoteException { } public void ejbRemove() throws EJBException, RemoteException { } public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException { } public int helloworld() { return ++val; } }Step 2:
Home interface
package com.bhuvan; public interface HelloHome extends javax.ejb.EJBHome{ HelloRemote create(int val) throws java.rmi.RemoteException; }Step 3:
Remote interface
package com.bhuvan; public interface HelloRemote extends javax.ejb.EJBObject { public int helloworld() throws java.rmi.RemoteException; }Step 4:
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar id="ejb-jar_ID" version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"> <enterprise-beans> <session> <ejb-name>HelloWorld</ejb-name> <home>com.bhuvan.HelloHome</home> <remote>com.bhuvan.HelloRemote</remote> <local-home>com.bhuvan.HelloLocalHome</local-home> <local>com.bhuvan.HelloLocal</local> <ejb-class>com.bhuvan.HelloBean</ejb-class> <session-type>Stateful</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar>Step 5:
Application Client
import java.util.Properties; import javax.naming.InitialContext; import javax.naming.Context; import com.bhuvan.HelloHome; import com.bhuvan.HelloRemote; import com.bhuvan.HelloLocalHome; import com.bhuvan.HelloLocal; import javax.rmi.PortableRemoteObject; public class ClientApp { public static void main(String[] args)throws Exception { try{ Properties properties = new Properties(); properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.provider.url","jnp://localhost:1099"); properties.put("java.naming.factory.url.pkgs","org.jboss.naming.client"); InitialContext context =new InitialContext(properties); HelloHome home = (HelloHome) context.lookup("HelloWorld"); HelloRemote count[]=new HelloRemote[3]; int countVal=0; for(int i=0;i<3;i++) { count[i]= home.create(countVal); countVal=count[i].helloworld(); System.out.println("First :"+countVal); Thread.sleep(5000); } for(int i=0;i<3;i++) { countVal=count[i].helloworld(); System.out.println("Second :"+countVal); Thread.sleep(5000); } for(int i=0;i<3;i++) { count[i].remove(); Thread.sleep(500); } } catch(Exception e) { e.printStackTrace(); } } }OUTPUT in client Console:
First : 1
First : 2
First : 3
Second : 2
Second : 3
Second : 4