简体   繁体   English

Wildfly远程EJB调用

[英]Wildfly Remote EJB Invocation

I am trying to invoke a stateless EJB, deployed on a remote server. 我试图调用部署在远程服务器上的无状态EJB。 I can invoke the bean from my local JBoss environment but when I change the remote.connection.default.host to the remote machine's host, my client code does not work. 我可以从本地JBoss环境调用Bean,但是当将remote.connection.default.host更改为远程计算机的主机时,我的客户端代码不起作用。

This is my jboss-ejb-client.properties : 这是我的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

And my client code looks like this: 我的客户代码如下所示:

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);

Please help. 请帮忙。

Thanks all. 谢谢大家 Jack. 插口。

This answer may be late but I faced the same problem, none of the above answers helped me, to solve this problem, refer the following : http://blog.jonasbandi.net/2013/08/jboss-remote-ejb-invocation-unexpected.html 这个答案可能很晚,但是我遇到了同样的问题,以上答案都没有帮助我解决此问题,请参考以下内容: http : //blog.jonasbandi.net/2013/08/jboss-remote-ejb-invocation -unexpected.html

The code that works for me is as below: 适用于我的代码如下:

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());

Foolishly believing that I understood EJB deployment on Wildfly, I struggled with remote invocation of my stateless EJBs from a Servlet for several days, trying various configurations from snippets of code that I found on the net. 愚蠢地相信我了解Wildfly上的EJB部署,我努力了几天,从Servlet远程调用我的无状态EJB,并尝试从网上找到的代码片段尝试各种配置。 No luck. 没运气。

I finally came across the following guide from JBOSS which had my problems resolved in about 10 minutes. 我终于找到了JBOSS以下指南,指南在大约10分钟内解决了我的问题。 I had incorrectly configured my remote outbound connection. 我没有正确配置远程出站连接。 The Wildfly developers have written an awesome Java EE application server, but they are terrible with explanatory error messages. Wildfly开发人员已经编写了很棒的Java EE应用程序服务器,但是由于解释错误消息而使他们感到可怕。

The guide is for AS7.2 but it was relevant for my Wildfly 8.2.0 platform. 该指南适用于AS7.2,但与我的Wildfly 8.2.0平台有关。

Hopefully this will save someone the grief that I suffered. 希望这可以减轻我遭受的痛苦。

What worked for me: 对我有用的是:

My client was in a standalone maven project. 我的客户在一个独立的Maven项目中。 All I needed to do was to add this dependency: 我要做的就是添加此依赖项:

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

I've come to this solution by looking at the ejb-remote example. 我通过查看ejb-remote示例来解决此问题。

Your client code has to use EJB 3 standardized JNDI name to access your beans 您的客户代码必须使用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);

And check WildFly server.log for the exported JNDI binding of your bean: java:jboss/exported/myapp/mymodule/myEjbName!my.own.package.EjbInterface 并检查WildFly server.log以获取您的bean的已导出JNDI绑定: java:jboss/exported/myapp/mymodule/myEjbName!my.own.package.EjbInterface

To pass security checks, username must be defined in ApplicationRealm . 要通过安全检查,必须在ApplicationRealm定义用户名。

Usually in Java EE, you let the container do the lookup instead of doing it yourself. 通常,在Java EE中,让容器执行查找,而不是自己执行。 From the Java EE 6 tutorial : Java EE 6教程中

To obtain a reference to the remote business interface of an enterprise bean through dependency injection, use the javax.ejb.EJB annotation and specify the enterprise bean's remote business interface name: 要通过依赖注入获得对企业bean的远程业务接口的引用,请使用javax.ejb.EJB批注并指定企业bean的远程业务接口名称:

@EJB Example example;

If it has to be JNDI lookup, check out this JBoss tutorial - it's for version 7 though, but maybe it is of help for your case. 如果必须是JNDI查找,请查看此JBoss教程 -尽管它适用于版本7,但也许对您的情况有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM