activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kureckam <mkure...@fractech.net>
Subject JMS Communications
Date Fri, 22 Jul 2011 22:19:51 GMT
Below is a sample program on Tomcat that uses ActiveMQ JMS example code.  The
code runs and per my messages sends a message and is listening, but doesn't
receive the message. I would appreciate if someone could review and let me
know what I've done wrong in or outside of this code or if I need to add
something additionally.

*web.xml*
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID"
         version="3.0">
         
   <listener>
     
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <listener>
      <listener-class>JMSReceiveListener</listener-class>
   </listener>
   <listener>
      <listener-class>JMSSendListener</listener-class>
   </listener>
</web-app>

*applicationContext.xml*
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:amq="http://activemq.apache.org/schema/core"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://activemq.apache.org/schema/core
                          
http://activemq.apache.org/schema/core/activemq-core-5.3.0.xsd
                           http://www.springframework.org/schema/beans
                          
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                          
http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/jee
                          
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                           http://www.springframework.org/schema/jms
                          
http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
                           http://www.springframework.org/schema/tx
                          
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >    
   
      
      <context:component-scan base-package=""/>
      <context:annotation-config/>
   
   
   <amq:broker useJmx="false"
               persistent="false">
      <amq:transportConnectors>
         <amq:transportConnector uri="tcp://localhost:0" />
      </amq:transportConnectors>
   </amq:broker>
   
   
   <amq:topic id="startTopic"
              physicalName="START" />
              
   
   <amq:connectionFactory id="jmsFactory"    
                          brokerURL="vm://localhost" />
                          
   
   <bean id="producerJMSTemplate"
         class="org.springframework.jms.core.JmsTemplate">
      <property name="connectionFactory">
         
         <bean
class="org.springframework.jms.connection.SingleConnectionFactory">
            <property name="targetConnectionFactory">
               <ref local="jmsFactory" />
            </property>
         </bean>
      </property>
   </bean>

   <bean id="consumerJMSTemplate"
         class="org.springframework.jms.core.JmsTemplate">
      <property name="connectionFactory" ref="jmsFactory" />
   </bean>
   
   
   <bean id="producer"
         class="TestSender">
      <property name="jMSTemplate">
         <ref bean="producerJMSTemplate"></ref>
      </property>
      <property name="topic">
         <ref bean="startTopic" />
      </property>
   </bean>
   
   
   <bean id="consumer"
         class="TestReceiver">
      <property name="jMSTemplate" ref="consumerJMSTemplate" />
      <property name="topic" ref="startTopic" />
   </bean>
</beans>

*TestSender.java*
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

@Component
public class TestSender
{
   @Autowired(required=true)
   @Qualifier("producerJMSTemplate")
   private JmsTemplate jmsTemplate;
   
   @Autowired(required=true)
   @Qualifier("startTopic")
   private Topic       topic;

   public void produce(final String responseMsg_)
   {
      try
      {
         System.out.println("TestSender:sending message on topic ==> " +
topic.getTopicName());
      }
      catch(JMSException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
      this.jmsTemplate.send(this.topic,
                            new MessageCreator()
         {
            public Message createMessage(Session session_) throws
JMSException
            {
               ObjectMessage mm = session_.createObjectMessage();
               
               mm.setIntProperty("JobId", 12);
               mm.setStringProperty("Response", responseMsg_);
               
               System.out.println("TestSender:message created ==> " + mm);
               return(mm);
            }
         });
   }

   public void setJMSTemplate(JmsTemplate jmsTemplate_)
   {
      System.out.println("TestSender:Connection factory set");
      this.jmsTemplate = jmsTemplate_;
   }
   
   public void setTopic(Topic topic_)
   {
      System.out.println("TestSender:Topic set");
      this.topic = topic_;
   }
}

*TestReceiver.java*
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

@Component
public class TestReceiver implements MessageListener
{
   @Autowired(required=true)
   @Qualifier("consumerJMSTemplate")
   private JmsTemplate jmsTemplate;
   
   @Autowired(required=true)
   @Qualifier("startTopic")
   private Topic       topic;

   public void setJMSTemplate(JmsTemplate jmsTemplate_)
   {
      System.out.println("TestReceiver:Connection factory set");
      this.jmsTemplate = jmsTemplate_;
   }
   
   public void setTopic(Topic topic_)
   {
      System.out.println("TestReceiver:Topic set");
      this.topic = topic_;
   }

   public void onMessage(Message message_)
   {
      ObjectMessage oMessage = (ObjectMessage)message_;
      
      try
      {
         System.out.println("StartTopicHandler received message=" +
oMessage.getObject().toString());
      }
      catch(JMSException e)
      {
         e.printStackTrace();
      }
   }
}

*JMSSendListener.java*
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class JMSSendListener implements ServletContextListener, Runnable
{
   public void contextDestroyed(ServletContextEvent arg0)
   {
   }

   public void contextInitialized(ServletContextEvent event_)
   {
      new Thread(this).start();
   }

   public void run()
   {
      BeanFactory beanFactory = new
FileSystemXmlApplicationContext("/WEB-INF/applicationContext.xml").getBeanFactory();
      
      TestSender testSender = (TestSender)beanFactory.getBean("producer");
      
      System.out.println("Got bean, testSender=" + testSender + " is
sending");

      while(true)
      {
         testSender.produce("This is the response message from the sender");

         try
         {
            Thread.sleep(1000);
         }
         catch(InterruptedException e)
         {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
   }
}

*JMSReceiveListener.java*
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class FDSListener implements ServletContextListener, Runnable
{
   private TestReceiver testReceiver;

   public void contextDestroyed(ServletContextEvent arg0)
   {
   }

   public void contextInitialized(ServletContextEvent event_)
   {
      new Thread(this).start();
   }

   public void run()
   {
      BeanFactory beanFactory = new
FileSystemXmlApplicationContext("/WEB-INF/applicationContext.xml").getBeanFactory();
      
      testReceiver = (TestReceiver)beanFactory.getBean("consumer");
      
      System.out.println("Got bean, testReceiver=" + testReceiver+ " is
listening");
      
      while(true)
      {
         try
         {
            Thread.sleep(500);
         }
         catch(Exception e)
         {
            e.printStackTrace();
         }
      }
   }
}

--
View this message in context: http://activemq.2283324.n4.nabble.com/JMS-Communications-tp3688040p3688040.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Mime
View raw message