有状态和无状态EJB的比较
有状态和无状态的会话bean都在客户端产生不同的代理实例
不同的是在服务器端,有状态的每次lookup都是新的独立的bean,而无状态的是单例bean。
public class StatelessEjbClient { /** * @param args * @throws NamingException */ public static void main(String[] args) throws NamingException { Hashtable env=new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory"); env.put(Context.PROVIDER_URL,"localhost"); env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces"); InitialContext cxt=new InitialContext(env); //第一次会话 StatelessEjb se1=(StatelessEjb)cxt.lookup("StatelessEjbBean/remote"); System.out.println("刚开始"+se1.getCount()); se1.compute(1); System.out.println(se1.getCount()); se1.compute(1); System.out.println(se1.getCount()); se1.compute(1); System.out.println(se1.getCount()); se1.compute(1); System.out.println(se1.getCount()); //第二次会话 StatelessEjb se2=(StatelessEjb)cxt.lookup("StatelessEjbBean/remote"); System.out.println("刚开始"+se2.getCount()); se2.compute(1); System.out.println(se2.getCount()); se2.compute(1); System.out.println(se2.getCount()); se2.compute(1); System.out.println(se2.getCount()); se2.compute(1); System.out.println(se2.getCount()); se2.compute(1); System.out.println(se2.getCount()); System.out.println("ejb1==ejb2"+(se1==se2)); } }
第一次运行结果
刚开始0
1
2
3
4
刚开始4
5
6
7
8
9
ejb1==ejb2false
而第二次却不是递增了:
刚开始9
10
11
12
13
刚开始13
14
14
14
14
14
ejb1==ejb2false
浙公网安备 33010602011771号