2 Replies Latest reply on Nov 16, 2012 7:40 AM by Aleksey Ternovsky

    JMS between two JBoss AS instances

    Aleksey Ternovsky Newbie

      I have two JBoss AS 7 instances with differernt IP(X and Y).  X instance send queue message from Singleton EJB to Queue into Y instance. Queues have the same JNDI into instances. It's queue/test.

      Into X instance I create InitialContext for Y, lookup ConnectionFacory, Queue and send message. But message get X instance queue instead Y instance queue. Below X instance code:

       

      package com.kg.interaction;
      
      import javax.annotation.PostConstruct;
      import javax.ejb.Singleton;
      import javax.ejb.Startup;
      import javax.jms.*;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import java.util.Properties;
      
      
      /**
       * Created with IntelliJ IDEA.
       * User: ternovski
       * Date: 14.11.12
       * Time: 14:33
       */
      @Startup
      @Singleton
      public class TestSingleton {
      
          // Set up all the default values
          private static final String DEFAULT_MESSAGE = "Hello, World!!! =))))";
          private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
          private static final String DEFAULT_DESTINATION = "jms/queue/test";
          private static final String DEFAULT_MESSAGE_COUNT = "1";
          private static final String DEFAULT_USERNAME = "au";
          private static final String DEFAULT_PASSWORD = "ua";
          private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
          private static final String PROVIDER_URL = "remote://srv-oracle-06:4447";
      
      
          @PostConstruct
          public void postConstruct() throws Exception {
              ConnectionFactory connectionFactory = null;
              Connection connection = null;
              Session session = null;
              MessageProducer producer = null;
              MessageConsumer consumer = null;
              Destination destination = null;
              TextMessage message = null;
              Context context = null;
      
              try {
                  final Properties env = new Properties();
                  env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
                  env.put(Context.PROVIDER_URL, PROVIDER_URL);
                  env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
                  env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);
                  context = new InitialContext(env);
      
                  String connectionFactoryString = DEFAULT_CONNECTION_FACTORY;
                  connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
      
                  String destinationString = DEFAULT_DESTINATION;
                  destination = (Destination) context.lookup(destinationString);
      
                  connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
                  session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                  producer = session.createProducer(destination);
                  consumer = session.createConsumer(destination);
                  connection.start();
      
                  int count = Integer.parseInt(DEFAULT_MESSAGE_COUNT);
                  String content = DEFAULT_MESSAGE;
      
                  for (int i = 0; i < count; i++) {
                      message = session.createTextMessage(content);
                      producer.send(message);
                  }
              } catch (Exception e) {
                  throw e;
              } finally {
                  if (context != null) {
                      context.close();
                  }
                  if (connection != null) {
                      connection.close();
                  }
              }
          }
      }