import com.example.delayedmsg.config.QueueConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class DlxConsumer { private static final Logger logger = LoggerFactory.getLogger(DlxConsumer.class); @RabbitListener(queues = QueueConfig.DLX_QUEUE_NAME) public void handle(String msg) { logger.info("handleMsg,{}", msg); } }
import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.HashMap; import java.util.Map; @Configuration public class QueueConfig { public static final String JAVABOY_QUEUE_NAME = "javaboy_queue_name"; public static final String JAVABOY_EXCHANGE_NAME = "javaboy_exchange_name"; public static final String JAVABOY_ROUTING_KEY = "javaboy_routing_key"; public static final String DLX_QUEUE_NAME = "dlx_queue_name"; public static final String DLX_EXCHANGE_NAME = "dlx_exchange_name"; public static final String DLX_ROUTING_KEY = "dlx_routing_key"; /** * 死信队列 * @return */ @Bean Queue dlxQueue() { return new Queue(DLX_QUEUE_NAME, true, false, false); } /** * 死信交换机 * @return */ @Bean DirectExchange dlxExchange() { return new DirectExchange(DLX_EXCHANGE_NAME, true, false); } /** * 绑定死信队列和死信交换机 * @return */ @Bean Binding dlxBinding() { return BindingBuilder.bind(dlxQueue()).to(dlxExchange()) .with(DLX_ROUTING_KEY); } /** * 普通消息队列 * @return */ @Bean Queue javaboyQueue() { Map<String, Object> args = new HashMap<>(); //设置消息过期时间 args.put("x-message-ttl", 1000*60*2); //设置死信交换机 args.put("x-dead-letter-exchange", DLX_EXCHANGE_NAME); //设置死信 routing_key args.put("x-dead-letter-routing-key", DLX_ROUTING_KEY); return new Queue(JAVABOY_QUEUE_NAME, true, false, false, args); } /** * 普通交换机 * @return */ @Bean DirectExchange javaboyExchange() { return new DirectExchange(JAVABOY_EXCHANGE_NAME, true, false); } /** * 绑定普通队列和与之对应的交换机 * @return */ @Bean Binding javaboyBinding() { return BindingBuilder.bind(javaboyQueue()) .to(javaboyExchange()) .with(JAVABOY_ROUTING_KEY); } }
spring.rabbitmq.host=localhost spring.rabbitmq.password=guest spring.rabbitmq.username=guest #spring.rabbitmq.virtual-host=/ spring.rabbitmq.port=5672
@SpringBootTest class DelayedMsgApplicationTests { @Autowired RabbitTemplate rabbitTemplate; @Test void contextLoads2() { System.out.println(new Date()); rabbitTemplate.convertAndSend(QueueConfig.JAVABOY_EXCHANGE_NAME, QueueConfig.JAVABOY_ROUTING_KEY, "hello javaboy!"); } }
标签:return,NAME,队列,mq,延时,org,import,DLX,public From: https://www.cnblogs.com/joyny/p/17191824.html