-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d32469c
commit 388edbb
Showing
7 changed files
with
290 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.hmdp.config; | ||
|
||
import org.springframework.amqp.core.Binding; | ||
import org.springframework.amqp.core.BindingBuilder; | ||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.amqp.core.TopicExchange; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
@Configuration | ||
public class RabbitMQTopicConfig { | ||
public static final String QUEUE = "seckillQueue"; | ||
public static final String EXCHANGE = "seckillExchange"; | ||
public static final String ROUTINGKEY = "seckill.#"; | ||
@Bean | ||
public Queue queue(){ | ||
return new Queue(QUEUE); | ||
} | ||
@Bean | ||
public TopicExchange topicExchange(){ | ||
return new TopicExchange(EXCHANGE); | ||
} | ||
@Bean | ||
public Binding binding(){ | ||
return BindingBuilder.bind(queue()).to(topicExchange()).with(ROUTINGKEY); | ||
} | ||
// private static final String QUEUE01="queue_topic01"; | ||
// private static final String QUEUE02="queue_topic02"; | ||
// private static final String EXCHANGE = "topicExchange"; | ||
// private static final String ROUTINGKEY01 = "#.queue.#"; | ||
// private static final String ROUTINGKEY02 = "*.queue.#"; | ||
// @Bean | ||
// public Queue topicqueue01(){ | ||
// return new Queue(QUEUE01); | ||
// } | ||
// @Bean | ||
// public Queue topicqueue02(){ | ||
// return new Queue(QUEUE02); | ||
// } | ||
// @Bean | ||
// public TopicExchange topicExchange(){ | ||
// return new TopicExchange(EXCHANGE); | ||
// } | ||
// @Bean | ||
// public Binding topicbinding01(){ | ||
// return BindingBuilder.bind(topicqueue01()).to(topicExchange()).with(ROUTINGKEY01); | ||
// } | ||
// @Bean | ||
// public Binding topicbinding02(){ | ||
// return BindingBuilder.bind(topicqueue02()).to(topicExchange()).with(ROUTINGKEY02); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.hmdp.rebbitmq; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.hmdp.config.RabbitMQTopicConfig; | ||
import com.hmdp.entity.VoucherOrder; | ||
import com.hmdp.service.ISeckillVoucherService; | ||
import com.hmdp.service.IVoucherOrderService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import javax.annotation.Resource; | ||
|
||
/** | ||
* 消息消费者 | ||
*/ | ||
@Slf4j | ||
@Service | ||
public class MQReceiver { | ||
|
||
@Resource | ||
IVoucherOrderService voucherOrderService; | ||
|
||
@Resource | ||
ISeckillVoucherService seckillVoucherService; | ||
/** | ||
* 接收秒杀信息并下单 | ||
* @param msg | ||
*/ | ||
@Transactional | ||
@RabbitListener(queues = RabbitMQTopicConfig.QUEUE) | ||
public void receiveSeckillMessage(String msg){ | ||
log.info("接收到消息: "+msg); | ||
VoucherOrder voucherOrder = JSON.parseObject(msg, VoucherOrder.class); | ||
|
||
Long voucherId = voucherOrder.getVoucherId(); | ||
//5.一人一单 | ||
Long userId = voucherOrder.getUserId(); | ||
//5.1查询订单 | ||
int count = voucherOrderService.query().eq("user_id",userId).eq("voucher_id", voucherId).count(); | ||
//5.2判断是否存在 | ||
if(count>0){ | ||
//用户已经购买过了 | ||
log.error("该用户已购买过"); | ||
return ; | ||
} | ||
log.info("扣减库存"); | ||
//6.扣减库存 | ||
boolean success = seckillVoucherService | ||
.update() | ||
.setSql("stock = stock-1") | ||
.eq("voucher_id", voucherId) | ||
.gt("stock",0)//cas乐观锁 | ||
.update(); | ||
if(!success){ | ||
log.error("库存不足"); | ||
return; | ||
} | ||
//直接保存订单 | ||
voucherOrderService.save(voucherOrder); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.hmdp.rebbitmq; | ||
|
||
import com.hmdp.config.RabbitMQTopicConfig; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* 消息发送者 | ||
*/ | ||
@Slf4j | ||
@Service | ||
public class MQSender { | ||
|
||
@Autowired | ||
private RabbitTemplate rabbitTemplate; | ||
|
||
private static final String ROUTINGKEY = "seckill.message"; | ||
/** | ||
* 发送秒杀信息 | ||
* @param msg | ||
*/ | ||
public void sendSeckillMessage(String msg){ | ||
log.info("发送消息"+msg); | ||
rabbitTemplate.convertAndSend(RabbitMQTopicConfig.EXCHANGE,ROUTINGKEY,msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.