Creating a Stateful session Bean
By Ramakrishna on Mar 25, 2009 in Example of stateful session bean
To send two values to a stateful session bean and getting their sum and difference.
The program makes two conversations(calls) with the session bean. In one call it gets the sum and in the second call it gets the difference. Between the calls state is to be maintained.
1 Step) Remote interface :
import javax.ejb.*;
import java.rmi.*;
//No change from Stateless
public interface Calc extends EJBObject
{
public int add(int a,int b) throws RemoteException;
public int sub(int a, int b) throws RemoteException;
}
2 Step: Home interface;
import javax.ejb.*;
import javax.rmi.*;
public interface CalcHome extends EJBHome
{
public Calc create() throws CreateException, RemoteException;
//Observe the change in the implementation of business methods
}
3 Step: Bean class file;
import javax.ejb.*;
import javax.rmi.*;
public class CalcBean implements SessionBean
{
//change here, creating some instance variables to be used in between method calls
SessionContext ctx;
int a,b;
public int add(int a ,int b)
{
//initialize local ot instance variables
this.a=a;
this.b=b;
return a+b;
}
//for subtraction, instance variables assigned in the previous call are used
public int sub()
{
return a-b;
}
//override all the 4 SessionBean methods
public void ejbCreate(){ }
public void ejbActivate(){ }
public void ejbPassivate(){ }
public void setSessionContext(SessionContext ctx)
{
this.ctx = ctx;
}
}
4 Step: Write XML files needed to create a manifest file
The template of XML files with default setting are available in :
C:\ weblogic\examples\ejb\basic\statefulsession\build\meta-Inf
Copy the Meta-Inf directory to your working directory. That is this Meta-Inf directory becomes subdirectory to your working directory.
1. ejb-jar.xml:
//on this name bean will be deployed
<ejb-name> Calculation </ejb-name>
//write your home interface name
<home> CalcHome </home>
//write your remote interface name
<remote> Calc </remote>
//write your bean class name
//earlier it is Stateless
<session -type> Stateful </session-type>
comedown and write again
<ejb-name> Calculation </ejb-name>
2. weblogic-ejb-jar.xml :
<ejb-name> Calculation </ejb-name>
comedown and write again
//this is reference name to be mentioned in client
//program and with this reference name bean and client will communicate.
<jndi-name> XXX </jndi-name>
5 Step : Write JAR file :
jar cvf CalcJar.jar *.class META-INF\.xml
6 Step: Compile JAR file and obtain compiled JAR file :
ejbc CalcJar.jar CalcJar1.jar
7 Step: Copy the compiled JAR file to myserver directory:
copy CalcJar1.jar c:\weblogic \myserver
8 Step: Deploy the bean: (run from start menu)
start a weblogic 5.1.0 a Utilities a Edit Server Properties
Use Search menu option to locate Weblogic EJB Demo Properties
Write the following statement :
c:\weblogic \myserver \CalcJar1.jar \
9 Step Run weblogic server (run from start menu )
start weblogic 5.1.0 a weblogic Server
Observer the server displays: 1 deployed. 0 failed to deploy
What we have done so far is, we have created the bean and deployed in weblogic server. Now
we can call and use it.
10 Step: to write the client program, compile and run it to call the bean:
//Observe the change in the implementation of business methods
import javax.ejb.*;
import javax.rmi.*;
import javax.naming.*;
import java.util.*;
class CalcClient
{
public static void main(String args[] ) throws Exception
{
Properties p=new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, ” weblogic.jndi.T#InitialContextFactory”);
p.put(Context.PROVIDER_RUL, “t3://local:7001″);
//InitialContext() method throws javax.naming.NamingException
Context ctx = new InitialContext(p);
CalcHome chome = (calcHome) ctx.lookup(“SNRao”);
//create() method throws RemoteException and javax.ejb.CreateException
Calc rref = chome.create();
//observe change here
int sum = rref.add(20,5);
int diff = rref.sub();
System.out.println(“sum :”+sum);
System.out.println(“Diff : “+diff);
}
}

1 Comment(s)
By hockmanli on Apr 1, 2009 | Reply
Good posting! more professional web templates at itemplatez.com… its a
easy download.