1.所有无状态会话bean的实例都是一样的。
2.对于有状态的EJB,要限制内存中会话bean的数量,EJB容器需要把会话bean的会话状态保持到硬盘或其他存储器中,以释放内存给其他bean使用--挂起
(@PrePassive)。被挂起的bean的状态重新回到内存中--激活(@PostActive)。transient类型的bean不能被挂起。通过@PrePassive,@PostActive回调方法来释放和获得资源。
3.默认是本地接口@local。如果再本机调用EJB(客户端和EJB容器运行在同一个JVM中),采用Local接口访问EJB优于Remote接口。因为Remote接口访问EJB需奥经过远程方法调用环节;而Local接口访问EJB直接从JVM中返回EJB的引用。
4.对于客户端和EJB容器在同一个JVM中,可使用local接口访问和remote访问;对于不在同一JVM中只能用remote访问。
5.当会话bean的某些方法只提供EJB容器内部调用而不对外暴露时,可以将其定义为local接口。
例如
package com.persia.ejb; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Stateless; @Stateless @Remote({Operation.class}) @Local({LocalOperation.class}) public class OperationBean implements Operation, LocalOperation { private int total=0; private int addresult=0; public int Add(int a, int b) { addresult=a+b; return addresult; } public int getResult() { // TODO Auto-generated method stub total+=addresult; return total; } }
<% Properties props=new Properties(); props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); props.setProperty("java.naming.provider.url","localhost:1099"); props.setProperty("java.naming.factory.url.pkgs","org.jboss.naming"); InitialContext ctx; ctx=new InitialContext(props); try{ Operation h=(Operation)ctx.lookup("OperationBean/remote"); out.println("通过远程接口调用EJB:相加:"+h.Add(1,1)); } catch(NamingException e){ out.println(e.getMessage()); } try{ LocalOperation a=(LocalOperation)ctx.lookup("OperationBean/local"); out.println("通过本地接口调用EJB:相加:"+a.Add(1,1)); out.println("通过本地接口调用EJB:相加结果:"+a.getResult()); LocalOperation b=(LocalOperation)ctx.lookup("OperationBean/local"); out.println("通过本地接口调用EJB:相加:"+b.Add(1,1)); out.println("通过本地接口调用EJB:相加结果:"+b.getResult()); } catch(NamingException e){ out.println(e.getMessage()); } %>
若是本地接口调用,似乎可以直接InitialContext ctx=new InitialContext(); 但这里仍写了props,只是本地是localhost,远程是服务器地址。