RabbitMQ延迟消息实现

配置类

package com.changgou.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/****
 * @Author:gzy
 * @Description:
 * @Date 2019/9/5 10:50
 *****/
@Configuration
public class RabbitMQConfig {


    //延迟消息
    public static final String RELAY_QUEUE = "relay_queue";
    //交换机名称
    public static final String CANCEL_ORDER_PAY_EXCHANGE = "cancel_order_pay_exchange";

    //队列名称
    public static final String CANCEL_ORDER_QUEUE = "cancel_order_queue";

    //实例化延迟消息,将消息队列绑定到CANCEL_ORDER_PAY_EXCHANGE交换机
    @Bean
    public Queue relayQueue(){
        return QueueBuilder.durable(RELAY_QUEUE)
                .withArgument("x-dead-letter-exchange",CANCEL_ORDER_PAY_EXCHANGE)
                .withArgument("x-dead-letter-routing-key","")
                .build();
    }


    //接收延迟消息队列消息的队列
    @Bean
    public Queue cancelOrderQueue(){
        return new Queue(CANCEL_ORDER_QUEUE);
    }
    //实例化路由交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(CANCEL_ORDER_PAY_EXCHANGE);
    }
    //将普通队列绑定到交换机
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(cancelOrderQueue()).to(directExchange()).with("");
    }


}

向死信队列添加消息,等待5秒后消息发送给交换机

//给死信发消息
rabbitTemplate.convertAndSend(RabbitMQConfig.RELAY_QUEUE, (Object)order.getId(), new MessagePostProcessor() {
    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        message.getMessageProperties().setExpiration("5000");//5秒
        return message;
    }
});

接收消息

/**
     * 5分钟后  接收到消息
     * @param orderId
     */
    @RabbitListener(queues = {RabbitMQConfig.CANCEL_ORDER_QUEUE})
    public void cancelOrder(String orderId){
         System.out.println("接收到订单ID:" + orderId);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Key Features, Learn to administer, configure, and manage RabbitMQ instancesDiscover ways to secure and troubleshoot RabbitMQ instancesThis book is fully up-to-date with all the latest changes to version 3.5, Book Description, RabbitMQ is Open Source Message Queuing software based on the Advanced Message Queue Protocol Standard written in the Erlang Language. RabbitMQ is an ideal candidate for large-scale projects ranging from e-commerce and finance to Big Data and social networking because of its ease of use and high performance. Managing RabbitMQ in such a dynamic environment can be a challenging task that requires a good understanding not only of how to work properly with the message broker but also of its best practices and pitfalls., Learning RabbitMQ starts with a concise description of messaging solutions and patterns, then moves on to concrete practical scenarios for publishing and subscribing to the broker along with basic administration. This knowledge is further expanded by exploring how to establish clustering and high availability at the level of the message broker and how to integrate RabbitMQ with a number of technologies such as Spring, and enterprise service bus solutions such as MuleESB and WSO2. We will look at advanced topics such as performance tuning, secure messaging, and the internals of RabbitMQ. Finally we will work through case-studies so that we can see RabbitMQ in action and, if something goes wrong, we'll learn to resolve it in the Troubleshooting section., What you will learn, Apply messaging patterns using the message brokerAdminister RabbitMQ using the command line, management Web console, or management REST servicesCreate a cluster of scalable, and highly-available, RabbitMQ instancesUse RabbitMQ with the Spring Framework, MuleESB, WSO2, and Oracle databasesDeploy RabbitMQ using Puppet, Vagrant, or DockerFine-tune the performance of RabbitMQMonitor RabbitMQ using Nagios, Munin, or MonitSecure, troubleshoot, and extend RabbitMQ, About the Author, Martin Toshev is a software developer and Java enthusiast with more than eight years of experience and vast expertise originating from projects in areas such as enterprise Java, social networking, source code analysis, Internet of Things, and investment banking in companies such as Cisco and Deutsche Telekom. He is a graduate of computer science from the University of Sofia. He is also a certified Java professional (SCJP6) and a certified IBM cloud computing solution advisor. His areas of interest include a wide range of Java-related technologies (Servlets, JSP, JAXB, JAXP, JMS, JMX, JAX-RS, JAX-WS, Hibernate, Spring Framework, Liferay Portal, and Eclipse RCP), cloud computing technologies, cloud-based software architectures, enterprise application integration, and relational and NoSQL databases. Martin is one of the leaders of the Bulgarian Java Users group (BGJUG), a regular speaker at Java conferences, and one of the organizers behind the jPrime conference in Bulgaria (http://jprime.io/)., Table of Contents, Introducing RabbitMQDesign Patterns with RabbitMQAdministration, Confi guration, and ManagementClusteringHigh AvailabilityIntegrationsPerformance Tuning and MonitoringTroubleshootingSecurityInternalsContributing to RabbitMQ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值