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

接上一篇:实战04_SSM整合ActiveMQ支持多种类型消息https://blog.csdn.net/weixin_40816738/article/details/100572124

1、StreamMessage java原始值数据流
2、MapMessage 键值对
3、TextMessage 字符串
4、ObjectMessage 一个序列化的java对象
5、BytesMessage 一个字节的数据流

此文章为企业实战的展示操作,如果有地方不懂请留言,我看到后,会进行统一回复,让我们一起进步,为自己加油!!!

项目名项目说明
ssm-activemq父工程,统一版本控制
producer生产者
consumer消费者
base-pojo公共实体类
base-dao公共接口,数据库连接

文章目录

  • 五、生产者producer
    • 5.1. 创建QueueController
    • 5.2. 创建QueueController
    • 5.3. 创建IQueueProductService接口
    • 5.4. 创建ITopicProductService接口
    • 5.5. 创建QueueProductServiceImpl实现类
    • 5.6. 创建TopicProductServiceImpl实现类
    • 5.7. 在resources 目录下创建spring文件夹
      • 5.7.1. 在spring目录下创建applicationContext-jms-queue.xml文件
      • 5.7.2. 在spring目录下创建applicationContext-jms-topic.xml文件
      • 5.7.3. 在spring目录下创建applicationContext-service.xml文件
      • 5.7.4. 在spring目录下创建applicationContext-trans.xml文件
      • 5.7.5. 在spring目录下创建spring-mvc.xml文件
      • 5.8. 在resources目录下创建log4j.properties
      • 5.9. 在resources目录下创建log4j.xml
      • 5.10. web.xml
    • 5.11. 验证测试
    • 5.11.1. 数据库联通测试UserMapperTest
    • 5.11.2. 点对点测试场景
    • 5.11.3. 发布订阅测试场景

五、生产者producer

5.1. 创建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;/*** 发送消息类型 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";}/*** 发送消息类型 List* <p>* 1.List<User>转成jsonString* 2.由于list没有实现序列化,因此不能传递对象* <p>* 测试链接:http://localhost:8080/queue/objList** @return*/@RequestMapping("/objList")@ResponseBodypublic String sendListMessage() {//获取对象User user = getObj();//将获取对象芳容ListList<User> userList = getListObj(user);//把对象列表转成jsonStringString jsonString = JSON.toJSONString(userList);iQueueProductService.sendListMessage(QUEUE_Str_LIST, jsonString);return "success";}/*** 发送消息类型 Obj* <p>* 测试链接:http://localhost:8080/queue/obj** @return*/@RequestMapping("/obj")@ResponseBodypublic String sendObjMessage() {//获取对象User user = getObj();//把对象传递iQueueProductService.sendObjMessage(QUEUE_OBJ, user);return "success";}/*** 发送消息类型 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() {//封装测试数据User user = new User().builder().id("1").name("yuxin").age(02).build();return user;}
}

5.2. 创建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;/*** 发送消息类型 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";}/*** 发送消息类型 List* <p>* 1.List<User>转成jsonString* 2.由于list没有实现序列化,因此不能传递对象* <p>* 测试链接:http://localhost:8080/topic/objList** @return*/@RequestMapping("/objList")@ResponseBodypublic String sendListMessage() {//获取对象User user = getObj();//将获取对象芳容ListList<User> userList = getListObj(user);//把对象列表转成jsonStringString jsonString = JSON.toJSONString(userList);iTopicProductService.sendListMessage(TOPIC_Str_LIST, jsonString);return "success";}/*** 发送消息类型 Obj* <p>* 测试链接:http://localhost:8080/topic/obj** @return*/@RequestMapping("/obj")@ResponseBodypublic String sendObjMessage() {//获取对象User user = getObj();//把对象传递iTopicProductService.sendObjMessage(TOPIC_OBJ, user);return "success";}/*** 发送消息类型 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() {//封装测试数据User user = new User().builder().id("1").name("yuxin").age(02).build();return user;}
}

5.3. 创建IQueueProductService接口

