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

你可能感兴趣的文章
Oil Deposits
查看>>
oj2894(贝尔曼福特模板)
查看>>
OJ4TH|Let's play a game
查看>>
OJ中处理超大数据的方法
查看>>
OJ中常见的一种presentation error解决方法
查看>>
OK335xS UART device registe hacking
查看>>
ok6410内存初始化
查看>>
OkDeepLink 使用教程
查看>>
OKHTTP
查看>>
Okhttp3添加拦截器后,报错,java.io.IOException: unexpected end of stream on okhttp3.Address
查看>>
OkHttp透明压缩,收获性能10倍,外加故障一枚
查看>>
OKR为什么到今天才突然火了?
查看>>
ol3 Demo2 ----地图搜索功能
查看>>
OLAP、OLTP的介绍和比较
查看>>
OLAP在大数据时代的挑战
查看>>
oldboy.16课
查看>>
OLEDB IMEX行数限制的问题
查看>>
ollama 如何删除本地模型文件?
查看>>
ollama-python-Python快速部署Llama 3等大型语言模型最简单方法
查看>>
Ollama怎么启动.gguf 大模型
查看>>