一、前言
spring代码基于 SSM整合(spring-springmvc-mybatis)之CRUD ;代码地址:(基础班:https://gitee.com/joy521125/ssm-senior-base.gitmaven版:https://gitee.com/joy521125/ssm-senior.git)
代码地址:https://gitee.com/joy521125/ssm-senior.git(activemq 分支)
二、activem 队列queue配置
默认是持久化;
activemq_queue.xml(生产者)配置信息:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 3 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 4 <bean id="mQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 5 <!-- tcp://自己的服务器地址:端口 --> 6 <property name="brokerURL" value="tcp://192.168.2.189:61616"></property> 7 <property name="userName" value="admin"></property> 8 <property name="password" value="admin"></property> 9 <!-- 强制异步发送大幅提升性能;但意味着无论消息是否已发送,send() 方法都会立即返回,这可能导致消息丢失 --> 10 <property name="useAsyncSend" value="true"></property> 11 <!-- 如果未设置此标志,则不会使用单独的线程为连接中的每个会话分派消息。但是,如果有多个会话,或者会话未处于自动确认或重复 ok 模式,则始终使用单独的线程。 默认情况下,此值设置为 true,并且会话分派异步发生。 --> 12 <property name="alwaysSessionAsync" value="false"></property> 13 </bean> 14 15 <bean id="connectionFactory" class="org.apache.activemq.jms.pool.PooledConnectionFactory"> 16 <property name="connectionFactory" ref="mQConnectionFactory"></property> 17 <property name="maxConnections" value="100"></property> 18 </bean> 19 <!-- 这个是队列目的地,点对点的 --> 20 <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> 21 <constructor-arg index="0" value="spring-active-queue"></constructor-arg> 22 </bean> 23 24 <!-- 发布者配置 实际项目中消费者配置和发布者配置分开--> 25 <bean id="mQConverter" class="org.springframework.jms.support.converter.SimpleMessageConverter"></bean> 26 <!-- 发布者配置 实际项目中消费者配置和发布者配置分开--> 27 <bean id="queueTemplate" class="org.springframework.jms.core.JmsTemplate"> 28 <property name="connectionFactory" ref="connectionFactory" /> 29 <property name="defaultDestination" ref="destinationQueue" /> 30 <property name="messageConverter" ref="mQConverter" /> 31 <property name="sessionTransacted" value="true" /> 32 </bean> 33 <!-- 发布者配置 实际项目中消费者配置和发布者配置分开--> 34 <bean class="org.muses.ssm.entity.MessagePublisher"> 35 <property name="jmsTemplate" ref="queueTemplate"></property> 36 </bean> 37 38 </beans>
发布消息MessagePublisher.java:
1 package org.muses.ssm.entity; 2 3 import org.springframework.jms.core.JmsTemplate; 4 5 public class MessagePublisher { 6 7 private static JmsTemplate jmsTemplate; 8 9 public static JmsTemplate getJmsTemplate() { 10 return jmsTemplate; 11 } 12 13 public static void setJmsTemplate(JmsTemplate jmsTemplate) { 14 MessagePublisher.jmsTemplate = jmsTemplate; 15 } 16 17 public static void sendMessage(Object message) { 18 jmsTemplate.convertAndSend(message); 19 } 20 21 }
web.xml中添加 activemq_queue.xml配置:
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value>classpath:spring.xml,classpath:activemq_queue.xml</param-value> 4 </context-param>
完整web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 <filter> 7 <filter-name>CharacterEncodingFilter</filter-name> 8 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 9 <init-param> 10 <param-name>encoding</param-name> 11 <param-value>utf-8</param-value> 12 </init-param> 13 </filter> 14 <filter-mapping> 15 <filter-name>CharacterEncodingFilter</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping> 18 <context-param> 19 <param-name>contextConfigLocation</param-name> 20 <param-value>classpath:spring.xml,classpath:activemq_queue.xml</param-value> 21 </context-param> 22 <listener> 23 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 24 </listener> 25 <servlet> 26 <servlet-name>springDispatcherServlet</servlet-name> 27 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 28 <init-param> 29 <param-name>contextConfigLocation</param-name> 30 <param-value>classpath:springmvc.xml</param-value> 31 </init-param> 32 <load-on-startup>1</load-on-startup> 33 </servlet> 34 35 <servlet-mapping> 36 <servlet-name>springDispatcherServlet</servlet-name> 37 <url-pattern>/</url-pattern> 38 </servlet-mapping> 39 <!-- 配置HiddenHttpMethodFilter :可以把post请求转为delete or put请求 --> 40 41 <filter> 42 <filter-name>HiddenHttpMethodFilter</filter-name> 43 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 44 </filter> 45 <filter-mapping> 46 <filter-name>HiddenHttpMethodFilter</filter-name> 47 <url-pattern>/*</url-pattern> 48 </filter-mapping> 49 </web-app>View Code
三、非持久化、事务配置
非持久化配置
只需要更改activemq_queue.xml 配置信息;增加属性explicitQosEnabled ,explicitQosEnabled 属性值设置为true,deLiveryMode 设置为1 即配置完成;其他配置不变;此时如果发送了一条消息且该条消息没有被消费掉;如果关闭mq服务再重新启动mq,那么之前未消费掉的消息就没有了,显示未消费的数量为0;
事务配置
只需要更改activemq_queue.xml 配置信息;增加属性sessionTransacted 属性值设置为true;表示jms的事务由spring托管;
即:
<!-- 发布者配置 实际项目中消费者配置和发布者配置分开--> <bean id="queueTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="destinationQueue" /> <property name="messageConverter" ref="mQConverter" /> <property name="sessionTransacted" value="true"/><!-- sessionTransacted属性配置成了true,表示jms的事务由spring托管 --> <property name="explicitQosEnabled" value="true" /> <!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置为true,默认false--> <property name="deliveryMode" value="2" /> <!-- 发送模式 DeliveryMode.NON_PERSISTENT=1:非持久 ; DeliveryMode.PERSISTENT=2:持久--> </bean>
四、activemq_queue.xml消费者
activemq_queue.xml 消费者配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 3 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 4 <bean id="mQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 5 <!-- tcp://自己的服务器地址:端口 --> 6 <property name="brokerURL" value="tcp://192.168.2.189:61616"></property> 7 <property name="userName" value="admin"></property> 8 <property name="password" value="admin"></property> 9 <!-- 强制异步发送大幅提升性能;但意味着无论消息是否已发送,send() 方法都会立即返回,这可能导致消息丢失 --> 10 <property name="useAsyncSend" value="true"></property> 11 <!-- 如果未设置此标志,则不会使用单独的线程为连接中的每个会话分派消息。但是,如果有多个会话,或者会话未处于自动确认或重复 ok 模式,则始终使用单独的线程。 默认情况下,此值设置为 true,并且会话分派异步发生。 --> 12 <property name="alwaysSessionAsync" value="false"></property> 13 </bean> 14 15 <bean id="connectionFactory" class="org.apache.activemq.jms.pool.PooledConnectionFactory"> 16 <property name="connectionFactory" ref="mQConnectionFactory"></property> 17 <property name="maxConnections" value="100"></property> 18 </bean> 19 <!-- 这个是队列目的地,点对点的 --> 20 <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> 21 <constructor-arg index="0" value="spring-active-queue"></constructor-arg> 22 </bean> 23 <!-- 消费者配置 实际项目中消费者配置和发布者配置分开 --> 24 <bean id="mqMessageListener" class="org.muses.ssm.utils.MqMessageListener"> 25 </bean> 26 <!-- 消费者配置 实际项目中消费者配置和发布者配置分开 --> 27 <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 28 <property name="connectionFactory" ref="connectionFactory"></property> 29 <property name="destination" ref="destinationQueue"></property> 30 <property name="messageListener" ref="mqMessageListener"></property> 31 </bean> 32 </beans>
标签:xml,web,spring,queue,org,activemq From: https://www.cnblogs.com/lixiuming521125/p/16649086.html