package com.gblfy.mq.service;import javax.jms.Destination;
import java.io.Serializable;public interface IQueueProductService {/*** 发送消息类型  String** @param destination* @param msg*/void sendStringMessage(Destination destination, final String msg);/*** 送消息类型 List** @param destination* @param msg*/void sendListMessage(Destination destination, final String msg);/*** 发送消息类型 Obj** @param destination* @param obj*/void sendObjMessage(Destination destination, final Serializable obj);/*** 发送消息类型 map** @param destination* @param message*/void sendMapMessage(Destination destination, final String mapKey, final String message);/*** 向指定Destination发送字节消息** @param destination* @param bytes*/void sendBytesMessage(Destination destination, final byte[] bytes);/*** 向默认队列发送Stream消息*/void sendStreamMessage(Destination destination);
}

5.4. 创建ITopicProductService接口

package com.gblfy.mq.service;import javax.jms.Destination;
import java.io.Serializable;public interface ITopicProductService {/*** 发送消息类型  String** @param destination* @param msg*/void sendStringMessage(Destination destination, final String msg);/*** 送消息类型 List** @param destination* @param msg*/void sendListMessage(Destination destination, final String msg);/*** 发送消息类型 Obj** @param destination* @param obj*/void sendObjMessage(Destination destination, final Serializable obj);/*** 发送消息类型 map** @param destination* @param message*/void sendMapMessage(Destination destination, final String mapKey, final String message);/*** 向指定Destination发送字节消息** @param destination* @param bytes*/void sendBytesMessage(Destination destination, final byte[] bytes);/*** 向默认队列发送Stream消息*/void sendStreamMessage(Destination destination);
}

5.5. 创建QueueProductServiceImpl实现类

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;/*** 发送消息类型  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);}});}/*** 发送消息类型 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);}});}/*** 发送消息类型 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发送字节消息** @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...");}/*** 向默认队列发送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. 创建TopicProductServiceImpl实现类

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;/*** 发送消息类型  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);}});}/*** 发送消息类型 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);}});}/*** 发送消息类型 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发送字节消息** @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...");}/*** 向默认队列发送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 目录下创建spring文件夹

5.7.1. 在spring目录下创建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--><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--><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"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="targetConnectionFactory"/></bean><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 这个connectionFactory对应的是我们定义的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><!--这个是队列目的地,导入索引库--><bean id="QUEUE_MAP" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="QUEUE_MAP"/></bean></beans>

5.7.2. 在spring目录下创建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--><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--><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"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="targetConnectionFactory"/></bean><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 这个connectionFactory对应的是我们定义的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><!--这个是队列目的地,导入索引库--><bean id="TOPIC_MAP" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="TOPIC_MAP"/></bean></beans>

5.7.3. 在spring目录下创建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实现类--><context:component-scan base-package="com.gblfy.mq"/>
</beans>

5.7.4. 在spring目录下创建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实现类--><context:component-scan base-package="com.gblfy.mq"/>
</beans>

5.7.5. 在spring目录下创建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等注解标记的组件 --><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目录下创建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目录下创建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><!-- 指定加载的配置文件 ,通过参数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. 数据库联通测试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;/*** 测试互数据库连接*/
public class UserMapperTest {private ApplicationContext ioc =new ClassPathXmlApplicationContext("/spring/applicationContext-dao.xml");private UserMapper userMapper =ioc.getBean("userMapper", UserMapper.class);/*** 测试数据库连接池*/@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);//把对象列表转成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);}/*** 发送消息类型  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);}});}/*** 发送消息类型 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);}});}/*** 发送消息类型 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发送字节消息** @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...");}/*** 向默认队列发送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() {//封装测试数据User user = new User().builder().id("1").name("yuxin").age(02).build();return user;}
}

5.11.3. 发布订阅测试场景

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);//把对象列表转成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);}/*** 发送消息类型  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);}});}/*** 发送消息类型 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);}});}/*** 发送消息类型 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发送字节消息** @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...");}/*** 向默认队列发送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() {//封装测试数据User user = new User().builder().id("1").name("yuxin").age(02).build();return user;}}

