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

你可能感兴趣的文章
NVelocity标签使用详解
查看>>
Nvidia Cudatoolkit 与 Conda Cudatoolkit
查看>>
NVIDIA GPU 的状态信息输出,由 `nvidia-smi` 命令生成
查看>>
NVIDIA-cuda-cudnn下载地址
查看>>
nvidia-htop 使用教程
查看>>
nvidia-smi 参数详解
查看>>
nvm安装以后,node -v npm 等命令提示不是内部或外部命令 node多版本控制管理 node多版本随意切换
查看>>
NYOJ 1066 CO-PRIME(数论)
查看>>
nyoj------203三国志
查看>>
nyoj58 最少步数
查看>>
OAuth2 + Gateway统一认证一步步实现(公司项目能直接使用),密码模式&授权码模式
查看>>
OAuth2 Provider 项目常见问题解决方案
查看>>
Vue.js 学习总结(14)—— Vue3 为什么推荐使用 ref 而不是 reactive
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>
OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
查看>>
OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
查看>>
OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
查看>>
OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
查看>>
OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
查看>>
OAuth2.0_授权服务配置_令牌服务和令牌端点配置_Spring Security OAuth2.0认证授权---springcloud工作笔记143
查看>>