现在有人使用Embeddable API获取服务器上下文(使用org.glassfish.embeddable.GlassFish
,而不是javax.ejb.embeddable.EJBContainer
)吗?
如果有办法从正在运行的Glassfish中获取EJBContainer,那么我甚至找不到可用于查找的服务列表。
答案 0 :(得分:1)
这是一种解决方法 - 我们可以将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停止后也在运行...
答案 1 :(得分:0)
据我所知,您可以初始化InitialContext
类以获取可以进一步用于执行查找的上下文。对此进行了测试,发现它在查找嵌入式容器中部署的EJB的上下文中工作。 EJB未配置为允许访问特定角色,在这种情况下,com.sun.appserv.security.ProgrammaticLogin
类(不通过Embeddable EJB API公开)可能有所帮助;这没有经过测试,但是建议的方法是为访问EJB的线程初始化Principal
。
从Maven运行并在POM中使用嵌入式Glassfish依赖项的一个或多或少完整的示例(为简洁起见,此处不再转载)如下:
EJB接口:
public interface EchoManager
{
String echo(String message);
}
会话Bean:
@Local(EchoManager.class)
@Stateless
@EJB(name="java:global/glassfish-ejb-validation/EchoManager",beanInterface=EchoManager.class)
public class EchoManagerBean implements EchoManager
{
public String echo(String message)
{
return message;
}
}
单元测试:
public class EchoManagerTest
{
@Rule
public TestName testMethod = new TestName();
private static final Logger logger = Logger.getLogger(EchoManagerTest.class.getName());
@Test
public void testEchoWithGlassfishRuntime() throws Exception
{
logger.info("Starting execution of test" + testMethod.getMethodName());
GlassFish glassFish = null;
Deployer deployer = null;
String appName = null;
try
{
//Setup
BootstrapProperties bootstrapProps = new BootstrapProperties();
GlassFishRuntime glassFishRuntime = GlassFishRuntime.bootstrap(bootstrapProps);
GlassFishProperties gfProps = new GlassFishProperties();
glassFish = glassFishRuntime.newGlassFish(gfProps);
glassFish.start();
deployer = glassFish.getDeployer();
ScatteredArchive archive = new ScatteredArchive("glassfish-ejb-validation", Type.JAR);
archive.addClassPath(new File("target", "classes"));
archive.addClassPath(new File("target", "test-classes"));
appName = deployer.deploy(archive.toURI(), "--force=true");
// Setup the context
InitialContext context = new InitialContext();
//Execute (after lookup the EJB from the context)
EchoManager manager = (EchoManager) context.lookup("java:global/glassfish-ejb-validation/EchoManager");
String echo = manager.echo("Hello World");
//Verify
assertEquals("Hello World", echo);
}
finally
{
if(deployer != null && appName != null)
{
deployer.undeploy(appName);
}
if(glassFish != null)
{
glassFish.stop();
glassFish.dispose();
}
logger.info("Ending execution of test" + testMethod.getMethodName());
}
}
}
请注意,EJB使用显式的可移植JNDI名称(通过@EJB
注释)进行部署,因为我在其他测试中使用公共可嵌入EJB API的其他测试,或多或少都很难在此类测试中指定应用程序名称;每次测试执行都可能导致EJB的JNDI名称不同,因此需要指定显式的JNDI名称。