Wildfly to Wildfly EJB client without remote-outbound-connections

前端 未结 2 2010
情深已故
情深已故 2021-01-15 20:33

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 documentati

相关标签:
2条回答
  • 2021-01-15 20:51

    You don't have to use the remote-outbound-connection. You can just write the code as though you were any external client. See: https://docs.jboss.org/author/display/WFLY9/EJB+invocations+from+a+remote+client+using+JNDI.

    0 讨论(0)
  • 2021-01-15 20:53

    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.

    0 讨论(0)
提交回复
热议问题