首页 > 其他分享 >七(一)、activemq queue整合spring

七(一)、activemq queue整合spring

时间:2022-09-02 16:34:22浏览次数:65  
标签:xml web spring queue org activemq

一、前言

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

相关文章

  • springboot启动报错
    springboot启动报错Description:FieldstationInformationServiceincom.htkj.aicheck.service.TrainCheckServicerequiredabeanoftype'com.htkj.ai.server.service......
  • SpringMVC 05: SpringMVC中携带数据的页面跳转
    SpringMVC默认的参数对象SpringMVC默认的参数对象是指,不用再另行创建,相当于SpringMVC内置对象,可以直接声明并使用默认的参数对象有:HttpServletRequest,HttpServletRespon......
  • SpringMVC异常处理
    基于配置的异常处理SpringMVC提供了一个处理控制器,在方法执行过程中所出现的异常的接口:HandlerExceptionResolver,该接口的实现类有:DefaultHandlerExceptionResolver和Sim......
  • springboot 自定义注解拦截器
    参考:https://blog.csdn.net/mxlgslcd/article/details/89155315第一步:自定义注解@Target(ElementType.METHOD)//可用在方法名上@Retention(RetentionPolicy.RUNTIME)/......
  • SpringBoot多数据源配置
    在实际的开发或者线上环境中,一般都不仅仅是一个数据库走天下,而是根据业务进行拆分多个数据库。另外,在日常开发中我们都是以单个数据库进行开发,在小型项目中是完全能够满足......
  • Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表
    通常,这类需求在客户端应用中不太会用到,但是在后端的各种统计分析模块会经常碰到。比如:通过折线图、柱状图、雷达图等可视化形式,更直观的展现和分析经营状况或系统运行情况......
  • Spring(二)——IOC
    Spring(二)——IOC概念1.什么是IOC控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理目的:降低耦合度2.IOC底层原理xml解析、工厂模式、反射IOC接......
  • [Spring框架]spring新注解配置、spring整合JUnit5
    1.spring新注解配置1.@Configuration作用:配置类,等同于bean.xml文件获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class)属性:value:指定配......
  • 为什么我要迁移SpringBoot到函数计算
    前言为什么要迁移?我们的业务有很多对外提供服务的RESTfulAPI,并且要执行很多不同的任务,例如同步连锁ERP中的商品信息到美团/饿了么等平台,在线开发票等。由于各种API......
  • 基于函数计算自定义运行时快速部署一个 springboot 项目 什么是函数计算?
    什么是函数计算?函数计算是事件驱动的全托管计算服务。使用函数计算,您无需采购与管理服务器等基础设施,只需编写并上传代码。函数计算为您准备好计算资源,弹性地可靠地运行任......