很簡(jiǎn)單的實(shí)現(xiàn)方法,大家?guī)兔纯茨懿荒芾^續(xù)完善。
步驟:
- 添加rabbitmq配置
- 添加rabbitmq配置類
- 添加訂單生產(chǎn)者
- 添加訂單消費(fèi)者(修改狀態(tài))
1、添加rabbitmq配置
注意一定要開啟手動(dòng)應(yīng)答,不然可能會(huì)報(bào)錯(cuò)
spring
rabbitmq:
host: 你的地址
port: 5672
username: admin
password: admin
listener:
simple:
# 開啟手動(dòng)應(yīng)答
acknowledge-mode: manual
2、添加rabbitmq配置類
下面這段代碼看著有點(diǎn)繁瑣,我解釋一下邏輯關(guān)系。
1、聲明訂單交換機(jī)、聲明訂單隊(duì)列,在訂單隊(duì)列中聲明綁定的死信交換機(jī)
2、訂單交換機(jī)Binding訂單隊(duì)列
3、聲明死信交換機(jī)、隊(duì)列并且Binding
package com.lutao.config;
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
@Configuration
public class RabbitMqConfig {
public static final String ORDER_QUEUE = "order_queue";
public static final String ORDER_EXCHANGE = "order_exchange";
public static final String ORDER_DEAD_EXCHANGE = "order_dead_exchange";
public static final String ORDER_DEAD_QUEUE = "order_dead_queue";
public static final String ORDER_ROUTING = "order_routing";
public static final String ORDER_DEAD_ROUTING = "order_dead_routing";
/**
* 訂單交換機(jī)
*/
@Bean("orderExchange")
public DirectExchange getOrderExchange(){
return new DirectExchange(ORDER_EXCHANGE);
}
/**
* 訂單隊(duì)列
*/
@Bean("orderQueue")
public Queue getOrderQueue(){
HashMap<String, Object> map = new HashMap<>(3);
map.put("x-dead-letter-exchange",ORDER_DEAD_EXCHANGE);
map.put("x-dead-letter-routing-key",ORDER_DEAD_ROUTING);
map.put("x-message-ttl",30000);
return QueueBuilder.durable(ORDER_QUEUE).withArguments(map).build();
}
/**
* 訂單交換機(jī)與訂單隊(duì)列綁定
*/
@Bean
public Binding oExchangeBindingO(
@Qualifier("orderExchange") DirectExchange exchange,
@Qualifier("orderQueue") Queue queue
){
return BindingBuilder.bind(queue).to(exchange).with(ORDER_ROUTING);
}
/**
* 死信交換機(jī)
*/
@Bean("orderDeadExchange")
public DirectExchange getOrderDeadExchange(){
return new DirectExchange(ORDER_DEAD_EXCHANGE);
}
/**
* 死信隊(duì)列
*/
@Bean("orderDeadQueue")
public Queue getOrderDradQueue(){
return new Queue(ORDER_DEAD_QUEUE,true,false,false,null);
}
@Bean
public Binding deadExchangeDead(
@Qualifier("orderDeadExchange") DirectExchange exchange,
@Qualifier("orderDeadQueue") Queue queue
){
return BindingBuilder.bind(queue).to(exchange).with(ORDER_DEAD_ROUTING);
}
}
3、添加訂單生產(chǎn)者
在插入新訂單數(shù)據(jù)時(shí),向訂單隊(duì)列發(fā)送一條延遲時(shí)間為30秒的信息
@Override
@Transactional
public void insertOrder(OT ot) {
Orders orders = new Orders();
BeanUtils.copyProperties(ot, orders);
List<CartVO> listCart = ot.getListCart();
//生成訂單號(hào)
String onumber = get16UUID();
orders.setOnumber(onumber);
ordersMapper.insertOrder(orders);
//刪除購(gòu)物車
Integer[] cids = new Integer[listCart.size()];
for (int i = 0; i < listCart.size(); i++) {
CartVO cartVO = listCart.get(i);
ordersMapper.insertGoods(onumber, cartVO);
cids[i] = listCart.get(i).getCid();
}
//發(fā)送到隊(duì)列中
//將訂單編號(hào)發(fā)送到隊(duì)列中
log.info("將訂單編號(hào)發(fā)送到訂單隊(duì)列中");
rabbitTemplate.convertAndSend(RabbitMqConfig.ORDER_EXCHANGE,RabbitMqConfig.ORDER_ROUTING,orders.getOnumber());
cartMapper.deleteCid(cids);
}
4、死信隊(duì)列消費(fèi)者處理信息文章來源:http://www.zghlxwxcb.cn/news/detail-679328.html
30秒后訂單進(jìn)入死信隊(duì)列,由消費(fèi)者消費(fèi),根據(jù)訂單號(hào)查詢訂單的狀態(tài)如果還是0(未支付)則將狀態(tài)更新成(2)已取消。文章來源地址http://www.zghlxwxcb.cn/news/detail-679328.html
@Autowired
private OrdersMapper ordersMapper;
/**
* 判斷是否在規(guī)定時(shí)間內(nèi)支付
*
* @param message
* @param channel
* @throws IOException
*/
@RabbitListener(queues = RabbitMqConfig.ORDER_DEAD_QUEUE)
public void receive(Message message, Channel channel) throws IOException {
log.info("訂單進(jìn)入死信隊(duì)列");
String msg = new String(message.getBody());
log.info("訂單號(hào):"+msg);
OrderPageRequest request = new OrderPageRequest();
request.setOnumber(msg);
Integer status = ordersMapper.getStatusByONumber(msg);
if (status != null && status == 0){
ordersMapper.updateStatus(msg,2);
}
//應(yīng)答
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
到了這里,關(guān)于超簡(jiǎn)單的RabbitMq實(shí)現(xiàn)訂單超時(shí)自動(dòng)取消的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!