spring---消息订阅发布之RabbiteMQ
2016-06-03 14:33
459 查看
上一篇文章我们使用spring + redis实现消息的订阅发布,但是redis作为消息组件仅适合轻量级的任务处理,例如:秒杀计数器、缓存等,对于重量级,高并发的处理redis就稍显劣势。接下来我们spring+RabbiteMQ的实现。
准备
首先安装ErLang环境,因为RabbiteMQ依赖ErLang,这里我们使用windows环境,因为都有exe。
Erlang下载地址
RabbiteMQ下载地址
编码
首先添加依赖jar包:
接下来同上一篇文章一样添加消息接收类Receiver:
最后来看一下消息的发送Application:
最后一起看一下运行的结果:
参考:https://spring.io/guides
准备
首先安装ErLang环境,因为RabbiteMQ依赖ErLang,这里我们使用windows环境,因为都有exe。
Erlang下载地址
RabbiteMQ下载地址
编码
首先添加依赖jar包:
<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-amqp</artifactId> <version>1.5.4.RELEASE </version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId> spring-rabbit</artifactId> <version>1.5.4.RELEASE</version> </dependency>
接下来同上一篇文章一样添加消息接收类Receiver:
import java.util.concurrent.CountDownLatch; 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; } }
最后来看一下消息的发送Application:
package rabbitmq; import java.util.concurrent.TimeUnit; 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.core.RabbitTemplate; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import ch.qos.logback.core.Context; @SpringBootApplication public class Application implements CommandLineRunner { final static String queueName = "spring-boot"; @Autowired RabbitTemplate rabbitTemplate; @Bean Queue queue() { return new Queue(queueName, false); } @Bean AnnotationConfigApplicationContext context(){ return new AnnotationConfigApplicationContext(); } @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 Receiver receiver() { return new Receiver(); } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); } public static void main(String[] args) throws InterruptedException { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { System.out.println("Waiting five seconds..."); Thread.sleep(5000); System.out.println("Sending message..."); rabbitTemplate.convertAndSend(queueName, "Hello from RabbitMQ!"); receiver().getLatch().await(10000, TimeUnit.MILLISECONDS); } }
最后一起看一下运行的结果:
Sending message... Received <Hello from RabbitMQ!>
参考:https://spring.io/guides
相关文章推荐
- redis安装问题小结
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Rabbitmq集群搭建笔记
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 使用 Redis 和 Python 构建一个共享单车的应用程序
- Redis偶发连接失败案例实战记录
- Erlang项目内存泄漏分析方法
- Erlang实现的一个Web服务器代码实例
- Erlang并发编程介绍
- Erlang的一些编程技巧分享
- Redis中实现查找某个值的范围
- win 7 安装redis服务【笔记】
- redis的hGetAll函数的性能问题(记Redis那坑人的HGETALL)