Java 从嵌入的Glassfish 3.1中获取上下文
现在是否有人找到了使用可嵌入API(使用Java 从嵌入的Glassfish 3.1中获取上下文,java,glassfish-embedded,Java,Glassfish Embedded,现在是否有人找到了使用可嵌入API(使用org.glassfish.embeddeble.glassfish,而不是javax.ejb.embeddeble.EJBContainer)获取服务器上下文的方法? 如果有办法从运行的Glassfish获取EJBContainer,这是可能的,但我甚至找不到可供查找的服务列表。这里有一个解决方法-我们可以作为外部客户端获取InitialContext。 查看完整的解释检查。这样至少可以测试远程EJB: 因此,完整示例如下所示: //Start GF G
org.glassfish.embeddeble.glassfish
,而不是javax.ejb.embeddeble.EJBContainer
)获取服务器上下文的方法?
如果有办法从运行的Glassfish获取EJBContainer,这是可能的,但我甚至找不到可供查找的服务列表。这里有一个解决方法-我们可以作为外部客户端获取InitialContext。 查看完整的解释检查。这样至少可以测试远程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停止之后 据我所知,您可以初始化
InitialContext
类以获取上下文,该上下文可进一步用于执行查找。这是经过测试的,并且发现在查找部署在嵌入式容器中的EJB的上下文中是有效的。EJB未配置为允许访问特定角色,在这种情况下,com.sun.appserv.security.programmaticologin
类(未通过嵌入式EJB API公开)可能会有所帮助;这没有经过测试,但建议使用此方法为访问EJB的线程初始化主体
从Maven运行并在POM中使用嵌入的Glassfish依赖项的一个或多或少完整的示例(为简洁起见,此处不复制)如下所示:
EJB接口:
会话Bean:
单元测试:
请注意,EJB是使用显式可移植JNDI名称(通过@EJB
注释)部署的,因为我有其他测试在其他测试中使用公共可嵌入EJB API,在这些测试中指定应用程序名称或多或少有些困难;每次测试执行可能会导致EJB使用不同的JNDI名称,因此需要指定显式JNDI名称
public interface EchoManager
{
String echo(String message);
}
@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());
}
}
}