Activemq java web spring configuration, activemqspring

Source: Internet
Author: User

Activemq java web spring configuration, activemqspring

Properties file configuration
1 # activeMq address Port 2 jms_url = tcp: // 127.0.0.1: 616163 4 # queue name 5 jms_test_monitor_data_queue = test_queue

Spring xml configuration
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xmlns: amq = "http://activemq.apache.org/schema/core" 5 xmlns: jms = "http://www.springframework.org/schema/jms" 6 xsi: schemaLocation = "http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 8 http://www.springframework.org/schema/j MS 9 http://www.springframework.org/schema/jms/spring-jms-4.1.xsd10 http://activemq.apache.org/schema/core11 http://activemq.apache.org/schema/core/activemq-core-5.9.0.xsd12 "> 13 <bean class =" org. springframework. beans. factory. config. propertyplaceholderpolicer "> 14 <property name =" locations "> 15 <value> classpath: propertiesConfig/test. properties </value> 16 </property> 17 </bean> 18 19 <bean id = "amqCon NectionFactory "class =" org. apache. activemq. activeMQConnectionFactory "> 20 <constructor-arg index =" 0 "value =" $ {jms_url} "/> 21 <property name =" useAsyncSend "value =" true "/> 22 </bean> 23 24 <bean id = "connectionFactory" class = "org. springframework. jms. connection. cachingConnectionFactory "> 25 <constructor-arg ref =" amqConnectionFactory "/> 26 <property name =" sessionCacheSize "value =" 20 "/> 27 </bean> 28 29 <! -- Define the Queue type of JmsTemplate --> 30 <bean id = "jmsQueueTemplate" class = "org. springframework. jms. core. jmsTemplate "> 31 <constructor-arg ref =" connectionFactory "/> 32 <! -- Non-pub/sub Model (publish/subscribe ), that is, the queue mode --> 33 <property name = "pubSubDomain" value = "false"/> 34 </bean> 35 36 <! -- Define the Topic type of JmsTemplate --> 37 <bean id = "jmsTopicTemplate" class = "org. springframework. jms. core. jmsTemplate "> 38 <constructor-arg ref =" connectionFactory "/> 39 <! -- Pub/sub Model (publish/subscribe) --> 40 <property name = "pubSubDomain" value = "true"/> 41 </bean> 42 43 <! -- Message consumer start --> 44 45 <! -- Define the Queue listener --> 46 <! -- <Bean id = "testMessageReceiver" class = "com. maven. project. web. jmsmessageworkflow. testMonitorQueue "/> 47 <jms: listener-container destination-type = "queue" container-type = "default" connection-factory = "connectionFactory" acknowledge = "auto"> 48 <jms: listener destination = "$ {jms_test_monitor_data_queue}" ref = "testMessageReceiver"/> 49 </jms: listener-container> --> 50 51 <! -- Define the Topic listener --> 52 <! -- <Jms: listener-container destination-type = "topic" container-type = "default" connection-factory = "connectionFactory" acknowledge = "auto"> 53 <jms: listener destination = "test. topic "ref =" topicReceiver1 "/> 54 <jms: listener destination =" test. topic "ref =" topicReceiver2 "/> 55 </jms: listener-container> --> 56 57 <! -- Define activme queue listening --> 58 <! -- Corresponding listening class, com. maven. project. web. jmsmessageworkflow. testMonitorQueue --> 60 <bean id = "testMessageReceiver" class = "com. maven. project. web. jmsmessageworkflow. testMonitorQueue "/> 61 <bean class =" org. springframework. jms. listener. defaultMessageListenerContainer "> 62 <property name =" connectionFactory "ref =" connectionFactory "/> 63 <property name =" destinationName "value =" $ {jms_test_monitor_data_queue} "/> 64 <property n Ame = "messageListener" ref = "testMessageReceiver"/> 65 </bean> 66 67 <! -- Message consumer end --> 68 </beans>

 

