项目实践-Spring Boot实现RabbitMQ消息监听

项目实践-Spring Boot实现RabbitMQ消息监听

前言

**书山有路勤为径,学海无涯苦作舟**
记录程序员生活点点滴滴,希望记录的内容能帮助到努力爬山的各位伙伴!

标签:Rabbit MQ/消息监听

一、RabbitMQ概念

  • RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件),是应用层协议的一个开放标准。
  • RabbitMQ服务器是用Erlang语言编写的,是企业级消息代理软件。
  • RabbitMQ的应用场景是异步处理/应用解耦/流量削峰。
  • RabbitMQ客户端分为两种角色生产者(Producer )/消费者(Consumer )
  • AMQP四个概念:虚拟主机(virtual host),交换机(exchange),队列(queue)和绑定(binding)

二、SpringBoot集成Rabbit MQ

1.引入Maven依赖

	<dependency>
     	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-amqp</artifactId>
  	</dependency>

2.Rabbit MQ基本配置

mq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    virtualhost: /
    exchange: sync.target.queue.direct
    queue.fromGw: sync.target.queue

备注:

  • 在我的项目中Rabbit MQ配置信息放在bootstrap.yml文件中
  • queue.fromGw: sync.target.queue是本次监听的目标队列

三、实现消息监听

1.创建RabbitMQ配置类

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @author Miracle
 * @title: RabbitConfig
 * @projectName proxy
 * @description: 【MQ配置类】
 * @date 2021/6/719:07
 */
@Configuration
public class RabbitConfig {
    @Value("${spring.mq.queue.fromGw}")
    public String queue_fromGw;

    @Bean
    public Queue fromGw() {
        return new Queue(queue_fromGw);
    }
    
    @Bean
    ConnectionFactory connectionFactory(@Value("${spring.mq.port}") int port,
                                        @Value("${spring.mq.host}") String host,
                                        @Value("${spring.mq.username}") String userName,
                                        @Value("${spring.mq.password}") String password,
                                        @Value("${spring.mq.virtualhost}") String vhost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setVirtualHost(vhost);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    @Bean
    public SimpleMessageListenerContainer modbusMessageContainer(WaMingQueueListener receiver, ConnectionFactory connectionFactory) throws AmqpException, IOException {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueueNames(queue_fromGw);
        container.setExposeListenerChannel(true);
		//container.setAcknowledgeMode(AcknowledgeMode.MANUAL);//设置确认模式为手工确认
        container.setMessageListener(receiver);//监听处理类
        return container;
    }
}

2.创建监听实现类

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Miracle
 * @title: WaMingQueueListener
 * @projectName Proxy
 * @description: 【监听MQ】
 * @date 2021/6/719:10
 */
@Component
@Slf4j
public class WaMingQueueListener  implements ChannelAwareMessageListener {

    @Resource
    OperateMenuService OperateMenuService;

    /**
     * 监听MQ
     * @param message
     * @param channel
     * @throws Exception
     */
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        byte[] body = message.getBody();
        log.info("Listener RabbitMQ Msg :" + new String(body));
        // 实际业务处理
        ......
    }
}

结言

*登山路上的慕码人,理解不透的地方还请各位指点!*
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

观云卷云舒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值