博客
关于我
八.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中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>
MySQL中的表是什么?
查看>>
mysql中穿件函数时候delimiter的用法
查看>>
Mysql中索引的分类、增删改查与存储引擎对应关系
查看>>
Mysql中索引的最左前缀原则图文剖析(全)
查看>>
MySql中给视图添加注释怎么添加_默认不支持_可以这样取巧---MySql工作笔记002
查看>>
Mysql中获取所有表名以及表名带时间字符串使用BetweenAnd筛选区间范围
查看>>
Mysql中视图的使用以及常见运算符的使用示例和优先级
查看>>
Mysql中触发器的使用示例
查看>>
Mysql中设置只允许指定ip能连接访问(可视化工具的方式)
查看>>
mysql中还有窗口函数?这是什么东西?
查看>>