在从JBoss 6迁移到WildFly 10 期间获取旧版应用程序中会话bean的引用时,我面临以下JNDI查找问题。
以下代码在JBoss 6中有效,它引用了会话bean" OMGenEJB",然后用于获取代理对象" genhome"与豆子互动。
try {
InitialContext ic = new InitialContext();
Object objRef = ic.lookup("ejb/OMGenEJB");
OMGenEJBHome genhome =
(OMGenEJBHome) PortableRemoteObject.narrow ( objRef, OMGenEJBHome.class );
...
EJB主页界面 OMGenEJBHome 。
EJB实现类是OMGenBean。
bean在" ejb-jar.xml"中定义,如下所示:
<session >
<ejb-name>OMGenEJB</ejb-name>
<home>com.irisel.oms.ejb.interfaces.OMGenEJBHome</home>
<remote>com.irisel.oms.ejb.interfaces.OMGenEJB</remote>
<service-endpoint>com.irisel.oms.ws.OMBrowserEndpoint</service-endpoint>
<ejb-class>com.irisel.oms.ejb.browser.OMGenBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
现在,客户端类属于与EJB jar位于同一EAR中的WAR。
正确部署EAR,启动时服务器记录以下 JNDI条目:
21:39:18,770 INFO [org.jboss.as.ejb3.deployment] (MSC service thread 1-3) WFLYEJB0473: JNDI bindings for session bean named 'OMGenEJB' in deployment unit 'subdeployment "HolmesEJB-2.0.jar" of deployment "HolmesBSS-2.0.ear"' are as follows:
java:global/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJB
java:app/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJB
java:module/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJB
java:jboss/exported/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJB
java:global/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.browser.OMGenBean
java:app/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.browser.OMGenBean
java:module/OMGenEJB!com.irisel.oms.ejb.browser.OMGenBean
java:global/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome
java:app/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome
java:module/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome
java:jboss/exported/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome
下面是在初始上下文中打印JNDI条目的修改,如客户端所示,&#34; java:comp / env&#34;
protected void init2(Credentials creds) throws SQLException, ConfigException, RemoteException {
logger = org.apache.log4j.Logger.getLogger(this.getClass());
try {
logger.info("Connecting to ejb/OMGenEJB");
InitialContext ic = new InitialContext();
// this code lists the entries in the initial context
NamingEnumeration<NameClassPair> list = ic.list("");
while (list.hasMore()) {
System.out.println(list.next().getName());
}
Context envCtx = (Context)ic.lookup("java:comp/env");
// this code lists the entries in java:comp/env context
list = envCtx.list("");
while (list.hasMore()) {
System.out.println(list.next().getName());
}
Object objRef2 = envCtx.lookup("ejb/OMGenEJB");
logger.info("Narrowing to class OMGenEJBHome #"+OMGenEJBHome.class.hashCode());
OMGenEJBHome genhome = (OMGenEJBHome)PortableRemoteObject.narrow(objRef2, OMGenEJBHome.class);
...
修改后的代码生成这些日志:
23:23:43,445 INFO [com.irisel.oms.olapi.CLEJBBrowser] (default task-16) Connecting to ejb/OMGenEJB
23:23:44,274 INFO [stdout] (default task-16) Mail
23:23:44,669 INFO [stdout] (default task-16) TransactionManager
事实证明,初始上下文是&#34; java:&#34; ,因为Mail和TransactionManager具有JNDI名称&#34; java:/ Mail&#34;和&#34; java:/ TransactionManager&#34;分别。但是,我希望相对路径是相对于java:comp / env。
接下来,我们会看到条目&#34; ejb&#34;:
23:23:46,163 INFO [stdout] (default task-16) com.irisel.oms.ws.server.KnockKnockImpl
23:23:46,789 INFO [stdout] (default task-16) ejb
23:23:50,146 INFO [stdout] (default task-16) com.irisel.oms.ws.server.HolmesImpl
我的问题是
为什么查找&#34; ejb / OMGenEJB&#34;修改代码
也失败了23:23:51,680 INFO [stdout](默认任务-16)异常:WFLYNAM0062:无法查找env / ejb / OMGenEJB
如果我们尝试查找&#34; ejb&#34;相反,查找工作正常。
所以我也尝试了这个:
try {
logger.info("Connecting to ejb/OMGenEJB");
InitialContext ic = new InitialContext();
NamingEnumeration<NameClassPair> list = ic.list("");
while (list.hasMore()) {
System.out.println("ic entry:"+list.next().getName());
}
Context envCtx = (Context)ic.lookup("java:comp/env/ejb");
list = envCtx.list("");
while (list.hasMore()) {
System.out.println("envCtx entry:"+list.next().getName());
}
Object objRef2 = envCtx.lookup("OMGenEJB");
logger.info("Narrowing to class OMGenEJBHome #"+OMGenEJBHome.class.hashCode());
OMGenEJBHome genhome = (OMGenEJBHome)PortableRemoteObject.narrow(objRef2, OMGenEJBHome.class);
//browser = genhome.create();
//className = browser.getClassNames();
genejb = genhome.create();
appvo = (AppVO) AppVO.readObject64(this.genejb.exec("getApp",null,null));
} catch (Exception ex) {
System.out.println("Exception :"+ ex.getMessage());
ConfigException ce = new ConfigException("Cannot initialize remote EJB client");
ce.initCause(ex);
throw ce;
}
生成这些日志:
00:35:52,048 INFO [com.irisel.oms.olapi.CLEJBBrowser] (default task-2) Connecting to ejb/OMGenEJB
00:35:52,823 INFO [stdout] (default task-2) ic entry:Mail
00:35:53,123 INFO [stdout] (default task-2) ic entry:TransactionManager
00:35:53,992 INFO [stdout] (default task-2) envCtx entry:OMBrowserEJB
00:35:54,314 INFO [stdout] (default task-2) envCtx entry:OMGenEJB
00:36:25,649 INFO [stdout] (default task-2) Exception :WFLYNAM0062: Failed to lookup env/ejb/OMGenEJB
00:36:25,650 ERROR [stderr] (default task-2) com.irisel.util.ConfigException: Cannot initialize remote EJB client
00:36:25,650 ERROR [stderr] (default task-2) at com.irisel.oms.olapi.CLEJBBrowser.init2(CLEJBBrowser.java:82)
...
00:36:25,671 ERROR [stderr] (default task-2) Caused by: javax.naming.NamingException: WFLYNAM0062: Failed to lookup env/ejb/OMGenEJB [Root exception is java.lang.RuntimeException: javax.naming.NameNotFoundException: ejb/OMGenEJB -- service jboss.naming.context.java.ejb.OMGenEJB]
00:36:25,671 ERROR [stderr] (default task-2) at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:157)
... ... 39 more
00:36:25,672 ERROR [stderr] (default task-2) Caused by: java.lang.RuntimeException: javax.naming.NameNotFoundException: ejb/OMGenEJB -- service jboss.naming.context.java.ejb.OMGenEJB
00:36:25,672 ERROR [stderr] (default task-2) at org.jboss.as.ejb3.deployment.processors.EjbLookupInjectionSource$1.getReference(EjbLookupInjectionSource.java:99)
00:36:25,672 ERROR [stderr] (default task-2) at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:143)
00:36:25,676 ERROR [stderr] (default task-2) ... 44 more
00:36:25,676 ERROR [stderr] (default task-2) Caused by: javax.naming.NameNotFoundException: ejb/OMGenEJB -- service jboss.naming.context.java.ejb.OMGenEJB
00:36:25,676 ERROR [stderr] (default task-2) at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
00:36:25,677 ERROR [stderr] (default task-2) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
00:36:25,677 ERROR [stderr] (default task-2) at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:235)
00:36:25,677 ERROR [stderr] (default task-2) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
00:36:25,677 ERROR [stderr] (default task-2) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
00:36:25,677 ERROR [stderr] (default task-2) at javax.naming.InitialContext.lookup(InitialContext.java:417)
00:36:25,677 ERROR [stderr] (default task-2) at javax.naming.InitialContext.lookup(InitialContext.java:417)
00:36:25,677 ERROR [stderr] (default task-2) at org.jboss.as.ejb3.deployment.processors.EjbLookupInjectionSource$1.getReference(EjbLookupInjectionSource.java:81)
00:36:25,677 ERROR [stderr] (default task-2) ... 45 more
工作的解决方案是使用全局或应用名称,问题是它包含耳朵名称和版本......
"java:global/HolmesBSS-2.0/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome"
"java:app/HolmesEJB-2.0/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome"
这些无法正常工作
"java:module/OMGenEJB!com.irisel.oms.ejb.browser.OMGenBean"
"java:module/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome"
"java:comp/env/ejb/OMGenEJB"
如果您认为这符合Java-7规范,我想与您联系。
由于
答案 0 :(得分:2)
Java EE 7的可移植JNDI名称具有以下形式:
java:global/<app-name>/<module-name>/<bean-name>!<fully-qualified-bean-interface-name>
默认应用程序名称是没有扩展名(.ear)的耳朵的名称。您可以在application.xml中设置应用程序名称
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
<application-name>myApp</application-name>
</application>
默认模块名称是没有扩展名(.jar)的ejb-jar名称。您可以在ejb-jar.xml中更改它
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
version = "3.1"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
<module-name>myModule</module-name>
</ejb-jar>
结果是:
java:global/myApp/myModule/OMGenEJB!com.irisel.oms.ejb.interfaces.OMGenEJBHome
注意:您不能使用ejb-jar.xml中的“mappedName”绑定到不同的JNDI名称,因为Wildfly不允许它。 Wildfly只允许上述形式。