版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31463999/article/details/89788161
1.添加依赖
<dependencies>
<!-- springboot-web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加springboot对amqp的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
</dependencies>
2.添加配置类
/**
* 描述:交换机绑定队列
* @author: myx
* @date: 2019-05-01
* Copyright © 2019-grape. All rights reserved.
*/
@Component
public class FanoutConfig {
// 1.定义队列邮件
@Bean
public Queue fanOutEamilQueue() {
return new Queue(FanoutConstrant.QUEUE_EMAIL_NAME);
}
@Bean
public Queue fanOutSmsQueue() {
return new Queue(FanoutConstrant.QUEUE_SMS_NAME);
}
// 2.定义交换机
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange(FanoutConstrant.EXCHANGE_NAME);
}
// 3.队列与交换机绑定邮件队列
@Bean
Binding bindingExchangeEamil(Queue fanOutEamilQueue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(fanOutEamilQueue).to(fanoutExchange);
}
// 4.队列与交换机绑定短信队列
@Bean
Binding bindingExchangeSms(Queue fanOutSmsQueue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(fanOutSmsQueue).to(fanoutExchange);
}
}
3.生产者
/**
* 描述:生产者
* @author: myx
* @date: 2019-05-01
* Copyright © 2019-grape. All rights reserved.
*/
@Component
public class FanoutProducer {
@Autowired
private AmqpTemplate amqpTemplate;
/**
* 发送消息
* @param queueName 队列名称
*/
public void send(String queueName) {
String msg = "my_fanout_msg:" + new Date();
System.out.println(msg + ":" + msg);
amqpTemplate.convertAndSend(queueName, msg);
}
}
4.邮件消费者
/**
* 描述:邮件消费者
* @author: myx
* @date: 2019-05-01
* Copyright © 2019-grape. All rights reserved.
*/
@Component
@RabbitListener(queues = FanoutConstrant.QUEUE_EMAIL_NAME)
public class FanoutEamilConsumer {
@RabbitHandler
public void process(String msg){
System.out.println("邮件消费者获取生产者消息msg:" + msg);
//如果处理过程中出现错误,默认为一直消费数据(可以通过application.yml配置修改重试次数)
//int i=1/0;
}
}
5.短信消费者
/**
* 描述:短信消费者
* @author: myx
* @date: 2019-05-01
* Copyright © 2019-grape. All rights reserved.
*/
@Component
@RabbitListener(queues = FanoutConstrant.QUEUE_SMS_NAME)
public class FanoutSmsConsumer {
@RabbitHandler
public void process(String msg) {
System.out.println("短信消费者获取生产者消息msg:" + msg);
}
}
6.启动类
/**
* 描述:启动类
* @author: myx
* @date: 2019-05-01
* Copyright © 2019-grape. All rights reserved.
*/
@RestController
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
@Autowired
private FanoutProducer fanoutProducer;
@RequestMapping("/sendFanout")
public String sendFanout(String queueName) {
fanoutProducer.send(queueName);
return "success";
}
}
7.常量类
/**
* 描述:常量
* @author: myx
* @date: 2019-05-01
* Copyright © 2019-grape. All rights reserved.
*/
public class FanoutConstrant {
/**
* 队列名称
*/
public static final String QUEUE_EMAIL_NAME = "fanout_queue_email";
public static final String QUEUE_SMS_NAME = "fanout_queue_sms";
/**
* 交换机名称
*/
public static final String EXCHANGE_NAME = "fanout";
}
8.测试