今天大佬给我Spring配置,然后让我实现订单在未付款状态,N秒后自动失效 ↓
一、大佬给的配置
spring:
#activemq延迟队列
activemq:
inMemory: false
broker-url: tcp://47.108.204.1:61616
user: admin
password: admin
二、添加依赖和俩文件

ActiveMQ依赖
<!--activeMq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
第一个配置文件------ActiveMQConfiguration
package com.nxt.order.utils.ActiveMQ;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
/**
* @author LunarYouI
* @create 2021-08-04 11:34
*/
@Configuration
public class ActiveMQConfiguration {
//这个Bean用于和mq建立连接、mq的地址、用户名、密码从配置文件中获取
@Bean(name = "innerConnectionFactory")
@Primary
public ActiveMQConnectionFactory firstConnectionFactory(
//mq服务地址
@Value("${spring.activemq.broker-url}") String brokerUrl,
//mq用户名
@Value("${spring.activemq.user}") String username,
//mq密码
@Value("${spring.activemq.password}") String password) {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
factory.setBrokerURL(brokerUrl);
factory.setUserName(username);
factory.setPassword(password);
factory.setTrustAllPackages(true);
factory.setMaxThreadPoolSize(ActiveMQConnection.DEFAULT_THREAD_POOL_SIZE);
RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
//定义ReDelivery(重发机制)机制 ,重发时间间隔是100毫秒,最大重发次数是3次
//是否在每次尝试重新发送失败后,增长这个等待时间
redeliveryPolicy.setUseExponentialBackOff(true);
//重发次数,默认为6次 这里设置为1次
redeliveryPolicy.setMaximumRedeliveries(1);
//重发时间间隔,默认为1秒
redeliveryPolicy.setInitialRedeliveryDelay(1000);
//第一次失败后重新发送之前等待500毫秒,第二次失败再等待500 * 2毫秒,这里的2就是value
redeliveryPolicy.setBackOffMultiplier(2);
//最大传送延迟,只在useExponentialBackOff为true时有效(V5.5),假设首次重连间隔为10ms,倍数为2,那么第
//二次重连时间间隔为 20ms,第三次重连时间间隔为40ms,当重连时间间隔大的最大重连时间间隔时,以后每次重连时间间隔都为最大重连时间间隔。
redeliveryPolicy.setMaximumRedeliveryDelay(1000);
factory.setRedeliveryPolicy(redeliveryPolicy);
return factory;
}
//在Topic模式中,对消息的监听需要对containerFactory进行配置
@Bean("topicJmsListenerContainerFactory")
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(
//将“innerConnectionFactory”这个Bean注入
@Qualifier("innerConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setSessionTransacted(true);
factory.setPubSubDomain(true);
return factory;
}
// 在Queue模式中,对消息的监听需要对containerFactory进行配置
@Bean("queueJmsListenerContainerFactory")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(
//将“innerConnectionFactory”这个Bean注入
@Qualifier("innerConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setSessionTransacted(true);
factory.setPubSubDomain(false);
return factory;
}
}
第二个配置文件------ActivemqOperator
package com.nxt.order.utils.ActiveMQ;
import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import javax.jms.*;
/**
* @author LunarYouI
* @create 2021-08-04 11:36
*/
@Component
public class ActivemqOperator {
private final JmsMessagingTemplate jmsTemplate;
@Autowired
public ActivemqOperator(JmsMessagingTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
/**
* 发送主题消息
*/
public void sendMsgTopic(String topic, String message) {
Destination destination = new ActiveMQTopic(topic);
jmsTemplate.convertAndSend(destination, message);
}
/**
* 发送队列消息
*/
public void sendMsgQueue(String queue, String message) {
Destination destination = new ActiveMQQueue(queue);
jmsTemplate.convertAndSend(destination, message);
}
/**
* 发送延时队列消息
*/
public <T> void delaySend(String queue, String message, Long time) {
Connection connection = null;
Session session = null;
MessageProducer producer = null;
ConnectionFactory connectionFactory = jmsTemplate.getConnectionFactory();
Destination destination = new ActiveMQQueue(queue);
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
producer.setDeliveryMode(JmsProperties.DeliveryMode.PERSISTENT.getValue());
ObjectMessage objectMessage = session.createObjectMessage(message);
objectMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
producer.send(objectMessage);
session.commit();
} catch (Exception e) {
throw new RuntimeException("delaySend exception : " + e.getMessage());
} finally {
try {
if (producer != null) {
producer.close();
}
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
三、演示效果
package com.nxt.order.controller;
import com.nxt.order.utils.ActiveMQ.ActivemqOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author LunarYouI
* @create 2021-08-04 11:42
*/
@RequestMapping("/testMq")
@RestController
public class Testaaaaa {
@Autowired
ActivemqOperator activemqOperator;
@GetMapping("/queue")
public String testMqQueue() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println("当前订单【123456789】生成时间: "+df.format(new Date()));// new Date()为获取当前系统时间
//设置延时时间推送到activeMQ
activemqOperator.delaySend("testQueue","延时10S",10000L);
return "OK";
}
@JmsListener(destination = "testQueue", containerFactory = "queueJmsListenerContainerFactory")
public void listenerMq(String message) {
//业务逻辑,根据单号查询是否已经付款
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println("当前订单【123456789】失效时间: " + df.format(new Date()));// new Date()为获取当前系统时间
}
}

如果我们把生成时间改成订单创建,失效时间改成订单结束时间到了 订单失效,那么此功能就算实现了
在后续的使用中,建议把ActivemqOperator里面的String换成泛型

本文介绍了一个基于ActiveMQ实现的订单自动失效功能。通过配置ActiveMQ并利用延时队列特性,使得未付款订单能在指定时间后自动变为失效状态。

2477

被折叠的 条评论
为什么被折叠?



