博客
关于我
八.spring+rabbitmq
阅读量:200 次
发布时间:2019-02-28

本文共 4881 字,大约阅读时间需要 16 分钟。

Spring + RabbitMQ集成指南

一、Spring + RabbitMQ的集成

1. pom.xml配置

在项目根目录下的pom.xml文件中添加必要的依赖:

4.0.0
com.tiglle
spring-rabbitmq-main
0.0.1-SNAPSHOT
org.springframework.amqp
spring-rabbit
1.7.1.RELEASE
ch.qos.logback
logback-classic
1.2.1

2. 消息生产者(Producer.java)

package com.rabbit.producer.main;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.amqp.core.BindingBuilder;import org.springframework.amqp.core.Queue;import org.springframework.amqp.core.TopicExchange;import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;import org.springframework.amqp.rabbit.core.RabbitAdmin;import org.springframework.amqp.rabbit.core.RabbitTemplate;public class Producer {    private static Logger logger = LoggerFactory.getLogger(Producer.class);    public static void main(String[] args) {        ConnectionFactory cf = new CachingConnectionFactory("localhost");        RabbitAdmin admin = new RabbitAdmin(cf);        Queue queue = new Queue("myQueue");        admin.declareQueue(queue);        TopicExchange exchange = new TopicExchange("myExchange");        admin.declareExchange(exchange);        BindingBuilder bind = BindingBuilder.bind(queue).to(exchange).with("foo.*");        admin.declareBinding(bind);        RabbitTemplate template = new RabbitTemplate(cf);        template.convertAndSend("myExchange", "foo.bar", "Hello Tiglle");        logger.info("Producer发送消息到{}的exchange上, queueName={}, routingKey=foo.*", exchange.getName(), queue.getName());    }}

3. 消息消费者(Consumer.java)

package com.rabbit.consumer.main;import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;public class Consumer {    public static void main(String[] args) {        ConnectionFactory cf = new CachingConnectionFactory("localhost");        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);        Object listener = new Object() {            public void handleMessage(String foo) {                System.out.println(foo);            }        };        MessageListenerAdapter adapter = new MessageListenerAdapter(listener);        container.setMessageListener(adapter);        container.setQueueNames("myQueue");        container.start();    }}

4. 启动程序

运行Producer.javaConsumer.java,确保RabbitMQ服务已启动。

二、通过配置文件配置

1. pom.xml

4.0.0
com.tiglle
spring-rabbitmq
0.0.1-SNAPSHOT
org.springframework
spring-context
4.3.7.RELEASE
org.springframework.amqp
spring-rabbit
1.7.1.RELEASE
ch.qos.logback
logback-classic
1.2.1

2. 配置文件(applicationContext-rabbit.xml)

3. 消费者注入(Consumer.java)

package com.rabbit.consumer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;@Componentpublic class Consumer {    private static Logger logger = LoggerFactory.getLogger(Consumer.class);    public void consumerMessage(String message) {        logger.info("接收的消息为: {}", message);    }}

4. 启动程序(ProducerMain.java)

package com.rabbit.main;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.rabbit.consumer.Consumer;public class ProducerMain {    static Logger logger = LoggerFactory.getLogger(ProducerMain.class);    public static void main(String[] args) throws InterruptedException {        AbstractApplicationContext beans = new ClassPathXmlApplicationContext("applicationContext.xml");        RabbitTemplate rabbitTemplate = beans.getBean(RabbitTemplate.class);        rabbitTemplate.convertAndSend("hellow tiglle");        logger.info("发送的消息为: hellow tiglle");        Thread.sleep(1000);        beans.destroy();    }}

转载地址:http://bicj.baihongyu.com/

你可能感兴趣的文章
Nginx 结合 consul 实现动态负载均衡
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置https(一)—— 自签名证书
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>
Nginx 配置清单(一篇够用)
查看>>
Nginx 配置解析:从基础到高级应用指南
查看>>
nginx+php的搭建
查看>>
nginx+tomcat+memcached
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx之二:nginx.conf简单配置(参数详解)
查看>>
Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
查看>>
Nginx代理初探
查看>>
Nginx代理外网映射
查看>>
Nginx代理模式下 log-format 获取客户端真实IP
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>