Spring集成消息队列RabbitMQ

本文介绍了RabbitMQ的基本概念,包括RabbitMQ的结构图及对象间的关系,并详细阐述了如何将RabbitMQ与Spring进行集成,包括发送消息的Producer实现和消费端Consumer的配置,展示了实际应用的效果。
摘要由CSDN通过智能技术生成

1. RabbitMQ简介

1.1. RabbitMQ

RabbitMQ是由Erlang(爱立信公司)语言开发,实现Advanced Message Queuing Protocol (AMQP高级消息队列协议)的消息中间件。消息中间件主要用于组件之间的解耦,消息的发送者无需知道消息使用者的存在,反之亦然。

1.2. 结构图

这里写图片描述
• Broker:消息队列服务器实体,例如RabbitMQ服务
• Vhost:虚拟主机,默认为“/”,一个broker里可以有多个vhost,区分不同用户权限,类似java的命令空间
• Connection:应用程序与broker连接,可有多个连接
• Channel:消息通道,connection中可建立多个channel,每个channel代表一个会话任务,所有操作都在channel中进行。
• Exchange:消息交换机,channel中可有多个,用于投递消息。应用程序发送消息时先把消息给交换机,由交换机投递给队列,不是直接给队列。
类型有三种:fanout(广播)、Direct(处理路由键,轮播实现)、Topic(支持消息模糊匹配)
• Queue:队列,用于存放消息
• Message:消息,应用程序需要发送的数据
• Bind:根据routingKey绑定exchange与queue规则,决定消息发送的方向

1.3. 对象间关系

这里写图片描述

2. rabbitMQ与spring集成

使用spring封装的rabbitmq的 https://github.com/spring-projects/spring-amqp 做集成。

2.1. 发送消息Producer

发送接口

public interface SimpleMQProducer {
   

    /**
     * 发送消息至MQ
     */
    public void sendDataToMQ(Object message); 

    /**
     * 发送消息至MQ
     */
    public void sendDataToMQ(Object message, String msgid); 

}

发送接口实现

public class SmartMQProducer implements InitializingBean,SimpleMQProducer{
   

    protected final Loggerx logger = Loggerx.getLogger("dao");

    protected RabbitTemplate rabbitTemplate = new RabbitTemplate();

    protected String queue;

    protected String exchange;

    protected String routingKey;

    protected ConnectionFactory connectionFactory;

    protected MessageConverter messageConverter;

    protected RetryTemplate retryTemplate;

    protected ConfirmCallback confirmCallback;

    protected ReturnCallback failedCallback;

    public RabbitTemplate getRabbitTemplate() {
        return rabbitTemplate;
    }

    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void setQueue(String queue) {
        this.queue = queue;
    }

    public void setExchange(String exchange) {
        this.exchange = exchange;
    }

    public void setRoutingKey(String routingKey) {
        this.routingKey = routingKey;
    }

    public void setConnectionFactory(ConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    public void setMessageConverter(MessageConverter messageConverter) {
        this.messageConverter = messageConverter;
    }

    public void setRetryTemplate(RetryTemplate retryTemplate) {
        this.retryTemplate = retryTemplate;
    }

    public void setConfirmCallback(ConfirmCallback confirmCallback) {
        this.confirmCallback = confirmCallback;
    }

    public void setFailedCallback(ReturnCallback failedCallback) {
        this.failedCallback = failedCallback;
    }

    @Override
    public void sendDataToMQ(Object message) {
        CorrelationData correlationId = null;
        try {
            correlationId = new CorrelationData(GUID.genTxNo(25));
        } catch (Exception e) {
            logger.error(LogType.EX, "产生消息id失败",e);
            correlationId = new CorrelationData(UUID.randomUUID().toString());
        }
        this.rabbitTemplate.convertAndSend(this.routingKey, message, correlationId);
        logger.info(LogType.EX, "发送到MQ的消息内容["+JsonUtil.toJSONString(message)+"],消息ID["+correlationId.getId()+"]");
    }

    @Override
    public void sendDataToMQ(Object message, String msgid) {
        CorrelationData correlationId = new CorrelationData(msgid);
        this.rabbitTemplate.convertAndSend(this.routingKey, message, correlationId);
        logger.info
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值