用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

公园    -  云代码空间

——

企业级 SpringBoot 教程 (十五)Springboot整合RabbitMQ

2019-03-07|476阅||

摘要:企业级 SpringBoot 教程 (十五)Springboot整合RabbitMQ

这篇文章带你了解怎么整合RabbitMQ服务器,并且通过它怎么去发送和接收消息。我将构建一个springboot工程,通过RabbitTemplate去通过MessageListenerAdapter去订阅一个POJO类型的消息。

准备工作

  • 15min
  • IDEA
  • maven 3.0

在开始构建项目之前,机器需要安装rabbitmq,你可以去官网下载,http://www.rabbitmq.com/download.html ,如果你是用的Mac(程序员都应该用mac吧),你可以这样下载:

brew install rabbitmq 

 

 

安装完成后开启服务器:

rabbitmq-server

开启服务器成功,你可以看到以下信息:

RabbitMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc. ##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/ ##  ## ##########  Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log ######  ##        /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log ########## Starting broker... completed with 6 plugins. 

 

 

构建工程

构架一个SpringBoot工程,其pom文件依赖加上spring-boot-starter-amqp的起步依赖:

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

 

 

创建消息接收者

在任何的消息队列程序中,你需要创建一个消息接收者,用于响应发送的消息。

@Component public class Receiver { private CountDownLatch latch = new CountDownLatch(1); public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    } public CountDownLatch getLatch() { return latch;
    }

} 

 

 

消息接收者是一个简单的POJO类,它定义了一个方法去接收消息,当你注册它去接收消息,你可以给它取任何的名字。其中,它有CountDownLatch这样的一个类,它是用于告诉发送者消息已经收到了,你不需要在应用程序中具体实现它,只需要latch.countDown()就行了。

创建消息监听,并发送一条消息

在spring程序中,RabbitTemplate提供了发送消息和接收消息的所有方法。你只需简单的配置下就行了:

  • 需要一个消息监听容器
  • 声明一个quene,一个exchange,并且绑定它们
  • 一个组件去发送消息

代码清单如下:

package com.forezp; import com.forezp.message.Receiver; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class SpringbootRabbitmqApplication { final static String queueName = "spring-boot"; @Bean Queue queue() { return new Queue(queueName, false);
    } @Bean TopicExchange exchange() { return new TopicExchange("spring-boot-exchange");
    } @Bean Binding binding(Queue queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(queueName);
    } @Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter); return container;
    } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage");
    } public static void main(String[] args) {
        SpringApplication.run(SpringbootRabbitmqApplication.class, args);
    }
} 

 

 

创建一个测试方法:

@Component public class Runner implements CommandLineRunner { private final RabbitTemplate rabbitTemplate; private final Receiver receiver; private final ConfigurableApplicationContext context; public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
            ConfigurableApplicationContext context) { this.receiver = receiver; this.rabbitTemplate = rabbitTemplate; this.context = context;
    } @Override public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
        context.close();
    }

} 

启动程序,你会发现控制台打印:
Spring Cloud大型企业分布式微服务云架构源码请加企鹅求求:yuncode.net

顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: 公园
    • 等级: 初级设计师
    • 积分: 2760
    • 代码: 0 个
    • 文章: 89 篇
    • 随想: 0 条
    • 访问: 10 次
    • 关注

    人气代码

      标签

      MVC(9)教程(4)Spring(5)Redis分布式缓存(1)spring,spr(1)mvc,web开发,(1)spring,spr(1)mvc,web开发,(1)Zookeeper集(1)restful,(2)kafka,(2)shiro,Spri(1)MVC,mybati(1)跟我学习dubbo,(1)J2ee分布式架构,(1)shiro(1)跟我学习dubbo-(1)跟我学习dubbo-(1)spring+spr(1)Spring4+Sp(1)DUBBO与ZOOK(1)【分享】微服务分布式(1)Springmvc+(1)JEESZ(1)RestFul服务介(1)J2EE分布式架构(3)dubbo+spri(1)基于redis分布式(1)SSM框架Sprin(1)分布式缓存Redis(1)Centos下单节点(1)JEESZ分布式框架(1)JEESZ分布式框架(1)分布式架构sprin(1)+mybatis(8)+shiro+(3)Activiti(3)分布式服务:spri(1)+(11)Dubbo+Zook(1)JEESZ-kafk(1)JEESZ-Zook(1)FastDFS安装、(1)FastDFS分布式(1)分布式服务:spri(1)Dubbo+Zook(1)分布式架构sprin(1)FastDFS分布式(1)FastDFS安装、(1)Maven启用代理访(1)如何从Maven远程(1)Maven安装配置(1)Maven本地资源库(1)使用Maven创建J(1)使用Maven创建W(1)Maven(2)POM(1)构建生命周期(1)SSM框架——详细整(1)JavaEE的13种(1)使用Maven构建和(1)Maven存储库(1)(一)构建dubbo(1)(二)构建dubbo(1)(三)构建dubbo(1)分布式框架简介SSM(1)springmvc+(1)Maven快照(1)Maven项目模板(1)Maven构建自动化(1)dubbo(4)springmvc(4)mybatis(2)java企业架构(1)SSM框架——Spr(1)分布式服务:spri(1)Dubbo+Zook(1)分布式服务:spri(1)Dubbo+Zook(1)springmvc+(1)分布式架构sprin(1)mvc配置(2)dbcp数据源+jd(1)详细介绍(1)(十三)(1)(十四)(1)(十五)(1)MVC原理(1)入门示例讲解(1)(十六)(1)(十七)(1)Springmvc+(1)j2ee分布式架构核(1)【分享】微服务分布式(1)Springmvc+(1)dbcp数据源+jd(1)DUBBO+SPRI(1)JEESZ分布式系统(1)构建dubbo分布式(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)构建dubbo分布式(1)Springmvc+(1)构建springmv(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)跟我学习dubbo-(1)构建dubbo分布式(1)构建dubbo分布式(1)【企业级框架整合】S(1)构建dubbo分布式(1)【推荐】微服务大型分(1)springmvc整(1)【分享】微服务分布式(1)Springmvc+(1)SpringBoot(4)企业级(4)(十七)上传文件(1)(十四)在sprin(1)(十五)Spring(1)(十六)用restT(1)

      站长推荐