springboot整合各种消息队列(二):rabbitmq消息队列

栏目: Java · 发布时间: 6年前

内容简介:请参考笔者另一篇博客,有详细介绍:新建一个springboot项目,并且修改application.yml文件,pom如下:

springboot整合各种消息队列(二):rabbitmq消息队列 本篇博客将介绍使用rabbitmq消息中间件和springboot的整合使用;

安装配置rabbitmq

请参考笔者另一篇博客,有详细介绍: https://jsbintask.cn/2019/01/25/middleware/docker-rabbitmq-install/#more

整合springboot

新建项目

新建一个springboot项目,并且修改application.yml文件,pom如下:

spring:
  rabbitmq:
    host: yourhostaddress
    username: jsbintask
    password: jsbintask

host和password修改成自己的服务器用户名密码。pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.jsbintask</groupId>
    <artifactId>springboot-rabbitmq-learning</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-rabbitmq-learning</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

消息消费者(接收者)

编写一个消息消费者类:

@Component
@Log
public class RabbitmqMsgReceiver {
    @Autowired
    private CountDownLatch countDownLatch;

    public void receivedMsg(String msg) {
        log.info("received rabbitmq msg: " + msg);
        countDownLatch.countDown();
    }
}

并且加入@Component注解,将其作为bean归spring管理,并且通过@Autowried注入了一个CountdownLatch类。

配置exchange,queue,route,加入监听:

@Configuration
public class RabbitmqConfig {
    public static final String TOPIC_EXCHANGE_NAME = "jsbintask-exchange";
    public static final String ROUTE_KEY = "cn.jsbintask.key";

    private static final String QUEUE_NAME = "jsbintask-queue";


    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, false);
    }

    @Bean
    public CountDownLatch countDownLatch() {
        return new CountDownLatch(1);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(TOPIC_EXCHANGE_NAME);
    }

    /**
     * 将queue和exchange绑定,并且已 route_key暴漏出去
     */
    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTE_KEY);
    }

    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(QUEUE_NAME);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    public MessageListenerAdapter listenerAdapter(RabbitmqMsgReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receivedMsg");
    }
}

启动测试类,发送消息

@SpringBootApplication
@Log
public class SpringbootRabbitmqLearningApplication {

    public static void main(String[] args) throws Exception{
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootRabbitmqLearningApplication.class, args);
        CountDownLatch countDownLatch = context.getBean(CountDownLatch.class);
        RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);

        log.info("Sending msg....");
        rabbitTemplate.convertAndSend(RabbitmqConfig.TOPIC_EXCHANGE_NAME, RabbitmqConfig.ROUTE_KEY, "hello from jsbintask.");
        countDownLatch.await();

        System.exit(-1);
    }

}

启动,查看rabbitmq控制台以及idea控制台:收到消息并且打印:

springboot整合各种消息队列(二):rabbitmq消息队列 springboot整合各种消息队列(二):rabbitmq消息队列

这样rabitmq和springboot整合案例就完成了。 源码地址: https://github.com/jsbintask22/springboot-rabbitmq-learning.git

本文原创地址: https://jsbintask.cn/2019/01/25/springboot/springboot-rabbitmq-jms/ ,未经允许,禁止转载。

谢谢你支持我分享知识

springboot整合各种消息队列(二):rabbitmq消息队列

扫码打赏,心意已收

springboot整合各种消息队列(二):rabbitmq消息队列

打开 微信 扫一扫,即可进行扫码打赏哦


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

ActionScript 3.0精彩范例词典

ActionScript 3.0精彩范例词典

杨东昱 编 / 2008-5 / 59.00元

《ActionScript 3.0精彩范例词典》列出了最常用的ActionScript语法,并附有详细的程序代码范例,不但教您如何使用、修改ActionScript代码,而且还以实际范例和图解,说明每项语法还能呈现哪些动画效果和功能,对学习ActipScript有所帮助。读者在阅读《ActionScript 3.0精彩范例词典》之后,将能开发出属于自己的ActionScript程序与FLASH动画......一起来看看 《ActionScript 3.0精彩范例词典》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具