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

你可能感兴趣的文章
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>