Spring+jms+activemq+tomcat Implementing a messaging service

Source: Internet
Author: User
Tags message queue xmlns tomcat
It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.


Based on Spring+jms+activemq+tomcat, the version I'm using is as follows: Spring 2.5 ActiveMQ 5.4.0 Tomcat 6.0.30

The following learning and configuration, to achieve the basic message service functions: Send and receive. Spring provides very good support for JMS, which can be easily implemented with Jmstemplate for messaging services. Here, our messaging service does not involve transaction management. The following is a brief description of the implementation process:

First look at the contents of our final spring configuration file Applicationcontext.xml, as follows:<?xmlVersion= "1.0" encoding= "UTF-8"?><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns:amq= "Http://activemq.apache.org/schema/core" xmlns:xsi = "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd Http://activemq.apache.org/schema/core http://a Ctivemq.apache.org/schema/core/activemq-core.xsd "><beanId= "Listenercontainer" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer"> <propertyName= "ConnectionFactory" ref= "ConnectionFactory"></property><propertyName= "Destination" ref= "MessageQueue"></property><propertyName= "MessageListener" ref= "Receivemessagelistener"></property></bean><beanId= "ConnectionFactory" class= "Org.springframework.jndi.JndiObjectFactoryBean"> <propertyName= "Jndiname" value= "Java:comp/env/myjms/connectionfactory"></property></bean><beanId= "MessageQueue" class= "Org.springframework.jndi.JndiObjectFactoryBean"><propertyName= "Jndiname" value= "Java:comp/env/myjms/messagequeue"></property></bean><beanId= "Receivemessagelistener" class= "Org.shirdrn.spring.jms.integration.ReceiveMessageListener"></bean><beanId= "Messagesender" class= "Org.shirdrn.spring.jms.integration.MessageSender"><propertyName= "Jmstemplate" ref= "Jmstemplate"></property></bean><beanId= "Jmstemplate" class= "Org.springframework.jms.core.JmsTemplate"> <propertyName= "ConnectionFactory" ref= "ConnectionFactory"></property><propertyName= "Defaultdestination" ref= "MessageQueue"></property></bean><beanId= "Sendmessagecontroller" class= "Org.shirdrn.spring.jms.integration.SendMessageController"><propertyName= "Messagesender" ref= "Messagesender"/><propertyName= "Successview" value= "/success"/></bean><beanId= "urlmapping" class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><propertyName= "Mappings"> <props><propkey= "/sendmessage.do">Sendmessagecontroller</prop></props></property></bean><beanId= "Viewresolver" class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyName= "Requestcontextattribute" value= "RC"/><propertyName= "Viewclass" value= "Org.springframework.web.servlet.view.JstlView"/><propertyName= "prefix" value= "/"/><propertyName= "suffix" value= ". JSP"/> </bean></beans>

We use spring's org.springframework.jms.listener.DefaultMessageListenerContainer to gather messages by setting up a message listener, specifically implementing the class as ORG.SHIRDRN.SPRING.JMS . Integration. Receivemessagelistener, the code is as follows: PackageOrg.shirdrn.spring.jms.integration;ImportJavax.jms.JMSException;ImportJavax.jms.Message;ImportJavax.jms.MessageListener;ImportJavax.jms.TextMessage;ImportOrg.apache.log4j.Logger; PublicclassReceivemessagelistenerImplementsMessageListener {PrivateStaticFinalLogger LOG = Logger.getlogger (Receivemessagelistener.class); PublicvoidOnMessage (Message message) {if(MessageinstanceofTextMessage) {TextMessage text = (textmessage) message;Try{Log.info ("Received message:" + text.gettext ()); }Catch(JMSException e)               {E.printstacktrace (); }           }       }      }

Above, listen to the message sent, and receive processing, we simply print out a log content.

For Listenercontainer, Also need to inject connection factory connectionfactory and message purpose destination These two properties: connectionfactory we use ACTIVEMQ org.apache.activemq.ActiveMQConnectionFacto Ry, and through the Jndi service, bind to the name java:comp/env/myjms/connectionfactory on While the destination property is bound to the name through the Jndi service by using ACTIVEMQ's Org.apache.activemq.command.ActiveMQQueue message queue java:comp/env/myjms/ On the MessageQueue. So, in the <Context> element in Tomcat's Conf/context.xml, add the following configuration: <resource  name= "myjms/ ConnectionFactory "        auth=" Container "          type= "Org.apache.activemq.ActiveMQConnectionFactory"          description= "Jms connection factory"        factory= " Org.apache.activemq.jndi.JNDIReferenceFactory "        brokerurl=

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.