使用Wildfly上的EJB,调用另一个Wildfly上的远程EJB

6

我的当前问题是,我在我的计算机上运行着两个Wildfly 8.2.0Final实例。我知道有类似的问题,但没有一个能真正帮助我解决问题。其中一个包含一个restful应用程序,当它接收到GET请求时触发一个无状态会话bean SenderBean。之后,这个无状态会话bean应该调用位于另一个wildfly实例上的远程无状态会话bean PrintBean 的方法。

首先,我要解释一下我到目前为止做了什么(也许我漏掉了什么,我对Java EE和Wildfly还很陌生)。

我将拥有SenderBean的Wildfly实例称为Sender,拥有PrintBean的实例称为Receiver

我创建了一个名为Stefan的应用程序用户,密码为stefan,属于Receiver上的guest组。在Sender中,在standalone-full.xml中添加了一个安全域,通过以下方式实现:

<security-realm name="ejb-security-realm">
  <server-identities>
    <secret value="c3R1ZmFu"/>
  </server-identities>
</security-realm>

将其添加到<security-realms>部分中。我还通过以下方式添加了一个出站套接字绑定:

<outbound-socket-binding name="remote-ejb">
  <remote-destination host="localhost" port="8080"/>
</outbound-socket-binding>

将内容添加到<socket-binding-group ...>部分。 最后,我通过放置代码创建了一个出站连接。

<outbound-connections>
  <remote-outbound-connection name="remote-ejb-connection" outbound-socket-binding-ref="remote-ejb" username="Stefan" security-realm="ejb-security-realm">
    <properties>
      <property name="SASL_POLICY_NOANONYMOUS" value="false"/>
      <property name="SSL_ENABLED" value="false"/>
    </properties>
  </remote-outbound-connection>
</outbound-connections>

将内容添加到<subsystem xmlns="urn:jboss:domain:remoting:2.0">部分。

我使用CLI命令standalone.bat -c standalone-full.xml -Djboss.socket.binding.port-offset=100 -Djboss.node.name=Sender启动Sender,并使用standalone.bat -c standalone-full.xml -Djboss.node.name=Receiver启动Receiver

Sender上的本地无状态会话Bean称为SenderBean

@Stateless
public class SenderBean implements SenderService {

  private static final Logger logger = Logger.getLogger(SenderBean.class.getSimpleName());

  public void send(){
    logger.info("Trying to invoke");
    this.invoke();
  }

  private void invoke() {
    Properties clientProperties = new Properties();
    clientProperties.put("remote.connections", "default");
    clientProperties.put("remote.connection.default.port", "8080");
    clientProperties.put("remote.connection.default.host", "localhost");

    Properties properties = new Properties();
    properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");        

    try {
        Context context = new InitialContext(properties);
        context = new InitialContext(properties);
        Object x = context.lookup("ejb:baseproject-ear-01.00.00-SNAPSHOT/testdomain-service-01.00.00-SNAPSHOT/Receiver/PrintBean!com.schubert.baseproject.testdomain.service.PrintService");
        logger.info("Obtained some object "+x.toString());
        logger.info("Trying to cast.");
        PrintService s = (PrintService) x;
        logger.info("Cast successful");
        logger.info("Printing using remote ejb: "+s.print("Markus"));
    } catch (NamingException e) {
        e.printStackTrace();
    }
  } 
}

而且Receiver包含PrintBean

@Stateless
@Remote(PrintService.class)
public class PrintBean implements PrintService {

  @Override
  public String print(String name) {
    return "Hello " + name;
  }
}

现在的问题是,我总是收到一个IllegalStateException,它说EJBCLIENT000025:没有可用于处理...的EJB接收器。

我可能做错了什么吗?我对EJB和Wildfly相当新。您可以在GitHub上找到项目设置。

3个回答

2

您应该将文件jboss-ejb-client.xml添加到发送者EAR(而不是WAR)中。将其放置在application.xml旁边。

jboss-ejb-client.xml的内容:

<jboss-ejb-client>
    <client-context>
        <ejb-receivers>
            <remoting-ejb-receiver outbound-connection-ref="remote-ejb-connection"/>
        </ejb-receivers>
    </client-context>
</jboss-ejb-client> 

在发送方豆中,只需要两行代码:
Context context = new InitialContext();
Object x = context.lookup("ejb:baseproject-ear-01.00.00-SNAPSHOT/testdomain-service-01.00.00-SNAPSHOT/PrintBean!com.schubert.baseproject.testdomain.service.PrintService");

请注意,我从路径中删除了“Receiver /”。您可以在服务器日志中找到JNDI绑定。

0

0

我认为问题出在InitialContext参数上,服务器配置是正确的。尝试按照我的远程队列连接示例操作,在普通企业bean中也可以探索这种情况(插入正确的用户登录名和密码):

env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
    env.put(Context.SECURITY_PRINCIPAL, "client");
    env.put(Context.SECURITY_CREDENTIALS, "q");

    Context ctx = new InitialContext(env);
    connectionFactory = (ConnectionFactory)ctx.lookup("/jms/RemoteConnectionFactory");
    connection = connectionFactory.createConnection("client", "q");

请记住,具有外部访问权限的jndi资源必须从服务器配置的java:/jboss/exported/开始,但在客户端,您可以省略这些单词。此指令适用于WildFly,但不适用于JBoss EAP/AS和其他产品。如需了解更多信息,请参阅link


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