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

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

一.spring+rabbitmq使用main方法集成

1.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.connection.ConnectionFactory;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) {        //获取一个连接工厂,用户默认是guest/guest(只能使用部署在本机的RabbitMQ)        //是Spring实现的对com.rabbitmq.client.Connection的包装        ConnectionFactory cf = new CachingConnectionFactory("localhost");        //对AMQP 0-9-1的实现        RabbitAdmin admin = new RabbitAdmin(cf);        //声明一个队列        Queue queue = new Queue("myQueue");        admin.declareQueue(queue);        //声明一个exchange类型为topic        TopicExchange exchange = new TopicExchange("myExchange");        admin.declareExchange(exchange);        //绑定队列到exchange,并指定routingKey为foo.*        admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("foo.*"));        //发送模版,设置上连接工厂        RabbitTemplate template = new RabbitTemplate(cf);        //发送消息        /**         * 1.String exchange:exchange的名称         * 2.String routingKey:routingKey的名称         * 3.Object message:要像exchange发送的消息         */        template.convertAndSend("myExchange", "foo.bar", "Hello Tiglle");        logger.info("Produce发送消息到"+exchange.getName()+"的exchange上,"                + "queueName="+queue.getName()+",routingKey=foo.*");    } }

3.Consumer.java:

package com.rabbit.comsumer.main;import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;import org.springframework.amqp.rabbit.connection.ConnectionFactory;import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;public class Comsumer {    public static void main(String[] args) {        //获取一个连接工厂,用户默认是guest/guest(只能使用部署在本机的RabbitMQ)        //是Spring实现的对com.rabbitmq.client.Connection的包装        ConnectionFactory cf = new CachingConnectionFactory("localhost");        //监听容器        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);        //监听者对象        Object listener = new Object() {            @SuppressWarnings("unused")            public void handleMessage(String foo) {                System.out.println(foo);            }        };       //通过这个适配器代理listener        MessageListenerAdapter adapter = new MessageListenerAdapter(listener);        //把适配器(listener)设置给Container        container.setMessageListener(adapter);        //设置该容器监听的队列名,可以传多个,public void setQueueNames(String... queueName)          container.setQueueNames("myQueue");        //开始监听         container.start();    }}

启动Procuder和Consumer可以成功发送接收消息

二.通过配置文件配置

1.pom.xml

4.0.0
com.tiglle
rabbitmq-spring
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.spring+rabbitmq配置文件:applicationContext-rabbit.xml

3.消费者,通过注解注入spring容器中的:Consumer.java

package com.rabbit.consumer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;//注入spring容器@Componentpublic class Consumer {   //配置文件raf的类    Logger logger = LoggerFactory.getLogger(Consumer.class);    //配置文件指定的消息处理的方法    public void consumerMessage(String message){        logger.info("接收的消息为:"+message);    }}

4.测试启动spring并发送消息的Mian方法: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 {        //启动spring容器,启动后消费者就会一直监听        AbstractApplicationContext beans = new ClassPathXmlApplicationContext("applicationContext.xml");        //假装是Autowrited的        //@Autowrited        RabbitTemplate rabbitTemplate = beans.getBean(RabbitTemplate.class);        //设置routingKey        rabbitTemplate.setRoutingKey("core.info");        //发送,exchange,routingKey都在配置文件中配置好了        rabbitTemplate.convertAndSend("hellow tiglle");        logger.info("发送的消息为:hellow tiglle");        //关闭掉spring容器        Thread.sleep(1000);        beans.destroy();    }}

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

你可能感兴趣的文章
Mysql 8.0 新特性
查看>>
MultCloud – 支持数据互传的网盘管理
查看>>
MySQL 8.0.23中复制架构从节点自动故障转移
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>