我在Wildfly中部署了一个EJB3应用程序,其中包含一个模块和一个名为User Data的会话bean。 这是我的项目结构:
这是UserData有状态Bean:
@Stateful
@Remote(UserDataRemote.class)
public class UserData implements UserDataRemote, UserDataLocal {
private String name;
/**
* Default constructor.
*/
public UserData() {
// TODO Auto-generated constructor stub
}
@Override
public void saveName(String name) {
// TODO Auto-generated method stub
this.name =name;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
}
这是控制台的裁剪:
这就是我试图通过JNDI从应用程序中检索它的方式:
public static void main (String args[]) {
Properties jndiProperties = new Properties();
jndiProperties.put("jboss.naming.client.ejb.context", true);
jndiProperties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
try {
InitialContext namingContext = new InitialContext(jndiProperties);
UserDataRemote userRemote = (UserDataRemote)namingContext.lookup("java:global/testEJB/UserData!entities.UserDataRemote");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
**更新** 我也试图使用这个URL进行查找:
UserDataRemote userRemote = (UserDataRemote)namingContext.lookup("ejb:/testEJB//UserData!entities.UserDataRemote?stateful");
但它也找不到它。
这是我得到的错误:
ott 06, 2017 7:45:51 AM org.xnio.Xnio <clinit>
INFO: XNIO version 3.4.0.Final
ott 06, 2017 7:45:51 AM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.4.0.Final
ott 06, 2017 7:45:51 AM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.21.Final
ott 06, 2017 7:45:52 AM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
ott 06, 2017 7:45:52 AM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@6b09bb57, receiver=Remoting connection EJB receiver [connection=Remoting connection <13359fd6> on endpoint "config-based-naming-client-endpoint" <6536e911>,channel=jboss.ejb,nodename=macbook-pro-di-andrea]} on channel Channel ID 96d8a7d7 (outbound) of Remoting connection 1f36e637 to /127.0.0.1:8080 of endpoint "config-based-naming-client-endpoint" <6536e911>
javax.naming.NameNotFoundException: global/testEJB/UserData!entities.UserDataRemote -- service jboss.naming.context.java.jboss.exported.global.testEJB."UserData!entities.UserDataRemote"
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)
at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
答案 0 :(得分:2)
如果要直接部署EJB JAR,则查找应为
UserDataRemote userRemote =
(UserDataRemote) namingContext.lookup("testEJB/UserData!entities.UserDataRemote");
如果EJB JAR打包在EAR中,那么您需要添加EAR名称(应用程序名称)
UserDataRemote userRemote =
(UserDataRemote) namingContext.lookup("<ear-name>/testEJB/UserData!entities.UserDataRemote");
答案 1 :(得分:-1)
我想,你应该为./manage.py makemigrations another_app
使用其他值,并使用另一个值来查找EJB。我在下面修改了你的代码。
jndiProperties
给定的查找值适用于部署在JAR文件(不是EAR文件)中的无状态EJB。如果您的EJB是有状态的,则必须在最后添加以下public static void main (String args[]) {
Properties jndiProperties = new Properties();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProperties.put("jboss.naming.client.ejb.context", true);
try {
InitialContext namingContext = new InitialContext(jndiProperties);
UserDataRemote userRemote = (UserDataRemote)namingContext.lookup("ejb:/testEJB/UserData!entities.UserDataRemote");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
。
如何为EAR文件创建查找值在下面的链接中描述。
我的Wildfly服务器显示的JNDI-Bindings与您的类似。他们没有以"?stateful"
开头。但查询仍然有效。
此外,运行时类路径中应该有一个文件ejb:...
。 jboss-client.jar中的类将查找此文件。
以下是我的内容:
jboss-ejb-client.properties
您可以在link找到更多信息。