4 Replies Latest reply on May 8, 2009 3:42 AM by Tim Fox

    How to send message to a remote queue

    Wayland Chan Newbie

      @Stateless
      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public class JMSSender {
      @Resource(mappedName="/queue/myQueue")
      private Queue queue;

      @Resource(mappedName="QueueConnectionFactory")
      private QueueConnectionFactory conFac;

      public void send(final String... msgsTxt) {

      Connection connection = null;
      Session session = null;
      try {
      connection = this.conFac.createConnection();
      session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); // tx will be commited by container...
      for (String txt: msgsTxt) {
      if (txt != null) {
      TextMessage msg = session.createTextMessage();
      msg.setText(txt);
      MessageProducer producer = session.createProducer(queue);
      producer.send(msg);
      }
      }
      }
      catch (JMSException ex) {
      throw new RuntimeException(ex);
      }finally{
      if(session != null){
      try{
      session.close();
      }catch(Exception ignored){}
      }
      if(connection != null){
      try{
      connection.close();
      }catch(Exception ignored){}
      }
      }
      }
      }

      The code above has no problem to send message to a local queue.
      How can I modify it or configure to make it to send to a remote queue?

      change the annotation
      @Resource(mappedName="/queue/myQueue") to
      @Resource(mappedName="jnp://remotehost:1099/queue/myQueue")
      seems not work.

      If I don't inject the connectionfactory and queue but using remote jndi lookup, it is okay to send the messages but session.commit() needed to be called after sending. (otherwise, the message is not sent)
      That's not what I want as it should be container managed.
      How to send the message to remote queue in the container managed transaction? Thanks.

        • 1. Re: How to send message to a remote queue
          Wayland Chan Newbie

          finally solved.
          Forgot to add remote ip address to the connection factory.
          e.g. @Resource(mappedName="jnp://remotehost:1099/ConnectionFactory")

          • 2. Re: How to send message to a remote queue
            vandana awat Newbie

            hi

            could you please tell how's your jms-ds.xml configured to interact with the remote queues

            i have tried the approach of

            http://www.jboss.org/community/wiki/HowDoIConfigureTheJMSResourceAdapterToUseARemoteConnectionFactory;jsessionid=52AB6F6F37C5425035FA9B75AA8881CE

            but can't get it to work
            i'm not using EJB i want to use it in a web app...
            your help would be greatly appreciated ..

            • 3. Re: How to send message to a remote queue
              Clebert Suconic Master

              That's pretty much the procedure.

              - you define a JMSPRoviderLoader. Give it a name

               <mbean code="org.jboss.jms.jndi.JMSProviderLoader"
               name="jboss.mq:service=JMSProviderLoader,name=JMSProvider">
               <attribute name="ProviderName">MyProvider</attribute>
               <attribute name="ProviderAdapterClass">
               org.jboss.jms.jndi.JNDIProviderAdapter
               </attribute>
               <!-- The queue connection factory -->
               <attribute name="QueueFactoryRef">java:/XAConnectionFactory</attribute>
               <!-- The topic factory -->
               <attribute name="TopicFactoryRef">java:/XAConnectionFactory</attribute>
               <!-- Uncomment to use HAJNDI to access JMS
               <attribute name="Properties">
               java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
               java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
               java.naming.provider.url=localhost:1100
               </attribute>
               -->
              
               </mbean>


              - you define the tx-connection-factory. make a reference to the provider loader using the property JmsProviderAdapterJNDI. Also define a JNDI name for the tx-connection-factory

              <connection-factories>
               <tx-connection-factory>
               <jndi-name>MyConnectionFactory</jndi-name>
               <xa-transaction/>
               <rar-name>jms-ra.rar</rar-name>
               <connection-definition>org.jboss.resource.adapter.jms.JmsConnectionFactory</connection-definition>
               <adapter-display-name>JMS Adapter</adapter-display-name>
               <config-property name="SessionDefaultType" type="java.lang.String">javax.jms.Topic</config-property>
               <config-property name="JmsProviderAdapterJNDI" type="java.lang.String">java:/MyProvider</config-property>
               <security-domain-and-application>WhateverJmsXARealm</security-domain-and-application>
               </tx-connection-factory>
              </connection-factories>
              




              - In your code, use refer to JNDI name from tx-connection-factory:

              javax.jms.ConnectionFactory factory = (javax.jms.ConnectionFactory)jndi.lookup("java:/MyConnectionFactory");
              



              But all this is already defined on that Wiki. So I'm already providing you extra information.


              • 4. Re: How to send message to a remote queue
                Tim Fox Master

                Also this is a question on JCA configuration, not JBoss Messaging config.

                None of the above config is JBM config.

                Please ask in the JCA user forum.