springboot中模拟实现订单未支付取消订单
2021-04-10 23:29
标签:equals create hashmap redist 指定 vat listener let pat 1)先写一个日志订单的消费者; OrderConsumer.java 2)在RabbitConfig.java中配置相关需要的配置文件; RabbitConfig.java 3)控制层方法; IndexController.java springboot中模拟实现订单未支付取消订单 标签:equals create hashmap redist 指定 vat listener let pat 原文地址:https://www.cnblogs.com/xie-qi/p/13364014.htmlpackage com.seecen.redis.rabbitmq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@Slf4j
public class OrderConsumer {
@Autowired
private RedisTemplate redisTemplate;
@RabbitListener(queues = {"order.queue"},
containerFactory = "rabbitListenerContainerFactory")
public void insertLog(Map
//=========死信队列实现订单超时取消=============
/**
* 订单延迟队列的交换机(下单之后存入的交换机)
* @return
*/
@Bean
public DirectExchange orderTtlDirect(){
return (DirectExchange)
ExchangeBuilder
.directExchange("order.ttl.exchange")
.durable(true).build();
}
/**
* 订单延迟队列()
* @return
*/
@Bean
public Queue orderTtlQueue(){
Map
package com.seecen.redis.controller;
import com.seecen.redis.aop.Log;
import com.seecen.redis.aop.LogType;
import com.seecen.redis.entity.TAdmin;
import com.seecen.redis.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @author bigpeng
* @create 2020-07-21-16:18
*/
@Controller
@Slf4j
public class IndexController {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private AdminService adminService;
@Autowired
private RabbitTemplate rabbitTemplate;
@ResponseBody
@GetMapping("/order/{product}")
public String order(@PathVariable("product") String product){
//模拟一个订单,使用map存储数据
Map map = new HashMap();
map.put("product",product);
String orderId=UUID.randomUUID().toString();
map.put("orderId",orderId);
map.put("status","0");//只发状态 0:未支付 1:已支付 -1:已取消
//todo 将订单记录插入数据库
//redisTemplate.opsForValue().set("order:"+orderId,map);
redisTemplate.opsForHash().putAll("order:"+orderId,map);
//发送mq消息到超时队列
rabbitTemplate.convertAndSend(
"order.ttl.exchange",
"order.ttl.cancel",
map);
log.info("下单成功,订单号:"+orderId);
return "下单成功,订单号:"+orderId;
}
@ResponseBody
@GetMapping("/order/pay/{orderId}")
public String pay(@PathVariable("orderId") String orderId){
Boolean hasKey = redisTemplate.hasKey("order:" + orderId);
if (hasKey){
redisTemplate.opsForHash().put("order:"+orderId,"status","1");
log.info("订单:{}支付成功",orderId);
}
return "订单:"+orderId+"支付成功!";
}
}
文章标题:springboot中模拟实现订单未支付取消订单
文章链接:http://soscw.com/index.php/essay/74012.html