这一篇的主要是rabbitmq和springboot的整合
导入的依赖:
<!--加入rabbitmq队列--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>配置:
spring.application.name=springboot-rabbitmq spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.publisher-confirms=true spring.rabbitmq.virtual-host=/direct模式:
@Configuration public class SenderConf { //声明一个队列 direct模式队列 队列名完全匹配 所以不需要设置exchange之类的 @Bean public Queue direct() { return new Queue("queue", true);// true表示持久化该队列 } }生产者:
package com.yjp.rabbitmq; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * 发送消息 * * @Author : WenBao * Date : 17:24 2017/12/12 */ @Component public class HelloSender { //直接注入模板 springboot帮我们集成的模板 @Autowired private RabbitTemplate rabbitTemplate; public void send() { System.out.print("发送消息"); rabbitTemplate.convertAndSend("queue", "hello,rabbit"); } }消费者:
package com.yjp.rabbitmq; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class HelloReceive { @RabbitListener(queues = "queue") public void process(String str) { System.out.println("Receive:" + str); } }
测试类:
package springboot; import com.yjp.rabbitmq.HelloSender; import com.yjp.LoginApplication; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.jms.Destination; @RunWith(SpringRunner.class) /** * webEnvironment需要MVC的支持 * SpringBootTest.WebEnvironment.RANDOM_PORT 启动Servlet环境 端口号随机 */ @SpringBootTest(classes = LoginApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class LoginApplicationTests { @Autowired private HelloSender helloSender; @Test public void contextLoads() { helloSender.send(); } }topic模式:
配置bean:
package com.yjp.rabbitmq; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 创建队列 队列名为queue * * @Author : WenBao * Date : 17:23 2017/12/12 */ @Configuration public class SenderConf { //声明一个队列 direct模式队列 @Bean public Queue direct() { return new Queue("queue", true);// true表示持久化该队列 } //topic模式队列 @Bean public Queue topic1() { return new Queue("topic-1"); } //topic模式队列 @Bean public Queue topic2() { return new Queue("topic-2"); } // // //fanout模式队列 // @Bean // public Queue fanout1() { // return new Queue("fanout-1"); // } // // //fanout模式队列 // @Bean // public Queue fanout2() { // return new Queue("fanout-2"); // } //声明交换机 @Bean TopicExchange topicExchange() { return new TopicExchange("topicExchange"); } //绑定 将队列和exchange绑定 @Bean public Binding bindingTopic1() { return BindingBuilder.bind(topic1()).to(topicExchange()).with("123.#"); } @Bean public Binding bindingTopic2() { return BindingBuilder.bind(topic2()).to(topicExchange()).with("*.123"); } // // @Bean // public Binding binding2() { // return BindingBuilder.bind(queue2()).to(topicExchange()).with("key.#"); // } }
生产者:
package com.yjp.rabbitmq; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class TopicSender { @Autowired private RabbitTemplate rabbitTemplate; public void send() { System.err.println("发送消息"); //需要写上exchange的名字 routingkey 发送的消息 rabbitTemplate.convertAndSend("topicExchange", "123.123", "hello ni hao"); } }消费者 1:
package com.yjp.rabbitmq; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class TopicReceive1 { @RabbitListener(queues = "topic-1") public void getMessage(String message) { System.err.println("Topic1" + message); } }消费者 2:
package com.yjp.rabbitmq; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class TopicReceive2 { @RabbitListener(queues = "topic-2") public void getMessage(String message) { System.err.println("Topic2" + message); } }
测试类:
@Autowired private TopicSender topicSender;
@Test public void topic() { topicSender.send(); }fanout模式:
三种模式的配置都在
package com.yjp.rabbitmq; import org.springframework.amqp.core.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 创建队列 队列名为queue * * @Author : WenBao * Date : 17:23 2017/12/12 */ @Configuration public class SenderConf { //声明一个队列 direct模式队列 @Bean public Queue direct() { return new Queue("queue", true);// true表示持久化该队列 } //topic模式队列 @Bean public Queue topic1() { return new Queue("topic-1"); } //topic模式队列 @Bean public Queue topic2() { return new Queue("topic-2"); } //fanout模式队列 @Bean public Queue fanout1() { return new Queue("fanout-1"); } //fanout模式队列 @Bean public Queue fanout2() { return new Queue("fanout-2"); } //声明交换机 @Bean TopicExchange topicExchange() { return new TopicExchange("topicExchange"); } //绑定 将队列和exchange绑定 @Bean public Binding bindingTopic1() { return BindingBuilder.bind(topic1()).to(topicExchange()).with("123.#"); } @Bean public Binding bindingTopic2() { return BindingBuilder.bind(topic2()).to(topicExchange()).with("*.123"); } @Bean public FanoutExchange fanoutExchange() { return new FanoutExchange("fanoutExchange"); } @Bean public Binding bindingfanout1() { return BindingBuilder.bind(fanout1()).to(fanoutExchange()); } @Bean public Binding bindingfanout2() { return BindingBuilder.bind(fanout2()).to(fanoutExchange()); } }生产者:
package com.yjp.rabbitmq; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class FanoutSender { @Autowired private RabbitTemplate rabbitTemplate; public void sender() { System.err.println("消息发送"); //如果不写"" 那么fanoutExchange这个exchange会默认成为routingkey rabbitTemplate.convertAndSend("fanoutExchange", "", "你好"); } }消费者 1:
package com.yjp.rabbitmq; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class FanoutReceive1 { @RabbitListener(queues = "fanout-1") public void get(String message) { System.err.println(message); } }消费者 2:
package com.yjp.rabbitmq; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class FanoutReceive2 { @RabbitListener(queues = "fanout-2") public void get(String message) { System.err.println(message); } }
测试 所有的测试方法:
package springboot; import com.yjp.LoginApplication; import com.yjp.rabbitmq.FanoutSender; import com.yjp.rabbitmq.HelloSender; import com.yjp.rabbitmq.TopicSender; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) /** * webEnvironment需要MVC的支持 * SpringBootTest.WebEnvironment.RANDOM_PORT 启动Servlet环境 端口号随机 */ @SpringBootTest(classes = LoginApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class LoginApplicationTests { @Autowired private HelloSender helloSender; @Autowired private TopicSender topicSender; @Autowired private FanoutSender fanoutSender; @Test public void contextLoads() { helloSender.send(); } @Test public void topic() { topicSender.send(); } @Test public void fanout() { fanoutSender.sender(); } }
到此springb整合rabbitmq就结束了 下一篇就说一下rabbitmq的三种发送方式,以及和activemq的区别
努力吧 皮卡丘