Wildfly 到 Wildfly EJB 客户端,无需远程出站连接 [英] Wildfly to Wildfly EJB client without remote-outbound-connections

查看:13
本文介绍了Wildfly 到 Wildfly EJB 客户端,无需远程出站连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够拥有两个 Wildfly(或 JBoss 7)实例,其中一台服务器与另一台服务器上的 EJB 对话.棘手的部分是 根据文档,需要创建带有出站套接字绑定的远程出站连接.这对我们的运营团队来说是一个很大的麻烦,尤其是当我们想要扩展时.

I'd like to be able to have two Wildfly (or JBoss 7) instances where one of the servers talks to EJBs on the other server. The tricky part is that according to documentation, remote-outbound-connections with outbound-socket-bindings need to be created. This is a big hassle for our Operations team, especially when we want to scale out.

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

Is there any way for a Wildfly instance to call EJB's on another Wildfly instance by programmatically specifying the remote host?

我已经能够让 Tomcat 7 调用 Wildfly EJB.我在 org.jboss.as:jboss-as-ejb-client-bom:7.5.0.Final-redhat-21 上添加了 Maven 依赖,并根据 本文档.

I've been able to have Tomcat 7 invoke Wildfly EJB's. I added a Maven dependency on org.jboss.as:jboss-as-ejb-client-bom:7.5.0.Final-redhat-21 and set up connection settings according to this documentation.

谢谢!

编辑

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

When I try the same code that worked in Tomcat 7 (which uses the jboss-ejb-client library), I get the error EJBCLIENT000021: EJB client context selector may not be changed when my code tries to do EJBClientContext.setSelector( selector ). I am setting the remote connection host and port programmatically instead of using jboss-ejb-client.properties.

推荐答案

jgitter 的回答让我了解了大部分情况.这是我最后的结果:

jgitter's answer got me most of the way there. Here's what I ended up with:

  /**
   * @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 );
     }
  }

我不确定是否需要范围上下文,我可能没有正确关闭连接.我会根据我的发现更新这个答案.

I'm not sure if I need a scoped context, and I may not be closing the connection properly. I'll update this answer based on what I find out.

这篇关于Wildfly 到 Wildfly EJB 客户端,无需远程出站连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