3 Replies Latest reply on Apr 20, 2012 3:43 AM by ideaalloc

    Connecting to Embedded JMS from anther VM

    abdaraeh

      Can someone tell me how to connect to an embedded HQ JMS server from another Java VM

       

      The server code is

       

                  Configuration   clsConfig = new ConfigurationImpl();

       

                  clsConfig.setPersistenceEnabled(false);

                  clsConfig.setSecurityEnabled(false);

                  clsConfig.getAcceptorConfigurations().add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));

       

                  this.m_clsJmsServer = new EmbeddedJMS();

                  this.m_clsJmsServer.setConfiguration(clsConfig);

                  this.m_clsJmsServer.setJmsConfiguration(this.getJmsConfig());

                  this.m_clsJmsServer.start();

       

      How do we look this server from another VM. 

        • 1. Re: Connecting to Embedded JMS from anther VM
          clebert.suconic

          Look on the acceptors and connectors chapter.

           

          You just have to configure the acceptor and the connector with a proper IP, etc.. same way it happens on any HornetQ server. (It's no different here)

          • 2. Re: Connecting to Embedded JMS from anther VM
            abdaraeh

            I know how it is done using XML config files but not sure how do you configure this programatically.  Do you have sample code on how to set the connector/acceptor with the proper IP and port.

            • 3. Re: Connecting to Embedded JMS from anther VM

              You can also set connector/acceptor programatically. The example 'examples\core\embedded-remote' has every details on this.

              // Server

              try

                    {

                       // Step 2. Create the Configuration, and set the properties accordingly

                       Configuration configuration = new ConfigurationImpl();

                       configuration.setPersistenceEnabled(false);

                       configuration.setSecurityEnabled(false);

               

               

                       TransportConfiguration transpConf = new TransportConfiguration(NettyAcceptorFactory.class.getName());

               

               

                       HashSet<TransportConfiguration> setTransp = new HashSet<TransportConfiguration>();

                       setTransp.add(transpConf);

               

               

                       configuration.setAcceptorConfigurations(setTransp);

               

               

                       // Step 3. Create and start the server

                       HornetQServer server = HornetQServers.newHornetQServer(configuration);

                       server.start();

                       System.out.println("STARTED::");

                    }

                    catch (Exception e)

                    {

                       System.out.println("FAILED::");

                       e.printStackTrace();

                    }

               

              // client

                    try

                    {

                       // Step 1. As we are not using a JNDI environment we instantiate the objects directly

                       ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName()));

                       ClientSessionFactory sf = serverLocator.createSessionFactory();

               

               

                       // Step 2. Create a core queue

                       ClientSession coreSession = sf.createSession(false, false, false);

               

               

                       final String queueName = "queue.exampleQueue";

               

               

                       coreSession.createQueue(queueName, queueName, true);

               

               

                       coreSession.close();

               

               

                       ClientSession session = null;

               

               

                       try

                       {

               

               

                          // Step 3. Create the session, and producer

                          session = sf.createSession();

               

               

                          ClientProducer producer = session.createProducer(queueName);

               

               

                          // Step 4. Create and send a message

                          ClientMessage message = session.createMessage(false);

               

               

                          final String propName = "myprop";

               

               

                          message.putStringProperty(propName, "Hello sent at " + new Date());

               

               

                          System.out.println("Sending the message.");

               

               

                          producer.send(message);

               

               

                          // Step 5. Create the message consumer and start the connection

                          ClientConsumer messageConsumer = session.createConsumer(queueName);

                          session.start();

               

               

                          // Step 6. Receive the message.

                          ClientMessage messageReceived = messageConsumer.receive(1000);

                          System.out.println("Received TextMessage:" + messageReceived.getStringProperty(propName));

                       }

                       finally

                       {

                          // Step 7. Be sure to close our resources!

                          if (sf != null)

                          {

                             sf.close();

                          }

                       }

                    }

                    catch (Exception e)

                    {

                       e.printStackTrace();

                       System.exit(-1);

                    }