日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实战05_SSM整合ActiveMQ支持多种类型消息

發(fā)布時間:2024/9/27 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实战05_SSM整合ActiveMQ支持多种类型消息 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

接上一篇:實戰(zhàn)04_SSM整合ActiveMQ支持多種類型消息https://blog.csdn.net/weixin_40816738/article/details/100572124

1、StreamMessage java原始值數(shù)據(jù)流
2、MapMessage 鍵值對
3、TextMessage 字符串
4、ObjectMessage 一個序列化的java對象
5、BytesMessage 一個字節(jié)的數(shù)據(jù)流

此文章為企業(yè)實戰(zhàn)的展示操作,如果有地方不懂請留言,我看到后,會進(jìn)行統(tǒng)一回復(fù),讓我們一起進(jìn)步,為自己加油!!!

項目名項目說明
ssm-activemq父工程,統(tǒng)一版本控制
producer生產(chǎn)者
consumer消費者
base-pojo公共實體類
base-dao公共接口,數(shù)據(jù)庫連接

文章目錄

  • 五、生產(chǎn)者producer
    • 5.1. 創(chuàng)建QueueController
    • 5.2. 創(chuàng)建QueueController
    • 5.3. 創(chuàng)建IQueueProductService接口
    • 5.4. 創(chuàng)建ITopicProductService接口
    • 5.5. 創(chuàng)建QueueProductServiceImpl實現(xiàn)類
    • 5.6. 創(chuàng)建TopicProductServiceImpl實現(xiàn)類
    • 5.7. 在resources 目錄下創(chuàng)建spring文件夾
      • 5.7.1. 在spring目錄下創(chuàng)建applicationContext-jms-queue.xml文件
      • 5.7.2. 在spring目錄下創(chuàng)建applicationContext-jms-topic.xml文件
      • 5.7.3. 在spring目錄下創(chuàng)建applicationContext-service.xml文件
      • 5.7.4. 在spring目錄下創(chuàng)建applicationContext-trans.xml文件
      • 5.7.5. 在spring目錄下創(chuàng)建spring-mvc.xml文件
      • 5.8. 在resources目錄下創(chuàng)建log4j.properties
      • 5.9. 在resources目錄下創(chuàng)建log4j.xml
      • 5.10. web.xml
    • 5.11. 驗證測試
    • 5.11.1. 數(shù)據(jù)庫聯(lián)通測試UserMapperTest
    • 5.11.2. 點對點測試場景
    • 5.11.3. 發(fā)布訂閱測試場景

五、生產(chǎn)者producer

5.1. 創(chuàng)建QueueController

