博客
关于我
八.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/

你可能感兴趣的文章
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>
npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
查看>>
npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
查看>>
npm—小记
查看>>
npm上传自己的项目
查看>>
npm介绍以及常用命令
查看>>
NPM使用前设置和升级
查看>>
npm入门,这篇就够了
查看>>
npm切换到淘宝源
查看>>