没有远程出站连接的Wildfly到Wildfly EJB客户端

时间:2015-05-11 21:59:22

标签: jboss jboss7.x wildfly wildfly-8

我希望能够有两个Wildfly(或JBoss 7)实例,其中一个服务器与另一个服务器上的EJB通信。棘手的部分是according to documentation,需要创建带有出站套接字绑定的远程出站连接。这对我们的运营团队来说是一个很大的麻烦,特别是当我们想要扩展时。

Wildfly实例有没有办法通过以编程方式指定远程主机来调用另一个Wildfly实例上的EJB?

我已经能够让Tomcat 7调用Wildfly EJB了。我在org.jboss.as:jboss-as-ejb-client-bom:7.5.0.Final-redhat-21上添加了一个Maven依赖项,并根据this documentation设置了连接设置。

谢谢!

修改

当我尝试使用在Tomcat 7中工作的相同代码(使用jboss-ejb-client库)时,当我的代码尝试EJBCLIENT000021: EJB client context selector may not be changed时,我收到错误EJBClientContext.setSelector( selector )。我是以编程方式设置远程连接主机和端口,而不是使用jboss-ejb-client.properties。

2 个答案:

答案 0 :(得分:2)

jgitter的回答让我大部分都在那里。这就是我最终的结果:

  /**
   * @return a reference to the EJB
   * @throws EjbLookupException
   */
  @NotNull
  public T lookup ()
     throws EjbLookupException
  {
     String path = createJndiPath();
     Context initialContext = null;
     try
     {
        initialContext = createInitialContext();

        //noinspection unchecked
        final T ejb = (T)initialContext.lookup( path );

        if( m_apiVersion != null )
        {
           ( (RemoteAPI)ejb ).validateClientCompatibility( m_apiVersion );
        }

        return ejb;
     }
     catch( NamingException | RuntimeException e )
     {
        throw new EjbLookupException( "Unable to find the JBoss EJB at " + path, e );
     }
     finally
     {
        if( initialContext != null )
        {
           //noinspection ThrowableResultOfMethodCallIgnored
           Closer.close( initialContext );
        }
     }
  }

  /**
   * There are a lot of ways to do JBoss 7 / Wildfly EJB lookups.  Using this method, we don't have to create
   * outbound socket bindings whenever we want to use a remote EJB.
   *
   * @throws NamingException
   */
  @NotNull
  private Context createInitialContext ()
     throws NamingException
  {
     Properties properties = new Properties();

     properties.put( Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming" );
     properties.put( "org.jboss.ejb.client.scoped.context", "true" );
     properties.put( "remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false" );
     properties.put( "remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false" );
     properties.put( "remote.connections", "default" );

     properties.put( "remote.connection.default.host", m_host );
     properties.put( "remote.connection.default.port", String.valueOf( m_port ) );

     if( m_username != null )
     {
        properties.put( "remote.connection.default.username", m_username );
     }
     if( m_password != null )
     {
        properties.put( "remote.connection.default.password", m_password );
     }

     return new InitialContext( properties );
  }

  public static class EjbLookupException
     extends Exception
  {
     EjbLookupException (
        @NotNull String message,
        @NotNull Throwable cause )
     {
        super( message, cause );
     }
  }

我不确定是否需要scoped context,我可能无法正确关闭连接。我将根据我发现的内容更新此答案。

答案 1 :(得分:1)

您不必使用远程出站连接。您可以像编写任何外部客户端一样编写代码。请参阅:https://docs.jboss.org/author/display/WFLY9/EJB+invocations+from+a+remote+client+using+JNDI