package com.gblfy.mq.controller;import com.alibaba.fastjson.JSON; import com.gblfy.mq.entity.User; import com.gblfy.mq.service.IQueueProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import javax.jms.Destination; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;/*** @author gblfy* @ClassNme QueueController* @Description TODO* @Date 2019/8/31 18:45* @version1.0*/ @Controller @RequestMapping("/queue") public class QueueController {@Autowiredprivate Destination QUEUE_Str;//傳遞Str字符串@Autowiredprivate Destination QUEUE_Str_LIST;//傳遞傳遞JSON字符串@Autowiredprivate Destination QUEUE_OBJ;//傳遞OBJ對象@Autowiredprivate Destination QUEUE_MAP;//傳遞MAP@Autowiredprivate IQueueProductService iQueueProductService;/*** 發(fā)送消息類型 String* 測試鏈接:http://localhost:8080/queue/itemList** @return*/@RequestMapping("/str")@ResponseBodypublic String sendStringMessage() {String messge = "send string type message";iQueueProductService.sendStringMessage(QUEUE_Str, messge);return "success";}/*** 發(fā)送消息類型 List* <p>* 1.List<User>轉(zhuǎn)成jsonString* 2.由于list沒有實現(xiàn)序列化,因此不能傳遞對象* <p>* 測試鏈接:http://localhost:8080/queue/objList** @return*/@RequestMapping("/objList")@ResponseBodypublic String sendListMessage() {//獲取對象User user = getObj();//將獲取對象芳容ListList<User> userList = getListObj(user);//把對象列表轉(zhuǎn)成jsonStringString jsonString = JSON.toJSONString(userList);iQueueProductService.sendListMessage(QUEUE_Str_LIST, jsonString);return "success";}/*** 發(fā)送消息類型 Obj* <p>* 測試鏈接:http://localhost:8080/queue/obj** @return*/@RequestMapping("/obj")@ResponseBodypublic String sendObjMessage() {//獲取對象User user = getObj();//把對象傳遞iQueueProductService.sendObjMessage(QUEUE_OBJ, user);return "success";}/*** 發(fā)送消息類型 MAP* <p>* 測試鏈接:http://localhost:8080/queue/map** @return*/@RequestMapping("/map")@ResponseBodypublic String sendMapMessage() {String mapKey = "mapKey";String mapValue = "mapValue";//把map傳遞iQueueProductService.sendMapMessage(QUEUE_MAP, mapKey, mapValue);return "success";}/*** 封裝map** @param key* @param object* @return*/public Map<String, Object> getMap(String key, Object object) {Map<String, Object> map = new HashMap<>();map.put(key, object);return map;}/*** 封裝List** @param user* @return*/public List<User> getListObj(User user) {List<User> userList = new ArrayList<User>();userList.add(user);return userList;}/*** 封裝公用對象** @return*/private User getObj() {//封裝測試數(shù)據(jù)User user = new User().builder().id("1").name("yuxin").age(02).build();return user;} }

5.2. 創(chuàng)建QueueController

package com.gblfy.mq.controller;import com.alibaba.fastjson.JSON; import com.gblfy.mq.entity.User; import com.gblfy.mq.service.ITopicProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import javax.jms.Destination; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;/*** @author gblfy* @ClassNme TOPICController* @Description TODO* @Date 2019/8/31 18:45* @version1.0*/ @Controller @RequestMapping("/topic") public class TopicController {@Autowiredprivate Destination TOPIC_Str;//傳遞Str字符串@Autowiredprivate Destination TOPIC_Str_LIST;//傳遞傳遞JSON字符串@Autowiredprivate Destination TOPIC_OBJ;//傳遞OBJ對象@Autowiredprivate Destination TOPIC_MAP;//傳遞MAP@Autowiredprivate ITopicProductService iTopicProductService;/*** 發(fā)送消息類型 String* 測試鏈接:http://localhost:8080/topic/itemList** @return*/@RequestMapping("/str")@ResponseBodypublic String sendStringMessage() {String messge = "send string type message";iTopicProductService.sendStringMessage(TOPIC_Str, messge);return "success";}/*** 發(fā)送消息類型 List* <p>* 1.List<User>轉(zhuǎn)成jsonString* 2.由于list沒有實現(xiàn)序列化,因此不能傳遞對象* <p>* 測試鏈接:http://localhost:8080/topic/objList** @return*/@RequestMapping("/objList")@ResponseBodypublic String sendListMessage() {//獲取對象User user = getObj();//將獲取對象芳容ListList<User> userList = getListObj(user);//把對象列表轉(zhuǎn)成jsonStringString jsonString = JSON.toJSONString(userList);iTopicProductService.sendListMessage(TOPIC_Str_LIST, jsonString);return "success";}/*** 發(fā)送消息類型 Obj* <p>* 測試鏈接:http://localhost:8080/topic/obj** @return*/@RequestMapping("/obj")@ResponseBodypublic String sendObjMessage() {//獲取對象User user = getObj();//把對象傳遞iTopicProductService.sendObjMessage(TOPIC_OBJ, user);return "success";}/*** 發(fā)送消息類型 MAP* <p>* 測試鏈接:http://localhost:8080/topic/map** @return*/@RequestMapping("/map")@ResponseBodypublic String sendMapMessage() {String mapKey = "mapKey";String mapValue = "mapValue";//把map傳遞iTopicProductService.sendMapMessage(TOPIC_MAP, mapKey, mapValue);return "success";}/*** 封裝map** @param key* @param object* @return*/public Map<String, Object> getMap(String key, Object object) {Map<String, Object> map = new HashMap<>();map.put(key, object);return map;}/*** 封裝List** @param user* @return*/public List<User> getListObj(User user) {List<User> userList = new ArrayList<User>();userList.add(user);return userList;}/*** 封裝公用對象** @return*/private User getObj() {//封裝測試數(shù)據(jù)User user = new User().builder().id("1").name("yuxin").age(02).build();return user;} }

5.3. 創(chuàng)建IQueueProductService接口

package com.gblfy.mq.service;import javax.jms.Destination; import java.io.Serializable;public interface IQueueProductService {/*** 發(fā)送消息類型 String** @param destination* @param msg*/void sendStringMessage(Destination destination, final String msg);/*** 送消息類型 List** @param destination* @param msg*/void sendListMessage(Destination destination, final String msg);/*** 發(fā)送消息類型 Obj** @param destination* @param obj*/void sendObjMessage(Destination destination, final Serializable obj);/*** 發(fā)送消息類型 map** @param destination* @param message*/void sendMapMessage(Destination destination, final String mapKey, final String message);/*** 向指定Destination發(fā)送字節(jié)消息** @param destination* @param bytes*/void sendBytesMessage(Destination destination, final byte[] bytes);/*** 向默認(rèn)隊列發(fā)送Stream消息*/void sendStreamMessage(Destination destination); }

5.4. 創(chuàng)建ITopicProductService接口

package com.gblfy.mq.service;import javax.jms.Destination; import java.io.Serializable;public interface ITopicProductService {/*** 發(fā)送消息類型 String** @param destination* @param msg*/void sendStringMessage(Destination destination, final String msg);/*** 送消息類型 List** @param destination* @param msg*/void sendListMessage(Destination destination, final String msg);/*** 發(fā)送消息類型 Obj** @param destination* @param obj*/void sendObjMessage(Destination destination, final Serializable obj);/*** 發(fā)送消息類型 map** @param destination* @param message*/void sendMapMessage(Destination destination, final String mapKey, final String message);/*** 向指定Destination發(fā)送字節(jié)消息** @param destination* @param bytes*/void sendBytesMessage(Destination destination, final byte[] bytes);/*** 向默認(rèn)隊列發(fā)送Stream消息*/void sendStreamMessage(Destination destination); }

5.5. 創(chuàng)建QueueProductServiceImpl實現(xiàn)類

package com.gblfy.mq.service.impl;import com.gblfy.mq.service.IQueueProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Service;import javax.jms.*; import java.io.Serializable;/*** @author gblfy* @ClassNme QueueProductService* @Description TODO* @Date 2019/9/4 14:43* @version1.0*/ @Service public class QueueProductServiceImpl implements IQueueProductService {@Autowiredprivate JmsTemplate jmsQueueTemplate;/*** 發(fā)送消息類型 String** @param destination* @param msg*/public void sendStringMessage(Destination destination, final String msg) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}/*** 送消息類型 List** @param destination* @param msg*/public void sendListMessage(Destination destination, final String msg) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}/*** 發(fā)送消息類型 Obj** @param destination* @param obj*/public void sendObjMessage(Destination destination, final Serializable obj) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createObjectMessage(obj);}});}/*** 發(fā)送消息類型 map** @param destination* @param message*/public void sendMapMessage(Destination destination, final String mapKey, final String message) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {MapMessage mapMessage = session.createMapMessage();mapMessage.setString(mapKey, message);return mapMessage;}});System.out.println("springJMS send map message...");}/*** 向指定Destination發(fā)送字節(jié)消息** @param destination* @param bytes*/public void sendBytesMessage(Destination destination, final byte[] bytes) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.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...");}/*** 向默認(rèn)隊列發(fā)送Stream消息*/public void sendStreamMessage(Destination destination) {jmsQueueTemplate.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...");} }

