Spring集成RabbitMQ
2018-02-01 17:29
316 查看
此套免费课程视频,本人录播到了腾讯课堂
更多请关注腾讯课堂牧码人或 登录网站http://www.51mmr.net
https://ke.qq.com/course/288116#tuin=5740604a
https://projects.spring.io/spring-amqp/
Spring对amqp的支持 目前只对了Rabbitmq做了实现,所以我们可以使用Rabbitmq简化我们的开发;
org.springframework.amqp
spring-rabbit
1.7.5.RELEASE
更多请关注腾讯课堂牧码人或 登录网站http://www.51mmr.net
https://ke.qq.com/course/288116#tuin=5740604a
简介
为了更好更快速的去开发Rabbitmq应用,spring封装client ;目的是简化我们的使用.https://projects.spring.io/spring-amqp/
Spring对amqp的支持 目前只对了Rabbitmq做了实现,所以我们可以使用Rabbitmq简化我们的开发;
开发集成
maven依赖org.springframework.amqp
spring-rabbit
1.7.5.RELEASE
生产者
public class SpringMain { public static void main(final String... args) throws Exception { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:context.xml"); //RabbitMQ模板 RabbitTemplate template = ctx.getBean(RabbitTemplate.class); //发送消息 template.convertAndSend("Hello, world!"); Thread.sleep(1000);// 休眠1秒 ctx.destroy(); //容器销毁 } }
消费者
public class MyConsumer { //具体执行业务的方法 public void listen(String foo) { System.out.println("消费者: " + foo); } }
Context.xml配置文件
<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.4.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="127.0.0.1" port="5672" username="user_mmr" password="admin" virtual-host="/vhost_mmr" /> <!—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="foo" method="listen" queue-names="myQueue" /> </rabbit:listener-container> <!-- 消费者 --> <bean id="foo" class="com.mmr.rabbitmq.spring.MyConsumer" /> </bean>
相关文章推荐
- Spring集成rabbitmq消息中间件
- spring boot集成rabbitmq的实例教程
- spring集成rabbitMQ
- springboot中rabbitmq集成——单项目
- RabbitMQ与spring集成,配置完整的生产者和消费者
- 消息队列RabbitMQ与Spring集成
- Spring Boot (十三)集成RabbitMQ
- 消息队列RabbitMQ与Spring集成
- Spring集成RabbitMQ并实现延迟队列
- 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】
- springboot(集成篇):RabbitMQ集成详解
- rabbitMq集成Spring后,消费者设置手动ack,并且在业务上控制是否ack
- rabbitMQ第五篇:Spring集成RabbitMQ
- Spring集成RabbitMQ
- SpringBoot实践之---集成RabbitMQ
- rabbitMq集成Spring后,消费者设置手动ack,并且在业务上控制是否ack
- RabbitMQ学习以及与Spring的集成(一)
- Spring集成rabbitMQ
- Spring集成RabbitMQ
- Java SpringBoot集成RabbitMq实战和总结