导入配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.7.7</version>
</dependency>
配置文件配置
JavaConfig配置
@Component
@Slf4j
public class RabbitmqConfig implements InitializingBean {
@Resource
private RabbitTemplate rabbitTemplate;
@Override
public void afterPropertiesSet() throws Exception {
log.info("初始化配置rabbitmq配置");
// rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(new ObjectMapper()));
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean b, String s) {
if(!b)
{
log.error("发送消息到mq失败,原因:{}",s);
}
}
});
rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
log.error("消息返回回调触发,交换机:{},路由:{},消息内容:{},原因:{}",exchange,routingKey,message,replyText);
}
});
}
}
通过@Bean注入MessageConverter,保证消息的正确传输
@Bean
public MessageConverter jsonMessageConverter(ObjectMapper objectMapper) {
return new Jackson2JsonMessageConverter(objectMapper);
}
修改后的代码文章来源:https://www.toymoban.com/news/detail-852699.html
@Override
//关于事务注解
public Result seckillVoucher(Long voucherId) {
// 1,执行lua脚本,lua脚本用于判断库存是否充足,扣库存操作
Long userId=UserHolder.getUser().getId();
long orderId=redisIDworker.nextId("order");
//执行此操作前保证,用户查看优惠券的请求,和添加优惠券时 已经将秒杀优惠圈的库存数量添加到了redis中
Long result=stringRedisTemplate.execute(
SECKILL_SCRIPT,
Collections.emptyList(),
voucherId.toString(),
userId.toString(),
String.valueOf(orderId)
);
//获取lua脚本返回值
int r=result.intValue();
if(r!=0)
{
return Result.fail(r==1?"库存不足":"不能重复下单");
}
// //获取代理对象
// IVoucherOrderService proxy= (IVoucherOrderService) AopContext.currentProxy();
//异步写数据库
VoucherOrder voucherOrder = new VoucherOrder();
voucherOrder.setId(orderId);
voucherOrder.setUserId(userId);
voucherOrder.setVoucherId(voucherId);
rabbitTemplate.convertAndSend(SaveVoucherConstants.SECKILL_VOUCHER_SAVE_QUEUE,voucherOrder);
log.info("发送保存秒杀券订单信息成功:{}",orderId);
return Result.ok("seckSuccess");
}
监听器文章来源地址https://www.toymoban.com/news/detail-852699.html
@Component
@Slf4j
public class AsyncSaveVoucherListener {
@Resource
private IVoucherOrderService voucherOrderService;
@Resource
private ISeckillVoucherService seckillVoucherService;
@RabbitListener(queuesToDeclare = {@Queue(name= SaveVoucherConstants.SECKILL_VOUCHER_SAVE_QUEUE)})
public void AsyncSave(VoucherOrder voucherOrder)
{
log.info("接收到存储订单信息的消息,{}", JSON.toJSON(voucherOrder).toString());
boolean success = seckillVoucherService.update().setSql("stock=stock-1").eq("voucher_id", voucherOrder.getVoucherId()).gt("stock", 0).update();
voucherOrderService.save(voucherOrder);
log.info("订单信息存储完成?{}",success);
}
}
到了这里,关于黑马点评用rabbitmq实现优惠券秒杀下单后的异步操作数据库数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!