5.6. 創(chuàng)建TopicProductServiceImpl實現(xiàn)類

package com.gblfy.mq.service.impl;import com.gblfy.mq.service.ITopicProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Service;import javax.jms.*; import java.io.Serializable;/*** @author gblfy* @ClassNme QueueProductService* @Description TODO* @Date 2019/9/4 14:43* @version1.0*/@Service public class TopicProductServiceImpl implements ITopicProductService {@Autowiredprivate JmsTemplate jmsTopicTemplate;/*** 發(fā)送消息類型 String** @param destination* @param msg*/public void sendStringMessage(Destination destination, final String msg) {if (null == destination) {destination = jmsTopicTemplate.getDefaultDestination();}jmsTopicTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}/*** 送消息類型 List** @param destination* @param msg*/public void sendListMessage(Destination destination, final String msg) {if (null == destination) {destination = jmsTopicTemplate.getDefaultDestination();}jmsTopicTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}/*** 發(fā)送消息類型 Obj** @param destination* @param obj*/public void sendObjMessage(Destination destination, final Serializable obj) {if (null == destination) {destination = jmsTopicTemplate.getDefaultDestination();}jmsTopicTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createObjectMessage(obj);}});}/*** 發(fā)送消息類型 map** @param destination* @param message*/public void sendMapMessage(Destination destination, final String mapKey, final String message) {if (null == destination) {destination = jmsTopicTemplate.getDefaultDestination();}jmsTopicTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {MapMessage mapMessage = session.createMapMessage();mapMessage.setString(mapKey, message);return mapMessage;}});System.out.println("springJMS send map message...");}/*** 向指定Destination發(fā)送字節(jié)消息** @param destination* @param bytes*/public void sendBytesMessage(Destination destination, final byte[] bytes) {if (null == destination) {destination = jmsTopicTemplate.getDefaultDestination();}jmsTopicTemplate.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...");}/*** 向默認(rèn)隊列發(fā)送Stream消息*/public void sendStreamMessage(Destination destination) {jmsTopicTemplate.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...");} }