下一篇:实战06_SSM整合ActiveMQ支持多种类型消息https://blog.csdn.net/weixin_40816738/article/details/100572147

本专栏项目下载链接:

下载方式链接详细
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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/523333.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

腾讯云一口气发布四大新品,云原生时代将正式开启

6月25日&#xff0c;在上海召开的KubeCon 2019大会上&#xff0c;腾讯云重磅发布多款适用于企业不同场景的云原生技术产品&#xff0c;包括企业级容器服务平台TKE、容器服务网格、Serverless 2.0、一站式DevOps四大产品。这四款云原生技术产品的发布将助力国内数百万企业“上云…

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

接上一篇&#xff1a;企业实战05_SSM整合ActiveMQ支持多种类型消息https://blog.csdn.net/weixin_40816738/article/details/100572129 1、StreamMessage java原始值数据流 2、MapMessage 键值对 3、TextMessage 字符串 4、ObjectMessage 一个序列化的java对象 5、BytesMessage…

linux下远程登录如何退出,Ubuntu 中rdesktop如何切换和退出远程桌面

Ubuntu 中rdesktop如何切换和退出远程桌面原文如下&#xff1a;I use RDP a lot and having to disconnect from my session to switch to another window is not an option. The problem lies somewhere with compiz. What supposed to happen when you hit ctrlaltenter is …

K8S精华问答 | K8S和Openstack发展方向是怎样的?

kubernetes&#xff0c;简称K8S&#xff0c;是用8代替8个字符“ubernete”而成的缩写。是一个开源的&#xff0c;用于管理云平台中多个主机上的容器化的应用&#xff0c;Kubernetes的目标是让部署容器化的应用简单并且高效&#xff08;powerful&#xff09;,Kubernetes提供了应…

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

接上一篇&#xff1a;企业实战06_SSM整合ActiveMQ支持多种类型消息https://blog.csdn.net/weixin_40816738/article/details/100572147 1、StreamMessage java原始值数据流 2、MapMessage 键值对 3、TextMessage 字符串 4、ObjectMessage 一个序列化的java对象 5、BytesMessage…

Linux进程核心代码怎么查看,GCOV查看arm-linux代码覆盖率

一、关于gcov工具gcov伴随gcc发布。gcc编译加入-fprofile-arcs -ftest-coverage参数生成二进制程序&#xff0c;执行测试用例生成代码覆盖率信息。1、如何使用gcov用GCC编译的时候加上-fprofile-arcs -ftest-coverage选项&#xff0c;链接的时候也加上。fprofile-arcs参数使gcc…

对于华为,英特尔与微软表示继续提供支持;亚马逊亲证云计算服务出现宕机;中国移动5G套餐曝光,每月都含200G流量……...

关注并标星星CSDN云计算极客头条&#xff1a;速递、最新、绝对有料。这里有企业新动、这里有业界要闻&#xff0c;打起十二分精神&#xff0c;紧跟fashion你可以的&#xff01;每周三次&#xff0c;打卡即read更快、更全了解泛云圈精彩newsgo go go 小米CC全新系列小王子&小…

Linux 环境 zookeeper集群安装、配置、验证

架构说明&#xff1a; Dubbo 建议使用 Zookeeper 作为服务的注册中心。Zookeeper 集群中只要有过半的节点是正常的情况下&#xff0c;那么整个集群对外就是可用的。正是基于这个特性&#xff0c; 要将 ZK 集群的节点数量要为奇数&#xff08;2n1&#xff1a; 如 3、 5、 7 个节…

怎么时装linux可用空间变大,[合集]OpenSUSE安装octave时装1G多texliv - 精华区 - 优秀的Free OS(Linux)版 - 北大未名BBS...

发信人: mytbk (LCPU AP|ArchLinux), 信区: Linux标 题: [合集]OpenSUSE安装octave时装1G多texlive发信站: 北大未名站 (2014年01月10日13:17:19 星期五), 站内信件───────────────────────────────────────作者PsySunrise (无良之心)…

基于zookeeper(集群)+LevelDB的ActiveMq高可用集群安装、配置、测试

