RabbitMQ-Spring集成
2018-03-11 22:56
330 查看
1.添加pom依赖
2.spring-rabbitmq配置文件
3.生产者
4.消费者
<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.7.5.RELEASE</version> </dependency>
2.spring-rabbitmq配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 1.定义RabbitMQ的连接工厂 --> <rabbit:connection-factory id="connectionFactory" host="10.102.150.244" port="5672" username="hzk" password="hzk" virtual-host="/vhost_hzk" /> <!-- 2.定义Rabbit模板,指定连接工厂以及定义exchange --> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="fanoutExchange" /> <!-- MQ的管理,包括队列、交换器 声明等 --> <rabbit:admin connection-factory="connectionFactory" /> <!-- 定义队列,自动声明 --> <rabbit:queue name="myQueue" auto-declare="true" durable="true"/> <!-- 定义交换器,自动声明 --> <rabbit:fanout-exchange name="fanoutExchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding queue="myQueue"/> </rabbit:bindings> </rabbit:fanout-exchange> <!-- 队列监听 --> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="recv" method="listen" queue-names="myQueue" /> </rabbit:listener-container> <!-- 消费者 --> <bean id="recv" class="com.ithzk.rabbitmq.spring.MyConsumer" /> </beans>
3.生产者
package com.ithzk.rabbitmq.spring; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author hzk * @date 2018/3/11 */ public class ProduceMain { public static void main(String[] args) throws InterruptedException { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-rabbitmq.xml"); //RabbitMQ模板 RabbitTemplate rabbitTemplate = ctx.getBean(RabbitTemplate.class); //发送消息 rabbitTemplate.convertAndSend("hello spring rabbit"); Thread.sleep(1000); //容器销毁 ctx.destroy(); } }
4.消费者
package com.ithzk.rabbitmq.spring; /** * @author hzk * @date 2018/3/11 */ public class MyConsumer { //具体执行业务的方法 public void listen(String msg){ System.out.println("recv " + msg); } }
相关文章推荐
- Spring boot集成Quartz与RabbitMQ
- rabbitMq集成Spring后,消费者设置手动ack,并且在业务上控制是否ack
- Spring集成RabbitMQ-使用RabbitMQ更方便
- 消息队列RabbitMQ与Spring集成
- rabbitMQ第五篇:Spring集成RabbitMQ
- rabbitMQ第五篇:Spring集成RabbitMQ
- Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)
- Spring Boot (十三)集成RabbitMQ
- RabbitMQ简介,安装,Spring集成 demo,以及使用(spring集成在最下面)
- Spring集成rabbitmq消息中间件
- Spring集成RabbitMQ并实现延迟队列
- Spring Boot 集成RabbitMQ入门Demo
- RabbitMq在spring boot中集成和应用
- Spring集成消息队列RabbitMQ(消息失败处理)
- spring-boot 集成 rabbitmq
- Spring 集成的 RabbitMQ 的练习
- springboot中rabbitmq集成——单项目
- springboot中rabbitmq集成——多项目
- RabbitMQ学习以及与Spring的集成(一)
- RabbitMQ第四篇:Spring集成RabbitMQ