现在有人可以使用 Embeddable API(使用 org.glassfish.embeddable.GlassFish
,而不是 javax.ejb.embeddable.EJBContainer
)获取服务器上下文吗?
如果有一种方法可以从正在运行的 Glassfish 获取 EJBContainer,那是可能的,但我什至找不到可用于查找的服务列表。
最佳答案
这里有一个变通方法——我们可以获取 InitialContext 作为外部客户端。 完整的解释检查EJB_FAQ .这样至少可以测试远程 EJB:
所以完整的例子看起来像这样:
//Start GF
GlassFishRuntime gfRuntime = GlassFishRuntime.bootstrap();
GlassFish gf = gfRuntime.newGlassFish();
gf.start();
//Deploy application with EJBs
Deployer deployer = gf.getService(Deployer.class);
String deployedApp = deployer.deploy(new File(...), "--force=true");
//Create InitialContext
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");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ic = new InitialContext(props);
//Lookup EJBs
ic.lookup(...)
//Stop GF
gf.stop();
gfRuntime.shutdown();
//CORBA stuck thread, have to kill it manually
System.exit(0);
请注意,末尾有一个 System.exit(0) - com.sun.corba.ee.impl.javax.rmi.CORBA.Util.KeepAlive 线程即使在服务器停止阻止 JVM 停止后仍在运行...
关于java - 从嵌入式 Glassfish 3.1 获取上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5183551/