参考 腾讯云~基于zookeeper(集群)LevelDB的ActiveMq高可用伪集群安装、配置、测试 https://gblfy.blog.csdn.net/article/details/127465602

5G 来了,我们可以做什么?

5G 清风徐来&#xff0c;静待应用花开。这是最好的时代&#xff0c;也是最具挑战的时代。当下就国内而言&#xff0c;随着四张 5G 商用牌照的正式发放&#xff0c;运营商们纷纷扩大并加快了建网的规模与速度&#xff1b;手机厂商们也早已于今年年初相继推出了 5G 手机&#xff…

linux内核之内存管理.doc,linux内核之内存管理.doc

linux内核之内存管理Linux内核之内存管理作者&#xff1a;harvey wang邮箱&#xff1a;harvey.perfect新浪博客地址&#xff1a;/harveyperfect &#xff0c;有关于减肥和学习英语相关的博文&#xff0c;欢迎交流把linux内存管理分为下面四个层面(一)硬件辅助的虚实地址转换(二…

SpringBoot项目去除druid监控的底部广告

文章目录一、阿里Druid广告的介绍二、引入Druid的Starter依赖三、编写配置类,进行广告的去除四 、启动项目进行测试五、原理说明一、阿里Druid广告的介绍 如果使用的是阿里Druid的数据库连接池,那么会自带一个数据库的监控页面. 但是其页面底部会有阿里的广告,如下图所示,并且…

精简linux操作系统,Tiny Core Linux—仅10多MB的精简Linux 操作系统发行版

Tiny Core Linux是一款很简约的桌面Linux&#xff0c;体积小且可高度可扩展&#xff0c;基于Linux 3.x内核、Busybox、Tiny X、FLTK图形用户界面、JWM窗口管理器。像其他操作系统最少也要几百MB了&#xff0c;Tiny Core Linux不仅体积小&#xff0c;对硬件配置要求也很高&#…

面试官问你MyBatis中有哪些设计模式,把这篇文章发给他

戳蓝字“CSDN云计算”关注我们哦&#xff01;作者 | 疯狂的蚂蚁来源 | https://dwz.cn/KFgol1De之前总结过一篇Spring中用到了哪些设计模式&#xff1a;《面试官:“谈谈Spring中都用到了那些设计模式?”》&#xff0c;昨晚看到了一篇很不错的一篇介绍MyBatis中都用到了那些设计…

Linux 启动mysql提示表不存在

编辑my.cnf 设置大小写敏感配置在 vim /etc/my.cnf #添加lower_case_table_names1,忽略大小写 #重启MYSQL服务 service mysql restart&#xff0c;

linux 定时器 代码,linux C++ 定时器代码

linux C 定时器代码&#xff1a;#include #include #include using namespace std;/*union sigval{int sival_int; //integer valuevoid *sival_ptr; //pointer value};struct sigevent{int sigev_notify; //notification typeint sigev_signo; //signal numberunion sigval …

MySQL启动出现The server quit without updating PID file错误解决办法

解决办法其实很简单&#xff1a; 将 /etc/mysql 下的 my.cnf 文件删除&#xff0c;再次启动MySQL服务 删除前注意备份

腾讯云首次公开边缘计算网络开源平台,拥抱5G与万物互联

6月25日&#xff0c;由Cloud Native Computing Foundation (CNCF) 主办的云原生技术大会在上海举办&#xff0c;腾讯云对外展示自身在边缘计算领域的最新进展&#xff0c;首次公开腾讯智能边缘计算网络平台TSEC&#xff08;Tencent Smart Edge Connector&#xff09;&#xff0…

linux输入qsub显示错误,linux – 使用qsub运行shellscript的’意外的文件结束’和’错误导入功能定义’错误...

我有以下shellscript&#xff1a;#!/bin/shcd /sw/local/bin/export LD_LIBRARY_PATH/sw/local/lib:/usr/local/Trolltech/Qt-4.7.2/lib:$LD_LIBRARY_PATH./FeatureFinderRaw -in /homes/JG-C1-18.mzML -out /homes/test_remove_after_use.featureXML -threads 20当我从自己的命…