項目結構截圖java
maven所需依賴web
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gxf</groupId> <artifactId>springmq</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmq Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 版本管理 --> <properties> <springframework>4.1.8.RELEASE</springframework> <javax.servlet>3.1.0</javax.servlet> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet}</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${springframework}</version> </dependency> <!-- xbean 如<amq:connectionFactory /> --> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.16</version> </dependency> <!-- activemq --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.7.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.14.3</version> </dependency> </dependencies> <build> <finalName>springmq</finalName> </build> </project>
-activemq配置文件:activemq.xmlspring
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.14.3.xsd" > <context:component-scan base-package="com.gxf" /> <mvc:annotation-driven /> <amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://192.168.0.112:61616" userName="admin" password="admin"></amq:connectionFactory> <!-- 配置JMS鏈接工長 --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <constructor-arg ref="amqConnectionFactory" /> <property name="sessionCacheSize" value="100" /> </bean> <!-- 定義消息隊列(Queue) --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <!-- 配置兩個消息隊列:queue1,queue2 --> <constructor-arg index="0" value="queue1,queue2" /> </bean> <!-- 配置JMS模板(Queue),Spring提供的JMS工具類,它發送、接收消息。 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="queueDestination" /> <property name="receiveTimeout" value="10000" /> <!-- true是topic,false是queue,默認是false,此處顯示寫出false --> <property name="pubSubDomain" value="false" /> </bean> <!-- 配置消息隊列監聽者(Queue) --> <bean id="queueMessageListener" class="com.gxf.listener.QueueMessageListener" /> <bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="queueMessageListener" /> </bean> </beans>
-springmvc配置文件:springmvc.xmlapache
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <context:component-scan base-package="com.gxf" /> <mvc:annotation-driven /> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
-Controll層 MainHandler.java代碼:api
package com.gxf.handler; import java.text.SimpleDateFormat; import java.util.*; import javax.annotation.Resource; import javax.jms.Destination; import org.apache.activemq.command.ActiveMQDestination; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.gxf.service.ProducerService; /** * * @author stark2017 * */ @Controller public class MainHandler { //隊列名 @Resource(name="queueDestination") private Destination queueDestination; //隊列消息生產者 @Resource(name="producerService") private ProducerService producerService; @RequestMapping(value="/main",method=RequestMethod.GET) public String producer(){ return "main"; } /** * 往隊列queue1中發送消息 * @param message * @return */ @RequestMapping(value="/sendone",method=RequestMethod.POST) public String producer(@RequestParam("message") String message) { /** * 將destination強制轉換爲ActiveMQDestination,在ActiveMQDestination對象中, * 經過getCompositeDestinations()方法獲取destination隊列數組:queue://queue1 queue://queue2 * */ ActiveMQDestination activeMQDestination=(ActiveMQDestination) queueDestination; /** * 往隊列queue1中發送文本消息 */ System.out.println("往隊列"+activeMQDestination.getCompositeDestinations()[0].getPhysicalName()+"中發送文本消息"); producerService.sendTxtMessage(activeMQDestination.getCompositeDestinations()[0], message); /** * 往隊列queue1中發送MapMessage消息 */ System.out.println("往隊列"+activeMQDestination.getCompositeDestinations()[0].getPhysicalName()+"中發送MapMessage消息"); producerService.sendMapMessage(activeMQDestination.getCompositeDestinations()[0], message); //String bb="fdsalfkasdfkljasd;flkajsfd"; //byte[] b = bb.getBytes(); // producer.sendBytesMessage(demoQueueDestination, b); //producer.sendMapMessage(mqQueueDestination, message); return "main"; } /** * 往消息隊列queue2中發送消息 * @param message * @return */ @RequestMapping(value="/sendtwo",method=RequestMethod.POST) public String producertwo(@RequestParam("message") String message) { /** * 將destination強制轉換爲ActiveMQDestination,在ActiveMQDestination對象中, * 經過getCompositeDestinations()方法獲取destination隊列數組:queue://queue1 queue://queue2 * */ ActiveMQDestination activeMQDestination=(ActiveMQDestination) queueDestination; /** * 隊列queue2中發送文本消息 */ System.out.println("往隊列"+activeMQDestination.getCompositeDestinations()[1].getPhysicalName()+"中發送文本消息"); producerService.sendTxtMessage(activeMQDestination.getCompositeDestinations()[1], message); /** * 隊列queue2中發送mapMessage消息 */ System.out.println("往隊列"+activeMQDestination.getCompositeDestinations()[1].getPhysicalName()+"中發送文本消息"); producerService.sendMapMessage(activeMQDestination.getCompositeDestinations()[1], message); String bb="fdsalfkasdfkljasd;flkajsfd"; byte[] b = bb.getBytes(); // producer.sendBytesMessage(demoQueueDestination, b); //producer.sendMapMessage(mqQueueDestination, message); return "main"; } }
-生產者ProducerService.java代碼:數組
package com.gxf.service; import java.io.Serializable; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.jms.BytesMessage; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.Session; import javax.jms.StreamMessage; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Service; @Service public class ProducerService { @Resource(name = "jmsTemplate") private JmsTemplate jmsTemplate; /** * 向指定Destination發送text消息 * * @param destination * @param message */ public void sendTxtMessage(Destination destination, final String message) { if (null == destination) { destination = jmsTemplate.getDefaultDestination(); } jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); System.out.println("springJMS send text message..."); } /** * 向指定Destination發送map消息 * * @param destination * @param message */ public void sendMapMessage(Destination destination, final String message) { if (null == destination) { destination = jmsTemplate.getDefaultDestination(); } jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { MapMessage mapMessage = session.createMapMessage(); mapMessage.setString("msgId", message); return mapMessage; } }); System.out.println("springJMS send map message..."); } /** * 向指定Destination發送序列化的對象 * * @param destination * @param object * object 必須序列化 */ public void sendObjectMessage(Destination destination, final Serializable object) { if (null == destination) { destination = jmsTemplate.getDefaultDestination(); } jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createObjectMessage(object); } }); System.out.println("springJMS send object message..."); } /** * 向指定Destination發送字節消息 * * @param destination * @param bytes */ public void sendBytesMessage(Destination destination, final byte[] bytes) { if (null == destination) { destination = jmsTemplate.getDefaultDestination(); } jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { BytesMessage bytesMessage = session.createBytesMessage(); bytesMessage.writeBytes(bytes); return bytesMessage; } }); System.out.println("springJMS send bytes message..."); } /** * 向默認隊列發送Stream消息 */ public void sendStreamMessage(Destination destination) { jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { StreamMessage message = session.createStreamMessage(); message.writeString("stream string"); message.writeInt(11111); return message; } }); System.out.println("springJMS send Strem message..."); } }
-隊列消息監聽器QueueMessageListener.java代碼:spring-mvc
package com.gxf.listener; import javax.jms.BytesMessage; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.StreamMessage; import javax.jms.TextMessage; import org.apache.activemq.advisory.DestinationEvent; import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQMessage; import org.apache.activemq.command.DestinationInfo; public class QueueMessageListener implements MessageListener { //當收到消息後,自動調用該方法 @Override public void onMessage(Message message) { try { ActiveMQDestination queues=(ActiveMQDestination)message.getJMSDestination(); /** * 監聽消息隊列queue1中的消息 */ if(queues.getPhysicalName().equalsIgnoreCase("queue1")) { System.out.println("監聽隊列:"+queues.getPhysicalName()+"消費了消息:"); // 若是是文本消息 if (message instanceof TextMessage) { TextMessage tm = (TextMessage) message; try { System.out.println("from get textMessage:\t" + tm.getText()); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 若是是Map消息 if (message instanceof MapMessage) { MapMessage mm = (MapMessage) message; try { System.out.println("from get MapMessage:\t" + mm.getString("msgId")); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 監聽消息隊列queue2中的消息 */ if(queues.getPhysicalName().equalsIgnoreCase("queue2")) { System.out.println("監聽隊列:"+queues.getPhysicalName()+"消費了消息:"); // 若是是文本消息 if (message instanceof TextMessage) { TextMessage tm = (TextMessage) message; try { System.out.println("from get textMessage:\t" + tm.getText()); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 若是是Map消息 if (message instanceof MapMessage) { MapMessage mm = (MapMessage) message; try { System.out.println("from get MapMessage:\t" + mm.getString("msgId")); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } catch (JMSException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // 若是是Object消息 if (message instanceof ObjectMessage) { ObjectMessage om = (ObjectMessage) message; System.out.println("from get ObjectMessage:\t"); } // 若是是bytes消息 if (message instanceof BytesMessage) { System.out.println("from get BytesMessage:\t"); byte[] b = new byte[1024]; int len = -1; BytesMessage bm = (BytesMessage) message; try { while ((len = bm.readBytes(b)) != -1) { System.out.println(new String(b, 0, len)); } } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 若是是Stream消息 if (message instanceof StreamMessage) { System.out.println("from get BytesMessage:\t"); StreamMessage sm = (StreamMessage) message; try { System.out.println(sm.readString()); System.out.println(sm.readInt()); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} }
-啓動項目訪問main,進行消息發送: session
後臺打印往不一樣隊列發送的消息和監聽到不一樣隊列中的消息: mvc
隊列queue1發送消費了14條消息,queue2發送消費了10條消息:app
到此想要的功能需求已實現,持續更新中。。。