1、不使用@Resource如何获取EJBContext/SessionContext/MessageDrivenContext的实例?
Context ctx = new InitialContext();
MessageDrivenContext context = (MessageDrivenContext) ctx.lookup("java:comp/EJBContext");
2、已通过@Resource注入的但没有定义name属性的资源如何使用JNDI访问?
使用:java:comp/env/fully-qalified-class-name/variable-name或者java:comp/env/fully-qalified-class-name/property-name
例如:
@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
@Resource
private SessionContext sctx;
public void hello() {
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext) ic.lookup("java:comp/env/com.foo.ejb.HelloBean/sctx");
System.out.println("look up injected sctx by default name: " + sctxLookup);
} catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
3、如何获取TimerService实例?
Context ctx = new InitialContext();
TimerService context = (TimerService) ctx.lookup("java:comp/TimerService");
4、stand-alone Java client如何访问glassfish中的ejb?
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
// optional. Defaults to localhost. Only needed if web server is running
// on a different host than the appserver
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
// optional. Defaults to 3700. Only needed if target orb port is not 3700.
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ic = new InitialContext(props);
// EJB 3.x, assuming a global JNDI name of "com.acme.FooRemoteBusiness"
FooRemoteBusiness foo = (FooRemoteBusiness) ic.lookup("com.acme.FooRemoteBusiness");
// EJB 2.x, assuming a global JNDI name of "com.acme.FooHome"
Object obj = ic.lookup("com.acme.FooHome");
FooHome fooHome = (FooHome) PortableRemoteObject.narrow(obj, FooHome.class);
5、ejb容器隐含的jndi对象
java:comp/UserTransaction
java:comp/TimerService
java:comp/EJBContext
java:comp/ORB
6、ejb3.0中metadata-complete的含义
metadata-complete表示当前的配置文件是否包含了所有的元数据信息
metadata-complete=true 表示只用解析当前配置文件中的元数据信息,而不用解析class文件中的注解
metadata-complete=false 或metadata-complete不存在,表示要先解析class文件中的注解,然后解析配置文件中的信息,若class文件中的注解与配置文件中的信息冲突,则使用配置文件中的信息
7、ejb异常说明
javax.ejb.NoSuchEJBException extends javax.ejb.EJBException extends java.lang.RuntimeException
javax.ejb.EJBTransactionRequiredException extends javax.ejb.EJBException extends java.lang.RuntimeException
javax.ejb.EJBTransactionRolledbackException extends javax.ejb.EJBException extends java.lang.RuntimeException