有狀態(tài)和無(wú)狀態(tài)的會(huì)話bean都在客戶端產(chǎn)生不同的代理實(shí)例
不同的是在服務(wù)器端,有狀態(tài)的每次lookup都是新的獨(dú)立的bean,而無(wú)狀態(tài)的是單例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); //第一次會(huì)話 StatelessEjb se1=(StatelessEjb)cxt.lookup("StatelessEjbBean/remote"); System.out.println("剛開(kāi)始"+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()); //第二次會(huì)話 StatelessEjb se2=(StatelessEjb)cxt.lookup("StatelessEjbBean/remote"); System.out.println("剛開(kāi)始"+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)); } }
第一次運(yùn)行結(jié)果
剛開(kāi)始0
1
2
3
4
剛開(kāi)始4
5
6
7
8
9
ejb1==ejb2false
而第二次卻不是遞增了:
剛開(kāi)始9
10
11
12
13
剛開(kāi)始13
14
14
14
14
14
ejb1==ejb2false