Pages

EJB Stateless Session Bean

EJB is used to implement the business logic in server side. It is a Distributed Architecture, so from anywhere we can invoke the business logic using Remote Object.



Requirement
Application server : JBoss Server 5.0 GA Click here how to setup the Jboss Server
Java : jdk 1.5 and above
IDE : Eclipse Indigo/ Juno

Step1: In Eclipse, create a new EJB Project in workspace.
Step2: Create new interface for Home (HelloHome.java). Home is used to return a remote object when accessing from remote.

Home
package com.bhuvan;
public interface HelloHome extends javax.ejb.EJBHome{ 
 HelloRemote create(int val) throws java.rmi.RemoteException;
}

Step 3: Again create new interface for Remote (HelloRemote.java).

Interface
package com.bhuvan;
public interface HelloRemote extends javax.ejb.EJBObject { 
 public String helloworld() throws java.rmi.RemoteException; 
}

Step 4: Create a class file (HelloBean.java)for Bean. Bean is used to create, remove of object and implementation of interface.

Bean
package com.bhuvan;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
public class HelloBean implements javax.ejb.SessionBean {
 public void ejbCreate(int val) throws EJBException, RemoteException {   
 } 
 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 String helloworld()
 {
  System.out.println("Hello World"); 
 }
}

Step 5: Final part of the EJB is Deployment Descriptor. create a XML called ejb-jar.xml

ejb-jar.xml
 <ejb-jar id="ejb-jar_ID" version="2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" 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>Hello</ejb-name>
<home>com.bhuvan.HelloHome</home>
<remote>com.bhuvan.HelloRemote</remote>
<ejb-class>com.bhuvan.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

Step 6: Clean the folder(optional) and Run the server.

Client Application

Step 1: Create a new ApplicationClient in a same workspace. After created, you can see a main class in appClientModule folder. In Main Class, Put the below content :

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("Hello");
                HelloRemote homeRem=home.create();
                System.out.println(homeRem.helloworld());   
 } 
 catch(Exception e)
 {
  e.printStackTrace();
 }
}
}

Step 2: Right Click on the code and run the program.

Output:
Hello World

Under the hood:
                 When you run the Application Server(JBoss), JVM instances are initialized .i.e app server is running behind. When you execute the client code, another JVM instances are created. Now two JVM are in Process. Client object in JVM invoke the server object in another JVM using RMI-IIOP .This is how client can invoke the remote object in EJB and print the result.