[英]NetBeans 7.0 , JUnit and Glassfish Embedded 3.1: Does not work
[英]Obtaining Context from an embedded Glassfish 3.1
現在有沒有人使用org.glassfish.embeddable.GlassFish
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停止后仍在運行...
據我所知,您可以初始化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名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.