Activemq listener class

 1 package com.maven.project.web.jmsMessageOper; 2  3 import javax.jms.JMSException; 4 import javax.jms.Message; 5 import javax.jms.MessageListener; 6 import javax.jms.TextMessage; 7  8 public class TestMonitorQueue implements MessageListener { 9 10     public void onMessage(Message message) {11         if (message instanceof TextMessage) {12             TextMessage textMessage = (TextMessage) message;13             try {14                 System.out.println("============"+textMessage.getText());15             } catch (JMSException e) {16                 e.printStackTrace();17             }18         }19     }20 }

 

Activemq message sending class

 1 package com.maven.project.web.jmsMessageOper; 2  3 import java.io.Serializable; 4 import java.util.Map; 5  6 import javax.jms.BytesMessage; 7 import javax.jms.JMSException; 8 import javax.jms.MapMessage; 9 import javax.jms.Message;10 import javax.jms.Session;11 import javax.jms.StreamMessage;12 13 import org.springframework.beans.factory.annotation.Autowired;14 import org.springframework.jms.core.JmsTemplate;15 import org.springframework.jms.core.MessageCreator;16 import org.springframework.stereotype.Controller;17 18 @Controller19 public class MessageSender {20 21     @Autowired22     private JmsTemplate jmsQueueTemplate;23 24     public void sendTextMessage(final String queueName, final String txtMessage) {25         jmsQueueTemplate.send(queueName, new MessageCreator() {26             public Message createMessage(Session session) throws JMSException {27                 return session.createTextMessage(txtMessage);28             }29         });30     }31 32     public void sendObjectMessage(final String queueName, final Object objectMessage) {33         jmsQueueTemplate.send(queueName, new MessageCreator() {34             public Message createMessage(Session session) throws JMSException {35                 return session.createObjectMessage((Serializable) objectMessage);36             }37         });38     }39 40     public void sendMapMessage(final String queueName, final Map<String, Object> mapMessage) {41         jmsQueueTemplate.send(queueName, new MessageCreator() {42             @SuppressWarnings("unchecked")43             public Message createMessage(Session session) throws JMSException {44                 MapMessage mapMessage = session.createMapMessage();45                 for (Map.Entry<String, Object> entry : ((Map<String, Object>) mapMessage).entrySet()) {46                     mapMessage.setObject(entry.getKey(), entry.getValue());47                 }48                 return mapMessage;49             }50         });51     }52 53     public void sendByteMessage(final String queueName, final byte[] message) {54         jmsQueueTemplate.send(queueName, new MessageCreator() {55             public Message createMessage(Session session) throws JMSException {56                 BytesMessage bytesMessage = session.createBytesMessage();57                 bytesMessage.writeBytes(message);58                 return bytesMessage;59             }60         });61     }62 63     public void sendStreamMessage(final String queueName, final Object message) {64         jmsQueueTemplate.send(queueName, new MessageCreator() {65             public Message createMessage(Session session) throws JMSException {66                 StreamMessage streamMessage = session.createStreamMessage();67                 streamMessage.writeObject(message);68                 return streamMessage;69             }70         });71     }72 }

 

Message sending and calling

1 package com. maven. project. web. action; 2 3 import javax. servlet. http. httpServletRequest; 4 import javax. servlet. http. httpServletResponse; 5 6 import org. springframework. beans. factory. annotation. autowired; 7 import org. springframework. stereotype. controller; 8 import org. springframework. web. bind. annotation. requestMapping; 9 13 import com. maven. project. tools. utils. senderMessageQueueName; 14 import com. maven. project. web. jmsmessageworkflow. messageSender; 15 16 @ Controller17 @ RequestMapping ("/user") 18 public class UserLoginAction {19 20 @ Autowired21 private MessageSender messageSender; 22 32 @ RequestMapping ("/login ") 33 public void login (HttpServletRequest request, HttpServletResponse response) {35 messageSender. sendTextMessage ("test_queue", "message sending content"); // test_queue Message Queue name 37} 38}

 

Maven pom Configuration

1 <! -- Activemq dependencies --> 2 <dependency> 3 <groupId> org. apache. activemq </groupId> 4 <artifactId> activemq-core </artifactId> 5 <version> 5.7.0 </version> 6 </dependency>

 

Activemq server download

 

Related Article

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.