2015-05-11 79 views
1

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

有没有办法让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客户端库)的工作相同的代码,我得到的错误EJBCLIENT000021: EJB client context selector may not be changed当我的代码试图做EJBClientContext.setSelector(selector)。我以编程方式设置远程连接主机和端口,而不是使用jboss-ejb-client.properties。

+0

当你在wildly而不是tomcat中使用相同的代码时会发生什么? –

+0

您是否知道您可以为您的Ops团队提供一个六行CLI脚本,它将为您设置远程出站连接?这有利于将远程主机配置与应用程序完全分开。只是别的可以考虑... –

+0

在蜻蜓工作的代码生成的“EJB客户端上下文选择器可能不会改变”在蜻蜓 –

回答

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

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

+0

那太棒了。在这种情况下,在EJB调用完成后需要关闭InitialContext?有没有例程设置主机名而不是使用jboss-ejb-client.properties的例子? –

+0

如果你谷歌周围有一些样本。不过,关于关闭上下文的问题是一个很好的问题。实际上有第二个上下文(在引擎盖下使用的ejb:上下文)。这里有一些很好的信息:https://docs.jboss.org/author/display/AS72/Scoped+EJB+client+contexts。 – jgitter

相关问题