野飞(Remote EJB Invocation)

8

我正在尝试调用部署在远程服务器上的无状态EJB。我可以从本地的JBoss环境中调用该bean,但是当我将remote.connection.default.host更改为远程机器的主机时,我的客户端代码就无法工作。

这是我的jboss-ejb-client.properties文件:

endpoint.name=client-endpoint

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

remote.connections=default

remote.connection.default.host=SERVERIP/HOSTNAME
remote.connection.default.port=8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=username
remote.connection.default.password=Password

我的客户端代码如下:

Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
String jndi = "jndi_name";
Context context = new InitialContext(properties);
obj = context.lookup(jndi);

请帮忙。

谢谢大家。 Jack。


1
“不起作用”到底是指什么?你用的查找方式是什么?因为这可能是错误的,而你简化的“jndi_name”可能是其中重要的部分。 - Alexander Rühl
我无法调用远程EJB,但我能够调用本地EJB。我在我的jboss-ejb-client.properties文件中有localhost,并且它可以正常工作。但是当我将其更改为远程服务器的IP或主机名时,它就无法工作了。请给予建议。 - Jack Ant
当我尝试连接时,出现以下错误:"无法为连接到hostname:8080的EJB接收器注册。java.lang.RuntimeException:操作失败,状态为WAITING"。 - Jack Ant
调用EJB的远程端口可能是4447。 - Alexander Rühl
@Geziefer WildFly使用8080端口,4447端口用于JBoss AS 7。 您的JNDI查找字符串是什么? - gcvt
请查看此链接:https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI - Jason Holmberg
5个回答

3
这个答案可能有些晚了,但我也遇到了同样的问题。以上的答案都没有帮助到我,要解决这个问题,请参考以下链接:http://blog.jonasbandi.net/2013/08/jboss-remote-ejb-invocation-unexpected.html 下面是我用的代码:
Properties jndiProperties=new Properties();
    jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
    jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    jndiProperties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080/");
    //This property is important for remote resolving
    jndiProperties.put("jboss.naming.client.ejb.context", true);
    //This propert is not important for remote resolving
    jndiProperties.put("org.jboss.ejb.client.scoped.context", "true");

    Context context=new InitialContext(jndiProperties);


    /*
    java:global/JEETest_Project/EJBTest_Project/GenericStateless!test.stateless.GenericStateless
    java:app/EJBTest_Project/GenericStateless!test.stateless.GenericStateless
    java:module/GenericStateless!test.stateless.GenericStateless
    java:jboss/exported/JEETest_Project/EJBTest_Project/GenericStateless!test.stateless.GenericStateless
    java:global/JEETest_Project/EJBTest_Project/GenericStateless
    java:app/EJBTest_Project/GenericStateless
    java:module/GenericStateless
     */

    //None of the above names work for remote EJb resolution ONLY THIS WORKS - 
    //"/JEETest_Project/EJBTest_Project/GenericStateless!test.stateless.GenericStateless"

    GenericStateless bean=(GenericStateless)context.lookup("/JEETest_Project/EJBTest_Project/GenericStateless!test.stateless.GenericStateless");

    //GenericStateless bean=(GenericStateless)c.lookup("GenericStateless!test.stateless.GenericStateless");
    System.out.println(bean.getInt());

这是我尝试过的第一种方法!我已经尝试了(太)多次 :) - Peter Butkovic

1
愚蠢地认为我理解了Wildfly上的EJB部署,我为从Servlet远程调用我的无状态EJB而苦苦挣扎了几天,尝试了我在网上找到的各种配置代码片段,但没有成功。
最终,我找到了来自JBOSS的以下指南,它在大约10分钟内解决了我的问题。我错误地配置了我的远程出站连接。Wildfly开发人员编写了一个令人惊叹的Java EE应用服务器,但他们对解释性错误消息非常糟糕。
该指南适用于AS7.2,但对我的Wildfly 8.2.0平台也是相关的。
希望这能够帮助某个遭受过我痛苦的人。

1

我的做法:

我的客户端在一个独立的Maven项目中。我只需要添加这个依赖项:

<dependency>
    <groupId>org.wildfly</groupId>
    <artifactId>wildfly-ejb-client-bom</artifactId>
    <version>10.1.0.Final</version>
    <type>pom</type>
    <scope>runtime</scope>
</dependency>

我通过查看ejb-remote示例来得出这个解决方案。


1
链接到主分支(因为10.x已经过时):https://github.com/wildfly/quickstart/tree/master/ejb-remote - Bruno Medeiros

0

你的客户端代码必须使用EJB 3标准化的JNDI名称来访问你的Bean。

Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
// Not needed but I like to see it when debugging
properties.put(Context.PROVIDER_URL, "http-remoting://localhost:8080/");
// EJB 3 bean naming convention
String jndi = "ejb:/myapp/mymodule/myEjbName!my.own.package.EjbInterface";
Context context = new InitialContext(properties);
obj = context.lookup(jndi);

请检查WildFly服务器日志,查看您的bean导出的JNDI绑定:java:jboss/exported/myapp/mymodule/myEjbName!my.own.package.EjbInterface

为了通过安全检查,用户名必须在ApplicationRealm中定义。


-2
通常在Java EE中,您让容器来执行查找而不是自己执行。从Java EE 6教程中可以看到:

要通过依赖注入获取企业Bean的远程业务接口的引用,请使用javax.ejb.EJB注释并指定企业Bean的远程业务接口名称:

@EJB Example example;

如果必须进行JNDI查找,请查看this JBoss tutorial - 尽管它是针对版本7的,但也可能对您有所帮助。


这个问题是关于如何从远程服务器调用一个EJB。 - wibobm
@wibobm:你是因为评论中的原因对我的答案进行了投票吗?那么你可能需要阅读我提到的Java EE教程章节,特别是“远程客户端”子章节。如果你在一台机器上有一个@Remote接口EJB,在另一台机器上可以通过使用@EJB注入来访问它。 - Alexander Rühl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接