parent
a0fca4c050
commit
7d1e03540a
@ -0,0 +1,50 @@
|
|||||||
|
package com.mashibing.config;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.*;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建普通交换机&队列以及绑定上的死信交换机&队列
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class RabbitMQConfig {
|
||||||
|
|
||||||
|
public static final String ORDER_EXCHANGE = "order_exchange";
|
||||||
|
public static final String ORDER_QUEUE = "order_queue";
|
||||||
|
|
||||||
|
public static final String DEAD_EXCHANGE = "dead_exchange";
|
||||||
|
public static final String DEAD_QUEUE = "dead_queue";
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Exchange orderExchange(){
|
||||||
|
return ExchangeBuilder.fanoutExchange(ORDER_EXCHANGE).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue orderQueue(){
|
||||||
|
return QueueBuilder.durable(ORDER_QUEUE).deadLetterExchange(DEAD_EXCHANGE).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Exchange deadExchange(){
|
||||||
|
return ExchangeBuilder.fanoutExchange(DEAD_EXCHANGE).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue deadQueue(){
|
||||||
|
return QueueBuilder.durable(DEAD_QUEUE).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Binding orderBinding(Exchange orderExchange,Queue orderQueue){
|
||||||
|
return BindingBuilder.bind(orderQueue).to(orderExchange).with("").noargs();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Binding deadBinding(Exchange deadExchange,Queue deadQueue){
|
||||||
|
return BindingBuilder.bind(deadQueue).to(deadExchange).with("").noargs();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.mashibing.listener;
|
||||||
|
|
||||||
|
import com.mashibing.config.RabbitMQConfig;
|
||||||
|
import com.mashibing.service.TBOrderService;
|
||||||
|
import com.rabbitmq.client.Channel;
|
||||||
|
import org.springframework.amqp.core.Message;
|
||||||
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class DelayMessageListener {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TBOrderService orderService;
|
||||||
|
|
||||||
|
@RabbitListener(queues = RabbitMQConfig.DEAD_QUEUE)
|
||||||
|
public void consume(String id, Channel channel, Message message) throws IOException {
|
||||||
|
//1、 调用Service实现订单状态的处理
|
||||||
|
orderService.delayCancelOrder(id);
|
||||||
|
|
||||||
|
//2、 ack的干活~
|
||||||
|
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.mashibing.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Insert;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
import org.apache.ibatis.annotations.Update;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
public interface TBOrderMapper {
|
||||||
|
|
||||||
|
@Insert("insert into tb_order (id) values (#{id})")
|
||||||
|
void save(@Param("id") String id);
|
||||||
|
|
||||||
|
@Select("select order_state from tb_order where id = #{id} for update")
|
||||||
|
int findOrderStateByIdForUpdate(@Param("id") String id);
|
||||||
|
|
||||||
|
@Update("update tb_order set order_state = #{orderState} where id = #{id}")
|
||||||
|
void updateOrderStateById(@Param("orderState") int i, @Param("id") String id);
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.mashibing.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
public interface TBOrderService {
|
||||||
|
|
||||||
|
void save();
|
||||||
|
|
||||||
|
void delayCancelOrder(String id);
|
||||||
|
}
|
Loading…
Reference in new issue