demo结构截图
我这里采用了路由模式,在交换机和队列绑定的时候指定了routingkey
1.pom.xml导包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--spirngboot集成rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.编写RabbitMq配置类:
package com.baidu.rabbitmq.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitmqConfig {
public static final String QUEUE_EMAIL = "queue_email";
public static final String QUEUE_SMS = "queue_sms";
public static final String EXCHANGE_NFORM = "exchange_inform";
/**
* 交换机配置
* ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
*
* @return the exchange
*/
@Bean(EXCHANGE_NFORM)
public Exchange EXCHANGE_TOPICS_INFORM() {
//durable(true)持久化,消息队列重启后交换机仍然存在
return ExchangeBuilder.topicExchange(EXCHANGE_NFORM).durable(true).build();
}
//声明队列
@Bean(QUEUE_SMS)
public Queue QUEUE_INFORM_SMS() {
Queue queue = new Queue(QUEUE_SMS,true);
return queue;
}
//声明队列
@Bean(QUEUE_EMAIL)
public Queue QUEUE_INFORM_EMAIL() {
Queue queue = new Queue(QUEUE_EMAIL,true);
return queue;
}
/**
* channel.queueBind(INFORM_QUEUE_SMS,"inform_exchange_topic","inform.#.sms.#");
* 绑定队列到交换机 .
*
* @param queue the queue
* @param exchange the exchange
* @return the binding
*/
@Bean
public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_SMS) Queue queue,
@Qualifier(EXCHANGE_NFORM) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("sms").noargs();
}
@Bean
public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_EMAIL) Queue queue,
@Qualifier(EXCHANGE_NFORM) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("email").noargs();
}
}
3.消息生产者
package com.baidu.rabbitmq.send;
import com.baidu.rabbitmq.App;
import com.baidu.rabbitmq.config.RabbitmqConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = App.class)
@RunWith(SpringRunner.class)
public class SendMsg {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void testSendByTopics() {
String message = "牛牛牛!" ;
rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_NFORM, "sms", message);
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
4.消息接受者
package com.baidu.rabbitmq.reseive;
import com.baidu.rabbitmq.config.RabbitmqConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class ReceiveHandler {
//监听email队列
@RabbitListener(queues = {RabbitmqConfig.QUEUE_EMAIL})
public void receive_email(String msg, Message message, Channel channel) {
System.out.println(msg);
}
//监听sms队列
@RabbitListener(queues = {RabbitmqConfig.QUEUE_SMS})
public void receive_sms(String msg, Message message, Channel channel) {
System.out.println("消息ID:"+message.getMessageProperties().getDeliveryTag());
System.out.println("短信收到消息:"+message);
try {
channel.basicAck(message.getMessageProperties().getDeliveryTag() , false);//手动签收消息
} catch (IOException e) {
e.printStackTrace();
}
}
}