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

cover


本篇博客将介绍使用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:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
         
    
    cn.jsbintask
    springboot-rabbitmq-learning
    0.0.1-SNAPSHOT
    springboot-rabbitmq-learning
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-amqp
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


消息消费者(接收者)

编写一个消息消费者类:

@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控制台:收到消息并且打印:

/rabbitmq

/rabbitmq

这样rabitmq和springboot整合案例就完成了。 源码地址: https://github.com/jsbintask22/springboot-rabbitmq-learning.git
本文原创地址:https://jsbintask.cn/2019/01/25/springboot/springboot-rabbitmq-jms/,未经允许,禁止转载。

你可能感兴趣的:(springboot整合各种消息队列(二):rabbitmq消息队列)