SpringBoot + RabbitMQ 使用Demo
来源:互联网 发布:网盘源码系统 编辑:程序博客网 时间:2024/04/30 01:55
Spring-Boot 使用RabbitMQ
1.安装&运行 rabbitmq
- 安装服务器 :
brew install rabbitmq
- 运行Server:
rabbitmq-server
安装完之后:
- 应用端口:15672
- 服务端口:5672
如果没有此命令,可以去
/usr/local/Cellar/rabbitmq/3.6.6
目录下找rabbitmq-server命令(具体目录,安装的时候会有)
参考 rabbitmq 安装与配置
2. 搭建环境
2.1 引入包
compile('org.springframework.boot:spring-boot-starter-amqp')
2.2 建立接收者
import java.util.concurrent.CountDownLatch;import org.springframework.stereotype.Component;@Componentpublic 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; }}
2.3 建立监听者
import com.igouc.receiveMQ.Receiver;import org.springframework.amqp.core.*;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.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class ConfigMq { final static String QueueName = "test-rmq"; final static String ExchangeName = "test"; @Bean Queue queue(){ return new Queue(QueueName, false); } @Bean TopicExchange exchange(){ return new TopicExchange(ExchangeName); } @Bean Binding binding(Queue queue, TopicExchange topicExchange){ return BindingBuilder.bind(queue).to(topicExchange).with(ExchangeName); } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); } @Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(QueueName); container.setMessageListener(listenerAdapter); return container; }}
2.4 建立发送者
import com.igouc.receiveMQ.Receiver;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;@Componentpublic class SendMQ implements CommandLineRunner{ final static String QueueName = "test-rmq"; final static String ExchangeName = "test"; @Autowired private RabbitTemplate rabbitTemplate; @Autowired private Receiver receiver; @Override public void run(String... args) throws Exception { System.out.println("start send message ...."); rabbitTemplate.convertAndSend(QueueName, "Hello From SendMQ"); //像 receiver.getLatch().await(10000, TimeUnit.MILLISECONDS); }}
3. rabbitmq 中文文档:
RabbitMQ中文
0 0
- SpringBoot + RabbitMQ 使用Demo
- SpringBoot+rabbitMq的配置和使用Demo
- Springboot+RabbitMq
- rabbitmq demo
- 使用IDEA快速开发Springboot入门demo
- RabbitMQ在SpringBoot中使用的一些注意点
- SpringBoot--rabbitMq Producer
- SpringBoot项目实战--RabbitMQ
- springboot(八):RabbitMQ详解
- springboot(十二)整合rabbitmq
- springboot(八):RabbitMQ详解
- springboot(八):RabbitMQ详解
- SpringBoot集成RabbitMQ
- Springboot集成RabbitMq
- Springboot集成RabbitMQ
- SpringBoot ( 八 ) :RabbitMQ 详解
- springboot(八):RabbitMQ详解
- springboot实现rabbitmq
- Web.xml配置详解之context-param
- java基础 第三章 数组
- Single Number II
- SpringMVC非注解的处理映射器和适配器
- 2.Javascript语法-运算符(2)
- SpringBoot + RabbitMQ 使用Demo
- shell 脚本日期操作
- 复用类
- java web 常用框架
- 网络传输介质篇--大致了解常见的网络传输介质
- Java 字符串从后往前每隔三位添加逗号
- VTK的Reader与Writer
- PHP面向对象编程设计模式(二)单例模式
- java使用反射给对象属性赋值的两种方法