java订单未支付_springboot中模拟实现订单未支付取消订单

本文展示了如何在SpringBoot应用中实现一个简单的订单系统,当订单未支付时通过Redis和RabbitMQ模拟订单自动取消。通过创建HashMap存储订单数据,设置订单状态并利用RedisTemplate操作数据。此外,还利用RabbitTemplate发送MQ消息到超时队列,以处理未支付订单的取消逻辑。
摘要由CSDN通过智能技术生成

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

@Slf4jpublic classIndexController {

@AutowiredprivateRedisTemplate redisTemplate;

@AutowiredprivateAdminService adminService;

@AutowiredprivateRabbitTemplate 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+"支付成功!";

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值