【问题标题】:EJB on Wildfly calling remote EJB from another WildflyWildfly 上的 EJB 从另一个 Wildfly 调用远程 EJB
【发布时间】:2015-04-20 15:45:16
【问题描述】:

我目前的一般问题是,我的机器上运行了两个 Wildfly 8.2.0Final 实例。 我知道,有类似的问题,但它们都没有真正帮助解决我的问题。 其中一个拥有一个 restful 应用程序,当它收到一个 GET 时会触发一个无状态会话 bean SenderBean。 之后,这个无状态会话 bean 应该从远程无状态会话 bean PrintBean 调用一个方法,该方法位于另一个 wildfly 实例上。

我将首先解释我到目前为止所做的事情(也许我错过了一些东西,我对 Java EE 和 Wildfly 还很陌生)。

我将调用带有SenderBean 的Wildfly 实例@Sender 和带有PrintBeanReceiver

我创建了一个名为Stefan 的应用程序用户,密码为stefan,属于Receiver 上的组guest。 在Sender 上,在standalone-full.xml 上,我通过放置添加了一个安全领域

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

进入&lt;security-realms&gt; 部分。 我还通过放置添加了一个出站套接字绑定

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

进入&lt;socket-binding-group ...&gt; 部分。 最后,我创建了一个出站连接,通过放置

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

进入&lt;subsystem xmlns="urn:jboss:domain:remoting:2.0"&gt; 部分。

我用 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: No EJB receiver available for handling ...

我可能做错了什么吗?我对 EJB 和 Wildfly 还很陌生。 你可以在GitHub找到项目设置。

【问题讨论】:

    标签: jakarta-ee jboss ejb wildfly


    【解决方案1】:

    你可以在两台 JBoss 服务器之间找到detailed description of configuration,GitHub 上也提供了带有配置文件的源代码quickstart

    【讨论】:

      【解决方案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> 
      

      在sender bean中两行就足够了:

      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 绑定。

      【讨论】:

        【解决方案3】:

        在我看来问题在于 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

        【讨论】:

          猜你喜欢
          • 2014-08-18
          • 2014-08-22
          • 1970-01-01
          • 2020-09-16
          • 1970-01-01
          • 2014-01-16
          • 1970-01-01
          • 2015-08-06
          • 2015-05-12
          相关资源
          最近更新 更多