5.7. 在resources 目錄下創(chuàng)建spring文件夾

5.7.1. 在spring目錄下創(chuàng)建applicationContext-jms-queue.xml文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--公共部分 Start--><!-- 真正可以產(chǎn)生Connection的ConnectionFactory,由對應(yīng)的 JMS服務(wù)廠商提供--><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL"value="tcp://192.168.43.156:61616"/><property name="trustAllPackages" value="true"/><property name="userName" value="admin"></property><property name="password" value="admin"></property></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目標(biāo)ConnectionFactory對應(yīng)真實的可以產(chǎn)生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="targetConnectionFactory"/></bean><!-- Spring提供的JMS工具類,它可以進(jìn)行消息發(fā)送、接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 這個connectionFactory對應(yīng)的是我們定義的Spring提供的那個ConnectionFactory對象 --><property name="connectionFactory" ref="connectionFactory"/></bean><!--公共部分 End--><!--隊列名稱 gblfy_queue_String--><bean id="QUEUE_Str" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="QUEUE_Str"/></bean><!--隊列名稱 gblfy_queue_list--><bean id="QUEUE_Str_LIST" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="QUEUE_Str_LIST"/></bean><!--隊列名稱 gblfy_queue_obj--><bean id="QUEUE_OBJ" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="QUEUE_OBJ"/></bean><!--這個是隊列目的地,導(dǎo)入索引庫--><bean id="QUEUE_MAP" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="QUEUE_MAP"/></bean></beans>

