springBoot使用rabbitMq实现支付后订单状态更新

1、pom.xml

        
            org.springframework.boot
            spring-boot-starter-amqp
        

2、application.yml

spring:
  rabbitmq:
    addresses: 127.0.0.1
    port: 5672
    username: guest
    password: guest

3、使用

发送消息

PayServiceImpl.java

@Slf4j
@Service
public class PayServiceImpl implements IPayService {
    @Autowired
	private AmqpTemplate amqpTemplate;

    //TODO pay发送MQ消息,mall接受MQ消息
	amqpTemplate.convertAndSend(QUEUE_PAY_NOTIFY, new Gson().toJson(payInfo));

}

接收消息

listener/PayMsgListener

@Component
@RabbitListener(queues = "payNotify")
@Slf4j
public class PayMsgListener {

	@Autowired
	private IOrderService orderService;

	@RabbitHandler
	public void process(String msg) {
		log.info("【接收到消息】=> {}", msg);

		PayInfo payInfo = new Gson().fromJson(msg, PayInfo.class);
		if (payInfo.getPlatformStatus().equals("SUCCESS")) {
			//修改订单里的状态
			orderService.paid(payInfo.getOrderNo());
		}
	}
}

你可能感兴趣的:(java-rabbitmq,spring,boot,rabbitmq)