0 Replies Latest reply on Dec 15, 2016 8:32 AM by luiz_gustavo

    Remote JMS Connection from Producer and Consumer in JBoss 6

    luiz_gustavo

      Hi!

       

      I Need help with this scenario: I have 2 JBoss 6 servers, in different locations. In one of them I have a "client" application, and in the other I have a "server" application.

      The JBoss 6 instance where the server application is deployed in has some queues and topics that are used for communication between server and client applications.

       

      The problem is that from the client application I can not send messages to the remote queues on the JBoss instante of the server application.

      I create an InitialContext passing the remote server's IP and port. ConnectionFactory, Connection, Session and Queue are created but when I try to create the Sender, I get this Exception: javax.jms.InvalidDestinationException: Destination [queue name] does not exist.

       

      Here's the class:

       

       

      package acme.test.messaging;
      
      import javax.jms.DeliveryMode;
      import javax.jms.JMSException;
      import javax.jms.Queue;
      import javax.jms.QueueConnection;
      import javax.jms.QueueConnectionFactory;
      import javax.jms.QueueSender;
      import javax.jms.QueueSession;
      import javax.jms.Session;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      
      import org.apache.log4j.Logger;
      
      public class ClientToServerMessageSender {
      
        private static Logger logger;
      
          static {
              try {
                  logger = Logger.getLogger(ClientToServerMessageSender.class.getName());
              } catch (Exception e) {
                  e.printStackTrace(System.err);
              }
          }
              
        private static final String filaCToS = "queue/CToSQueue";
        private static final String URL_PATTERN_JNP = "jnp://{0}:{1}"; 
        private static final String CONNECTION_FACTORY_NAME_JMSXA = "java:/JmsXA";
           
        public boolean enviarMensagem(String mensagem, String codigoMensagem) throws Exception{
      
        InitialContext ctx = null;
      
        Queue remoteQueue = null;
        QueueConnectionFactory remoteConnectionFactory = null;
        QueueConnection remoteConnection = null;
        Session remoteSession = null;
        QueueSession remoteQueueSession = null;
        QueueSender remoteSender = null;
      
        String host = System.getProperty("jms.servers.ips"); // config with remote host
        String port = System.getProperty("jms.servers.port"); // config with remote port
      
        StringBuilder urlCompleta = new StringBuilder();
          urlCompleta.append(MessageFormat.format(URL_PATTERN_JNP, new Object[] { host, port }));
      
        try {
      
            Hashtable env = new Hashtable();
      
            env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
            env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
            env.put("java.naming.provider.url", urlCompleta.toString());
      
            ctx = new InitialContext(env);
      
            // goes fine
            remoteConnectionFactory = (QueueConnectionFactory) ctx.lookup(JMSUtils.obterFabricaConexaoJMS());
      
            // goes fine
            remoteQueue = (Queue) ctx.lookup(filaCToS);
      
            // goes fine
            remoteConnection = (QueueConnection) remoteConnectionFactory.createQueueConnection();
      
            // goes fine
            remoteSession = remoteConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      
            // goes fine
            remoteQueueSession = (QueueSession) remoteSession;
      
            // CRASH!
            remoteSender = remoteQueueSession.createSender(remoteQueue);
      
            javax.jms.TextMessage textMessage = remoteSession.createTextMessage();
            textMessage.setText(mensagem);
            textMessage.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
            textMessage.setJMSExpiration(0);
            remoteSender.send(textMessage);
      
           return true;
      
        } catch (Exception e) {
            throw new BusinessException("ClientToServerMessageSender - Fail!", e);
        } finally {
            // close message resources...
        }
      
        return false;
        }
      }
      

       

       

      This is the xml file with the destination's configurations in the JBoss instance with the server application:

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <configuration xmlns="urn:hornetq"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd">
      
          <queue name="CToSQueue">
              <entry name="/queue/CToSQueue"/>
          </queue>
      
        <topic name="SToCTopic">
              <entry name="/topic/SToCTopic"/>
          </topic>
      
      </configuration>
      

       

       

      So I need two things:

       

      Send messages to remote destinations and receive messages from the remote destinations too (this last I didn't try yet).

       

       

       

      Best regards!