5.7.2. 在spring目錄下創(chuàng)建applicationContext-jms-topic.xml文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--公共部分 Start--><!-- 真正可以產(chǎn)生Connection的ConnectionFactory,由對應(yīng)的 JMS服務(wù)廠商提供--><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL"value="tcp://192.168.43.156:61616"/><property name="trustAllPackages" value="true"/><property name="userName" value="admin"></property><property name="password" value="admin"></property></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目標(biāo)ConnectionFactory對應(yīng)真實的可以產(chǎn)生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="targetConnectionFactory"/></bean><!-- Spring提供的JMS工具類,它可以進(jìn)行消息發(fā)送、接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 這個connectionFactory對應(yīng)的是我們定義的Spring提供的那個ConnectionFactory對象 --><property name="connectionFactory" ref="connectionFactory"/></bean><!--公共部分 End--><!--隊列名稱 gblfy_queue_String--><bean id="TOPIC_Str" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="TOPIC_Str"/></bean><!--隊列名稱 gblfy_queue_list--><bean id="TOPIC_Str_LIST" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="TOPIC_Str_LIST"/></bean><!--隊列名稱 gblfy_queue_obj--><bean id="TOPIC_OBJ" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="TOPIC_OBJ"/></bean><!--這個是隊列目的地,導(dǎo)入索引庫--><bean id="TOPIC_MAP" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="TOPIC_MAP"/></bean></beans>

5.7.3. 在spring目錄下創(chuàng)建applicationContext-service.xml文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- spring自動去掃描base-pack下面或者子包下面的java文件--><!--管理Service實現(xiàn)類--><context:component-scan base-package="com.gblfy.mq"/> </beans>

5.7.4. 在spring目錄下創(chuàng)建applicationContext-trans.xml文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- spring自動去掃描base-pack下面或者子包下面的java文件--><!--管理Service實現(xiàn)類--><context:component-scan base-package="com.gblfy.mq"/> </beans>

5.7.5. 在spring目錄下創(chuàng)建spring-mvc.xml文件

<?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:p="http://www.springframework.org/schema/p"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.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 掃描controller --><context:component-scan base-package="com.gblfy.mq.controller" /><!-- Spring 來掃描指定包下的類,并注冊被@Component,@Controller,@Service,@Repository等注解標(biāo)記的組件 --><mvc:annotation-driven /><!-- 配置SpringMVC的視圖解析器--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean> </beans>

5.8. 在resources目錄下創(chuàng)建log4j.properties

log4j.rootLogger=error,CONSOLE,A log4j.addivity.org.apache=falselog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=error log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p %x - %m%n log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.Encoding=gbk log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.A=org.apache.log4j.DailyRollingFileAppender log4j.appender.A.File=${catalina.home}/logs/FH_log/PurePro_ log4j.appender.A.DatePattern=yyyy-MM-dd'.log' log4j.appender.A.layout=org.apache.log4j.PatternLayout log4j.appender.A.layout.ConversionPattern=[FH_sys] %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}\:%L \: %m%n

5.9. 在resources目錄下創(chuàng)建log4j.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"><!-- Appenders --><appender name="console" class="org.apache.log4j.ConsoleAppender"><param name="Target" value="System.out" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%d{yyyy HH:mm:ss} %-5p %c - %m%n" /></layout></appender><!-- Application Loggers --><logger name="com"><level value="error" /></logger><!-- 3rdparty Loggers --><logger name="org.springframework.core"><level value="error" /></logger><logger name="org.springframework.beans"><level value="error" /></logger><logger name="org.springframework.context"><level value="error" /></logger><logger name="org.springframework.web"><level value="error" /></logger><logger name="org.springframework.jdbc"><level value="error" /></logger><logger name="org.mybatis.spring"><level value="error" /></logger><logger name="java.sql"><level value="error" /></logger><!-- Root Logger --><root><priority value="error" /><appender-ref ref="console" /></root></log4j:configuration>

5.10. 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"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><display-name>producer-web</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 解決post亂碼 --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>ssm</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 指定加載的配置文件 ,通過參數(shù)contextConfigLocation加載--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/spring-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>ssm</servlet-name><url-pattern>/</url-pattern></servlet-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/spring/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>

5.11. 驗證測試

