消息队列+事件表
不适用数据量特别大的情况,可以在中小型系统中应用
流程图
通过消息事件id,主键约束,来保证消息重复消费的问题
项目演示
-
service-order 生产端(定时任务存信息到activemq)
-
service-pay 消费端(监听者在activemq取数据)
启动activemq
activemq.xml中配置死信队列
<destinationPolicy>
<policyMap>
<policyEntries>
<!--死信队列-->
<policyEntry queue=">">
<deadLetterStrategy>
<individualDeadLetterStrategy queuePrefix="DLQ."
useQueueForQueueMessages="true" processNonPersistent="true" />
</deadLetterStrategy>
</policyEntry>
<policyEntry topic=">" >
<!-- The constantPendingMessageLimitStrategy is used to prevent
slow topic consumers to block producers and affect other consumers
by limiting the number of messages that are retained
For more information, see:
http://activemq.apache.org/slow-consumer-handling.html
-->
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
启动过程中可能出现的错误
activemq 启动时出现错误 Address already in use: JVM_Bind
pom.xml
<!--activemq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--activemq pool-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
application.yml
server:
port: 5001
spring:
application:
name: service-order
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
pool:
enabled: true
max-connections: 100
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.113:3306/service-order?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
dbcp2:
initial-size: 5
min-idle: 5
max-total: 5
max-wait-millis: 200
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
mybatis:
mapper-locations:
- classpath:mapper/*.xml
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@EnableJms
public class ServiceOrderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceOrderApplication.class, args);
}
}
生产端代码 service-order
ActiveMQConfig配置类
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
/**
* 消息生产端的配置
*/
@Configuration
public class ActiveMQConfig {
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Bean
public Queue queue() {
return new ActiveMQQueue("ActiveMQQueue");
}
@Bean
public ActiveMQConnectionFactory connectionFactory(){
return new ActiveMQConnectionFactory(brokerUrl);
}
}
定时任务
import com.dandan.serviceorder.dao.TblOrderEventDao;
import com.dandan.serviceorder.entity.TblOrderEvent;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.jms.Queue;
import java.util.List;
@Component
public class ProduceTask {
@Autowired
private TblOrderEventDao tblOrderEventDao;
@Autowired
private Queue queue;
@Autowired
JmsMessagingTemplate jmsMessagingTemplate;
@Scheduled(cron="0/5 * * * * ?")
@Transactional(rollbackFor = Exception.class)
public void task(){
System.out.println("定时任务");
List<TblOrderEvent> tblOrderEventList = tblOrderEventDao.selectByOrderType("1");
for (int i = 0; i < tblOrderEventList.size(); i++) {
TblOrderEvent event = tblOrderEventList.get(i);
// 更改这条数据的orderType为2
tblOrderEventDao.updateEvent(event.getOrderType());
System.out.println("修改数据库完成");
jmsMessagingTemplate.convertAndSend(queue,JSONObject.fromObject(event).toString());
}
}
}
事件表sql
CREATE TABLE `tbl_order_event` (
`id` int(20) NOT NULL,
`order_type` varchar(10) DEFAULT NULL,
`process` varchar(255) DEFAULT NULL,
`content` varchar(255) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
消费端代码 service-pay
消息消费端的配置
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
/**
* 消息消费端的配置
*/
@Configuration
public class ActiveMQConfig {
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
/**
* 连接工厂
* @param redeliveryPolicy
* @return
*/
@Bean
public ActiveMQConnectionFactory connectionFactory(RedeliveryPolicy redeliveryPolicy){
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("admin","admin",brokerUrl);
activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
return activeMQConnectionFactory;
}
/**
* 重发配置
* @return
*/
@Bean
public RedeliveryPolicy redeliveryPolicy(){
RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
return redeliveryPolicy;
}
/**
* 设置消息队列 确认机制
* @param activeMQConnectionFactory
* @return
*/
@Bean
public JmsListenerContainerFactory jmsListenerContainerFactory(ActiveMQConnectionFactory activeMQConnectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(activeMQConnectionFactory);
// 1: 自动确认,2: 客户端手动确认,3:自动批量确认,4 事务提交并确认。
bean.setSessionAcknowledgeMode(2);
return bean;
}
}
消息监听
import com.dandan.servicepay.dao.TblOrderEventDao;
import com.dandan.servicepay.entity.TblOrderEvent;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
/**
* 消息监听
*/
@Component
public class ConsumerQueue {
@Autowired
private TblOrderEventDao tblOrderEventDao;
@JmsListener(destination = "ActiveMQQueue",containerFactory = "jmsListenerContainerFactory")
public void receive(TextMessage textMessage, Session session) throws JMSException {
try {
System.out.println("收到的消息:"+textMessage.getText());
String content = textMessage.getText();
TblOrderEvent tblOrderEvent = (TblOrderEvent) JSONObject.toBean(JSONObject.fromObject(content),TblOrderEvent.class);
tblOrderEventDao.insert(tblOrderEvent);
// 业务完成,确认消息 消费成功
textMessage.acknowledge();
}catch (Exception e){
// 回滚消息
e.printStackTrace();
// e.getMessage(); // 放到log中。
System.out.println("异常了");
//恢复数据到消息队列
session.recover();
}
}
/**
* mq中消息被消费6次都没有收到ack,就进入死信队列
*
* 补偿 处理(人工,脚本)。自己根据自己情况。
* @param text
*/
@JmsListener(destination = "DLQ.ActiveMQQueue")
public void receive2(String text){
System.out.println("死信队列:"+text);
}
}