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

你可能感兴趣的文章
MSSQL数据库查询优化(一)
查看>>
MSSQL数据库迁移到Oracle(二)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>
mt-datetime-picker type="date" 时间格式 bug
查看>>
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>