这是根据精通ejb一书中的count stateful session bean所做,但是现在有一个问题就是这个sessionbean好像再执行过程中,没有被挂起 ,这样也就更加不存在什么激活了,ejbpassivate 和ejbactivate两个方法就没有被调用到,通过jboss控制传出来的信息,根本就不存在这两个方法的调用
大致代码如下所示:
本地接口countjava
package examples;
import javax.ejb.*;
import java.rmi.*;
public interface Count extends EJBObject
{
public int count() throws RemoteException;
}
home接口
package examples;
import javax.ejb.*;
import java.rmi.*;
public interface CountHome extends EJBHome
{
public Count create(int value) throws RemoteException, CreateException;
}
beanfangfa
package examples;
import javax.ejb.*;
import java.rmi.*;
public class CountBean implements SessionBean
{
public int val;
public int count()
{
System.out.println("count()....");
return ++val;
}
public void ejbCreate(int val) throws CreateException
{
this.val=val;
System.out.println("ejbCreate()");
}
public void ejbRemove()
{
System.out.println("ejbRemove()");
}
public void ejbActivate()
{
System.out.println("ejbActivate()...");
}
public void ejbPassivate()
{
System.out.println("ejbPassivate()");
}
public void setSessionContext(SessionContext ctx)
{
}
}
ejb jar
<?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 version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:msi="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">-->
<ejb-jar>
<description>StatefulEJB</description>
<display-name>StatefulEJB</display-name>
<enterprise-beans>
<session>
<ejb-name>STATEFULEJB</ejb-name>
<home>examples.CountHome</home>
<remote>examples.Count</remote>
<ejb-class>examples.CountBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
client
import examples.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
/*
this class used for test Statefulejb STATEFULEJB
*/
public class CountClient
{
public static void main(String[] args) throws Exception
{
Properties props=new Properties();
props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url","jnp://192.168.52.136:1099");
Context ctx=new InitialContext(props);
CountHome home=(CountHome)javax.rmi.PortableRemoteObject.narrow(ctx.lookup("STATEFULEJB"),CountHome.class);
Count[] count=new Count[3];
int countval=1;
System.out.println("Instantiating bean.......");
for(int i=0;i<count.length;i++)
{
count[i]=home.create(countval);
countval=count[i].count();
System.out.println(countval);
Thread.sleep(5000);
}
System.out.println("call count on beans.....");
for(int i=0;i<3;i++)
{
countval=count[i].count();
System.out.println(countval);
Thread.sleep(5000);
}
// remove these beans
for(int i=0;i<3;i++)
{
count[i].remove();
}
}
}