1 2 Previous Next 23 Replies Latest reply on Jul 13, 2012 5:24 AM by jaikiran

    LookUp remote EJB from local EJB

    fargus

      Hello

       

      Please help me with my problem.

       

      I have many servers and one main server that should manage others. All servers is JBoss AS 7.1.1Final and deployed same ear file.

      Main server can administrate remote servers and should use ejb of these servers.

      Inside of ebj on main server i use next code for lookup remote ejb:

       

      final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
      
      
        String PROVIDER_URL = "remote://" + host + ":4447";
        Properties connectProperties = new Properties();
        connectProperties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
        connectProperties.put(Context.PROVIDER_URL, PROVIDER_URL);
        connectProperties.put("jboss.naming.client.ejb.context", "true");
        connectProperties.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");
      
      
        connectProperties.put(Context.SECURITY_PRINCIPAL, "admin");
        connectProperties.put(Context.SECURITY_CREDENTIALS, "admin");
      
      
        Environment env = null;
      
      
        try {
        InitialContext initialContext = new InitialContext(connectProperties);
        env = (Environment) initialContext.lookup("es-ear/es-core/Environment!com.service.Environment");
      
        } catch (Exception e) {
        e.printStackTrace();
        }
        return env;
      
      

       

      but getting next error:

       

      17:26:03,782 ERROR [stderr] (DefaultQuartzScheduler_Worker-1) javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory org.jboss.nami

      g.remote.client.InitialContextFactory from classloader ModuleClassLoader for Module "deployment.es-ear.ear.es-core-0.0.1-SNAPSHOT.jar:main" from Service Module

      Loader♪

      17:26:03,782 ERROR [stderr] (DefaultQuartzScheduler_Worker-1)   at org.jboss.as.naming.InitialContextFactoryBuilder.createInitialContextFactory(InitialContextF

      ctoryBuilder.java:64)♪

      17:26:03,782 ERROR [stderr] (DefaultQuartzScheduler_Worker-1)   at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:681)♪

      17:26:03,798 ERROR [stderr] (DefaultQuartzScheduler_Worker-1)   at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)♪

      17:26:03,798 ERROR [stderr] (DefaultQuartzScheduler_Worker-1)   at javax.naming.InitialContext.init(InitialContext.java:242)♪

      17:26:03,798 ERROR [stderr] (DefaultQuartzScheduler_Worker-1)   at javax.naming.InitialContext.<init>(InitialContext.java:216)♪

       

       

      If I use "org.jboss.as.naming.InitialContextFactory" as context factory I dont get any errors but any lookup returns instance from local server, not from remote.

       

      How I can invoke remote ejb methods inside local ejb, if i dynamically add remote servers and cant edit config files?

        • 1. Re: LookUp remote EJB from local EJB
          wdfink

          I recommend to not use the remote-naming project (as you do).

           

          The recommended way is described here.

          I've written a quickstart for this, see github. Follow the README.

          As you will see you don't need to change the config files, you can use the management API to do this.

          • 2. Re: LookUp remote EJB from local EJB
            fargus

            As I understand, I need restart server each time when I add new remote connection? Also how dynamically edit jboss-ejb-client.xml in main ear?

            • 3. Re: LookUp remote EJB from local EJB
              wdfink

              The application use a defined connection for i.e. one (or more) remote ejb, so you don't need to change the jboss-ejb-client.xml

               

              If you ant to change the server or add a new connection for this ejb you need only to edit the socket-binding-group and add a remote-destination.

              I'm not sure ATM but it might be that you have to restart the related server (can also be done in a domain for specific servers via mngmt)

              • 4. Re: LookUp remote EJB from local EJB
                fargus

                Follow your example and guide, I config servers and now try to use this code

                 

                final Hashtable<String, String> p = new Hashtable<String, String>();

                  p.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

                                    Environment env = null;

                 

                                    try {

                                              InitialContext initialContext = new InitialContext(p);

                                              env = (Environment) initialContext.lookup("ejb:es-ear/es-core/Environment!com.service.Environment");

                 

                                    } catch (Exception e) {

                                              e.printStackTrace();

                                    }

                                    return env;

                 

                but I getting local instance of Environment ejb.

                I try to use p.put(Context.PROVIDER_URL, PROVIDER_URL); but tis not help.

                 

                In your example you use different ejb on different severs. And I have Environment ejb on all servers(main and remotes), and when I want to lookup remote Environment in main server I have got Environment from main server, not from remote.

                • 5. Re: LookUp remote EJB from local EJB
                  wdfink

                  Now I got you.

                   

                  The Context.PROVIDER_URL did not help in that case because it is ignored.

                   

                  The other thing is that nevertheless whether you use remote-naming or the ejb-client the technique behind is always the same.

                  In case of having the same EJB deployed local and remote the call will prefere local every time because the benefit of LB will be less and a remote call produce overhead.

                  The assumption in this case is that the EJBs are clustered/loadbalanced (from outside).

                   

                  But nevertheless you might use 'distinct-names'.

                  Look for the ejb3 subsystem in your configuration profile and add <default-distinct-name>XYZ</default-distinct-name>

                  the lookup will be "ejb:es-ear/es-core/XYZ/Environment!com.service.Environment" in this case.

                  Remember that this ejb is no longer found with the 'normal' lookup.

                  1 of 1 people found this helpful
                  • 6. Re: LookUp remote EJB from local EJB
                    apakhunov

                    Wolf-Dieter,

                     

                    Let me provide more details on what Vadim was interested in:

                     

                    1. Servers aren't clustered.

                    2. The same EJBs on different servers provide different data.

                    3. Our goal is to create a web app on one of the servers that will present some high level data not only from local(server on that we deploy web app) but also from remote servers. So we want to call EJBs from certain remote servers from local EJBs/Servlets.

                    4. Number of remote servers can be variable and we want to configure list of servers dynamically in our web app.

                     

                    How can that be achieved?

                    Can we provide all connecton properties (host, login, password) programmatically?

                    Can we use "default-distinct-name" to accomplish this? Does JBoss 7.1.1 support "default-distinct-name"?  As I can see AS7-4386 "Add the ability to set a global default distinct name" in fixed in 7.1.2.

                     

                    Thanks,

                    Alex

                    • 7. Re: LookUp remote EJB from local EJB
                      wdfink

                      You might update the application to 7.1.2 or add <jboss:distinct-name>Security</jboss:distinct-name> to your application META-INF/jboss-ejb3.xml deployment descriptor.

                       

                      The remote server can be add to the configuration via CLI and your application can be use the distinct name dynamicaly.

                      • 8. Re: LookUp remote EJB from local EJB
                        fargus

                        Thanks Wolf-Dieter, this realy work.

                         

                        But I have another problem with it. If remote server is shutdown or unavailable remote connection terminate and if remote server back to work main server cant get access to it because remote connection terminated. Can jboss automaticly reconnect to remote servers?

                        • 9. Re: LookUp remote EJB from local EJB
                          jaikiran

                          Vadim Gusev wrote:

                           

                           

                           

                          But I have another problem with it. If remote server is shutdown or unavailable remote connection terminate and if remote server back to work main server cant get access to it because remote connection terminated. Can jboss automaticly reconnect to remote servers?

                          That was a bug https://issues.jboss.org/browse/AS7-4510 which was fixed after 7.1.1.Final and is available in latest nightly builds https://community.jboss.org/thread/167590

                          1 of 1 people found this helpful
                          • 10. Re: LookUp remote EJB from local EJB
                            fargus

                            Wolf-Dieter Fink wrote:

                             

                            The application use a defined connection for i.e. one (or more) remote ejb, so you don't need to change the jboss-ejb-client.xml

                             

                            If you ant to change the server or add a new connection for this ejb you need only to edit the socket-binding-group and add a remote-destination.

                            I'm not sure ATM but it might be that you have to restart the related server (can also be done in a domain for specific servers via mngmt)

                            And one more question about jboss-ejb-client.xml

                             

                            Now my jboss-ejb-client.xml contains this code:

                            <jboss-ejb-client xmlns="urn:jboss:ejb-client:1.0">

                                <client-context>

                                    <ejb-receivers>

                                        <remoting-ejb-receiver outbound-connection-ref="remote-ejb-connection"/>

                                    </ejb-receivers>

                                </client-context>

                            </jboss-ejb-client>

                             

                            and for example I want to use this receiver with to connections: to myserver1:4447 ant to myserver2:4447

                             

                            I dont undestand what I should add to standalone.xml of main server to get connection to both servers via one reciever? Or may be wrong configurate my jboss-ejb-client.xml?

                            • 11. Re: LookUp remote EJB from local EJB
                              fargus

                              I install last build from https://community.jboss.org/thread/167590 and I got next:

                              If I add <distinct-name>${jboss.server.name}</distinct-name> to jboss-app.xml in my ear I've got NPE:

                               

                              17:08:35,388 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-8) JBAS015876: Starting deployment of "es-ear.ear"

                              17:08:35,419 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC00001: Failed to start service jboss.deployment.unit."es-ear.ear".STRUCTURE: org.jboss.msc.service.StartException in service jboss.deployment.unit."es-ear.ear".STRUCTURE: JBAS018733: Failed to process phase STRUCTURE of deployment "es-ear.ear"

                                        at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:123) [jboss-as-server-7.2.0.Alpha1-SNAPSHOT.jar:7.2.0.Alpha1-SNAPSHOT]

                                        at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]

                                        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]

                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_04]

                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_04]

                                        at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_04]

                              Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011059: Failed to parse "/C:/jboss-as-7.2.0.Alpha1/standalone/deployments/es-ear.ear/META-INF/jboss-app.xml"

                                        at org.jboss.as.ee.structure.EarMetaDataParsingProcessor.handleJbossMetadata(EarMetaDataParsingProcessor.java:126)

                                        at org.jboss.as.ee.structure.EarMetaDataParsingProcessor.deploy(EarMetaDataParsingProcessor.java:67)

                                        at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:116) [jboss-as-server-7.2.0.Alpha1-SNAPSHOT.jar:7.2.0.Alpha1-SNAPSHOT]

                                        ... 5 more

                              Caused by: java.lang.NullPointerException

                                        at org.jboss.metadata.property.DefaultPropertyReplacer.replaceProperties(DefaultPropertyReplacer.java:110)

                                        at org.jboss.metadata.parser.util.MetaDataElementParser.getElementText(MetaDataElementParser.java:194)

                                        at org.jboss.metadata.parser.jboss.JBossAppMetaDataParser.parse(JBossAppMetaDataParser.java:103)

                                        at org.jboss.as.ee.structure.EarMetaDataParsingProcessor.handleJbossMetadata(EarMetaDataParsingProcessor.java:123)

                                        ... 7 more

                               

                               

                              17:08:35,419 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS015870: Deploy of deployment "es-ear.ear" was rolled back with failure message {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"es-ear.ear\".STRUCTURE" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"es-ear.ear\".STRUCTURE: JBAS018733: Failed to process phase STRUCTURE of deployment \"es-ear.ear\"

                                  Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011059: Failed to parse \"/C:/jboss-as-7.2.0.Alpha1/standalone/deployments/es-ear.ear/META-INF/jboss-app.xml\"

                                  Caused by: java.lang.NullPointerException"}}

                              17:08:35,419 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-5) JBAS015877: Stopped deployment es-ear.ear in 2ms

                              17:08:35,419 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"es-ear.ear\".STRUCTURE" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"es-ear.ear\".STRUCTURE: JBAS018733: Failed to process phase STRUCTURE of deployment \"es-ear.ear\"

                                  Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011059: Failed to parse \"/C:/jboss-as-7.2.0.Alpha1/standalone/deployments/es-ear.ear/META-INF/jboss-app.xml\"

                                  Caused by: java.lang.NullPointerException"}}}}

                               

                              In jboss 7.1.1 all work normaly.

                              • 12. Re: LookUp remote EJB from local EJB
                                wdfink

                                The jboss-app.xml should contain a distinctName, not the expression.

                                The expression can be used in the standalone.xml for the default-distinct-name

                                • 13. Re: LookUp remote EJB from local EJB
                                  fargus

                                  Thanks Wolf-Dieter one more time :)

                                   

                                  Can you answere on my previous post(number 10) and provide how can I set two remote connection for one receiver ?

                                  • 14. Re: LookUp remote EJB from local EJB
                                    wdfink

                                    No not at the moment, I expect (in the first moment) that this should be possible.

                                    One is you might use a clustered environment behind, in this case all members of a cluster can receive this invoccation (automatic).

                                     

                                    But from configuration I see no other posibility except to configure more than one remoting-ejb-receiver in the jboss-ejb-client.xml

                                    1 2 Previous Next