5.11.1. 數(shù)據(jù)庫聯(lián)通測試UserMapperTest

package com.gblfy.mq.mapper;import com.gblfy.mq.entity.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.sql.DataSource; import java.sql.Connection; import java.util.Arrays; import java.util.List;/*** 測試互數(shù)據(jù)庫連接*/ public class UserMapperTest {private ApplicationContext ioc =new ClassPathXmlApplicationContext("/spring/applicationContext-dao.xml");private UserMapper userMapper =ioc.getBean("userMapper", UserMapper.class);/*** 測試數(shù)據(jù)庫連接池*/@Testpublic void testDataSource() throws Exception {DataSource ds = ioc.getBean("dataSource", DataSource.class);System.out.println(ds);Connection conn = ds.getConnection();System.out.println(conn);}/*** 查詢單個商品操作*/@Testpublic void itemById() {User item = userMapper.selectById(1);System.out.println("~~~~~~~~~:" + item);}/*** 查詢商多個品操作*/@Testpublic void itemListByIds() {List<Integer> ids = Arrays.asList(1, 2);List<User> itemList = userMapper.selectBatchIds(ids);for (User item : itemList) {System.out.println("~~~~~~~" + item);}}/*** 查詢商品列表操作*/@Testpublic void itemList() {List<User> itemList = userMapper.selectList(null);for (User item : itemList) {System.out.println("這是一個測試" + "\n" + item);}} }

5.11.2. 點對點測試場景

package com.gblfy.mq.service;import com.alibaba.fastjson.JSON; import com.gblfy.mq.entity.User; import org.apache.activemq.command.ActiveMQQueue; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.jms.*; import java.io.Serializable; import java.util.ArrayList; import java.util.List;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/spring/applicationContext-jms-queue.xml") public class IQueueProductServiceTest {@Autowiredprivate JmsTemplate jmsQueueTemplate;/*** 消息類型 List*/@Testpublic void sendStringMessage() {Destination destination = new ActiveMQQueue("QUEUE_Str");sendListMessage(destination, "send string queue message!!!");}/*** 消息類型 List*/@Testpublic void sendListMessage() {Destination destination = new ActiveMQQueue("QUEUE_Str_LIST");User user = getObj();List<User> userList = getListObj(user);//把對象列表轉(zhuǎn)成jsonStringfinal String jsonString = JSON.toJSONString(userList);sendListMessage(destination, jsonString);}/*** 消息類型 Obj*/@Testpublic void sendObjMessage() {Destination destination = new ActiveMQQueue("QUEUE_OBJ");User user = getObj();sendObjMessage(destination, user);}/*** 消息類型 Map*/@Testpublic void sendMapMessage() {String mapKey = "mapKey";String mapValue = "mapValue";Destination destination = new ActiveMQQueue("QUEUE_MAP");sendMapMessage(destination, mapKey, mapValue);}/*** 發(fā)送消息類型 String** @param destination* @param msg*/public void sendStringMessage(Destination destination, final String msg) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}/*** 送消息類型 List** @param destination* @param msg*/public void sendListMessage(Destination destination, final String msg) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}/*** 發(fā)送消息類型 Obj** @param destination* @param obj*/public void sendObjMessage(Destination destination, final Serializable obj) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createObjectMessage(obj);}});}/*** 發(fā)送消息類型 map** @param destination* @param message*/public void sendMapMessage(Destination destination, final String mapKey, final String message) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {MapMessage mapMessage = session.createMapMessage();mapMessage.setString(mapKey, message);return mapMessage;}});System.out.println("springJMS send map message...");}/*** 向指定Destination發(fā)送字節(jié)消息** @param destination* @param bytes*/public void sendBytesMessage(Destination destination, final byte[] bytes) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.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...");}/*** 向默認(rèn)隊列發(fā)送Stream消息*/public void sendStreamMessage(Destination destination) {jmsQueueTemplate.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...");}/*** 封裝List** @param user* @return*/public List<User> getListObj(User user) {List<User> userList = new ArrayList<User>();userList.add(user);return userList;}/*** 封裝公用對象** @return*/private User getObj() {//封裝測試數(shù)據(jù)User user = new User().builder().id("1").name("yuxin").age(02).build();return user;} }

5.11.3. 發(fā)布訂閱測試場景

package com.gblfy.mq.service;import com.alibaba.fastjson.JSON; import com.gblfy.mq.entity.User; import org.apache.activemq.command.ActiveMQQueue; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.jms.*; import java.io.Serializable; import java.util.ArrayList; import java.util.List;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/spring/applicationContext-jms-topic.xml") public class ITopicProductServiceTest {@Autowiredprivate JmsTemplate jmsQueueTemplate;/*** 消息類型 List*/@Testpublic void sendStringMessage() {Destination destination = new ActiveMQQueue("TOPIC_Str");sendListMessage(destination, "send string queue message!!!");}/*** 消息類型 List*/@Testpublic void sendListMessage() {Destination destination = new ActiveMQQueue("TOPIC_Str_LIST");User user = getObj();List<User> userList = getListObj(user);//把對象列表轉(zhuǎn)成jsonStringfinal String jsonString = JSON.toJSONString(userList);sendListMessage(destination, jsonString);}/*** 消息類型 Obj*/@Testpublic void sendObjMessage() {Destination destination = new ActiveMQQueue("TOPIC_OBJ");User user = getObj();sendObjMessage(destination, user);}/*** 消息類型 Map*/@Testpublic void sendMapMessage() {String mapKey = "mapKey";String mapValue = "mapValue";Destination destination = new ActiveMQQueue("TOPIC_MAP");sendMapMessage(destination, mapKey, mapValue);}/*** 發(fā)送消息類型 String** @param destination* @param msg*/public void sendStringMessage(Destination destination, final String msg) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}/*** 送消息類型 List** @param destination* @param msg*/public void sendListMessage(Destination destination, final String msg) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(msg);}});}/*** 發(fā)送消息類型 Obj** @param destination* @param obj*/public void sendObjMessage(Destination destination, final Serializable obj) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createObjectMessage(obj);}});}/*** 發(fā)送消息類型 map** @param destination* @param message*/public void sendMapMessage(Destination destination, final String mapKey, final String message) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {MapMessage mapMessage = session.createMapMessage();mapMessage.setString(mapKey, message);return mapMessage;}});System.out.println("springJMS send map message...");}/*** 向指定Destination發(fā)送字節(jié)消息** @param destination* @param bytes*/public void sendBytesMessage(Destination destination, final byte[] bytes) {if (null == destination) {destination = jmsQueueTemplate.getDefaultDestination();}jmsQueueTemplate.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...");}/*** 向默認(rèn)隊列發(fā)送Stream消息*/public void sendStreamMessage(Destination destination) {jmsQueueTemplate.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...");}/*** 封裝List** @param user* @return*/public List<User> getListObj(User user) {List<User> userList = new ArrayList<User>();userList.add(user);return userList;}/*** 封裝公用對象** @return*/private User getObj() {//封裝測試數(shù)據(jù)User user = new User().builder().id("1").name("yuxin").age(02).build();return user;}}

下一篇:實戰(zhàn)06_SSM整合ActiveMQ支持多種類型消息https://blog.csdn.net/weixin_40816738/article/details/100572147

本專欄項目下載鏈接:

下載方式鏈接詳細(xì)
GitLab項目https://gitlab.com/gb-heima/ssm-activemq
Gitgit clone git@gitlab.com:gb-heima/ssm-activemq.git
zip包https://gitlab.com/gb-heima/ssm-activemq/-/archive/master/ssm-activemq-master.zip
Fork地址https://gitlab.com/gb-heima/ssm-activemq/-/forks/new

總結(jié)

以上是生活随笔為你收集整理的实战05_SSM整合ActiveMQ支持多种类型消息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。