作者 王勇

操作MQ动态创建删除交换机、队列

@@ -3,6 +3,11 @@ server: @@ -3,6 +3,11 @@ server:
3 3
4 # spring 配置 4 # spring 配置
5 spring: 5 spring:
  6 + security:
  7 + user:
  8 + name: admin
  9 + password: 123456
  10 +
6 application: 11 application:
7 name: message-bus-service 12 name: message-bus-service
8 13
@@ -58,11 +63,6 @@ spring: @@ -58,11 +63,6 @@ spring:
58 default-property-inclusion: ALWAYS 63 default-property-inclusion: ALWAYS
59 time-zone: GMT+8 64 time-zone: GMT+8
60 date-format: yyyy-MM-dd HH:mm:ss 65 date-format: yyyy-MM-dd HH:mm:ss
61 - # Spring Security配置  
62 - security:  
63 - user:  
64 - name: admin  
65 - password: 123456  
66 66
67 # zipkin 链路追踪配置 67 # zipkin 链路追踪配置
68 zipkin: 68 zipkin:
@@ -96,6 +96,7 @@ eureka: @@ -96,6 +96,7 @@ eureka:
96 user: 96 user:
97 name: "admin" 97 name: "admin"
98 password: "123456" 98 password: "123456"
  99 +
99 client: 100 client:
100 healthcheck: 101 healthcheck:
101 enabled: true 102 enabled: true
@@ -16,9 +16,5 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { @@ -16,9 +16,5 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
16 @Override 16 @Override
17 protected void configure(HttpSecurity http) throws Exception { 17 protected void configure(HttpSecurity http) throws Exception {
18 http.csrf().disable(); 18 http.csrf().disable();
19 -  
20 - //http.authorizeRequests().antMatchers("/","/bus/**").permitAll().and().csrf().disable();  
21 -  
22 } 19 }
23 } 20 }
24 -  
@@ -8,7 +8,9 @@ import com.sunyo.wlpt.message.bus.service.utils.IdUtils; @@ -8,7 +8,9 @@ import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
8 import org.springframework.web.bind.annotation.*; 8 import org.springframework.web.bind.annotation.*;
9 9
10 import javax.annotation.Resource; 10 import javax.annotation.Resource;
  11 +import java.io.IOException;
11 import java.util.List; 12 import java.util.List;
  13 +import java.util.concurrent.TimeoutException;
12 14
13 import static com.sunyo.wlpt.message.bus.service.common.Constant.EXIST_EXCHANGE_INFO; 15 import static com.sunyo.wlpt.message.bus.service.common.Constant.EXIST_EXCHANGE_INFO;
14 16
@@ -39,7 +41,8 @@ public class BusExchangeController { @@ -39,7 +41,8 @@ public class BusExchangeController {
39 @RequestParam(value = "exchangeName", required = false) String exchangeName, 41 @RequestParam(value = "exchangeName", required = false) String exchangeName,
40 @RequestParam(value = "virtualHostId", required = false) String virtualHostId, 42 @RequestParam(value = "virtualHostId", required = false) String virtualHostId,
41 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, 43 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
42 - @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) { 44 + @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)
  45 + {
43 // 获取查询参数 46 // 获取查询参数
44 BusExchange busExchange = BusExchange.builder().exchangeName(exchangeName).virtualHostId(virtualHostId).build(); 47 BusExchange busExchange = BusExchange.builder().exchangeName(exchangeName).virtualHostId(virtualHostId).build();
45 // 分页查询 48 // 分页查询
@@ -56,8 +59,8 @@ public class BusExchangeController { @@ -56,8 +59,8 @@ public class BusExchangeController {
56 * @return {@link ResultJson} 59 * @return {@link ResultJson}
57 */ 60 */
58 @DeleteMapping("/delete") 61 @DeleteMapping("/delete")
59 - public ResultJson deleteBusExchange(@RequestBody BusExchange busExchange) {  
60 - 62 + public ResultJson deleteBusExchange(@RequestBody BusExchange busExchange) throws IOException, TimeoutException
  63 + {
61 return busExchangeService.deleteByPrimaryKey(busExchange.getId()) > 0 64 return busExchangeService.deleteByPrimaryKey(busExchange.getId()) > 0
62 ? new ResultJson<>("200", "删除MQ交换机,成功") 65 ? new ResultJson<>("200", "删除MQ交换机,成功")
63 : new ResultJson<>("500", "删除MQ交换机,失败"); 66 : new ResultJson<>("500", "删除MQ交换机,失败");
@@ -70,8 +73,8 @@ public class BusExchangeController { @@ -70,8 +73,8 @@ public class BusExchangeController {
70 * @return {@link ResultJson} 73 * @return {@link ResultJson}
71 */ 74 */
72 @GetMapping("/batchRemove") 75 @GetMapping("/batchRemove")
73 - public ResultJson batchRemoveBusExchange(String ids) {  
74 - 76 + public ResultJson batchRemoveBusExchange(String ids) throws IOException, TimeoutException
  77 + {
75 return busExchangeService.deleteByPrimaryKey(ids) > 0 78 return busExchangeService.deleteByPrimaryKey(ids) > 0
76 ? new ResultJson<>("200", "批量删除MQ交换机,成功") 79 ? new ResultJson<>("200", "批量删除MQ交换机,成功")
77 : new ResultJson<>("500", "批量删除MQ交换机,失败"); 80 : new ResultJson<>("500", "批量删除MQ交换机,失败");
@@ -84,7 +87,8 @@ public class BusExchangeController { @@ -84,7 +87,8 @@ public class BusExchangeController {
84 * @return {@link ResultJson} 87 * @return {@link ResultJson}
85 */ 88 */
86 @PutMapping("/update") 89 @PutMapping("/update")
87 - public ResultJson updateBusExchange(@RequestBody BusExchange busExchange) { 90 + public ResultJson updateBusExchange(@RequestBody BusExchange busExchange)
  91 + {
88 92
89 // 先验证,修改好的核心信息(交换机名称)是否已存在 93 // 先验证,修改好的核心信息(交换机名称)是否已存在
90 String message = validateBusExchange(busExchange); 94 String message = validateBusExchange(busExchange);
@@ -102,7 +106,8 @@ public class BusExchangeController { @@ -102,7 +106,8 @@ public class BusExchangeController {
102 * @return {@link ResultJson} 106 * @return {@link ResultJson}
103 */ 107 */
104 @PostMapping("/insert") 108 @PostMapping("/insert")
105 - public ResultJson insertBusExchange(@RequestBody BusExchange busExchange) { 109 + public ResultJson insertBusExchange(@RequestBody BusExchange busExchange) throws IOException, TimeoutException
  110 + {
106 //先验证,增加的虚拟主机的核心信息(交换机名称)是否已存在 111 //先验证,增加的虚拟主机的核心信息(交换机名称)是否已存在
107 String message = validateBusExchange(busExchange); 112 String message = validateBusExchange(busExchange);
108 // 设置id 113 // 设置id
@@ -121,7 +126,8 @@ public class BusExchangeController { @@ -121,7 +126,8 @@ public class BusExchangeController {
121 * @param busExchange {@link BusExchange} 126 * @param busExchange {@link BusExchange}
122 * @return 通过,无返回消息 127 * @return 通过,无返回消息
123 */ 128 */
124 - private String validateBusExchange(BusExchange busExchange) { 129 + private String validateBusExchange(BusExchange busExchange)
  130 + {
125 // 判断交换机名称,是否为空 131 // 判断交换机名称,是否为空
126 if ("".equals(busExchange.getExchangeName()) || busExchange.getExchangeName() == null) { 132 if ("".equals(busExchange.getExchangeName()) || busExchange.getExchangeName() == null) {
127 return "该交换机信息中,没有交换机名称"; 133 return "该交换机信息中,没有交换机名称";
@@ -11,7 +11,9 @@ import io.netty.util.internal.StringUtil; @@ -11,7 +11,9 @@ import io.netty.util.internal.StringUtil;
11 import org.springframework.web.bind.annotation.*; 11 import org.springframework.web.bind.annotation.*;
12 12
13 import javax.annotation.Resource; 13 import javax.annotation.Resource;
  14 +import java.io.IOException;
14 import java.util.List; 15 import java.util.List;
  16 +import java.util.concurrent.TimeoutException;
15 17
16 import static com.sunyo.wlpt.message.bus.service.common.Constant.EXIST_QUEUE_INFO; 18 import static com.sunyo.wlpt.message.bus.service.common.Constant.EXIST_QUEUE_INFO;
17 19
@@ -67,9 +69,8 @@ public class BusQueueController { @@ -67,9 +69,8 @@ public class BusQueueController {
67 * @return {@link ResultJson} 69 * @return {@link ResultJson}
68 */ 70 */
69 @DeleteMapping("/delete") 71 @DeleteMapping("/delete")
70 - public ResultJson deleteBusQueue(@RequestBody BusQueue busQueue) 72 + public ResultJson deleteBusQueue(@RequestBody BusQueue busQueue) throws IOException, TimeoutException
71 { 73 {
72 -  
73 return busQueueService.deleteByPrimaryKey(busQueue.getId()) > 0 74 return busQueueService.deleteByPrimaryKey(busQueue.getId()) > 0
74 ? new ResultJson<>("200", "删除消息队列,成功") 75 ? new ResultJson<>("200", "删除消息队列,成功")
75 : new ResultJson<>("500", "删除消息队列,失败"); 76 : new ResultJson<>("500", "删除消息队列,失败");
@@ -82,9 +83,8 @@ public class BusQueueController { @@ -82,9 +83,8 @@ public class BusQueueController {
82 * @return {@link ResultJson} 83 * @return {@link ResultJson}
83 */ 84 */
84 @GetMapping("/batchRemove") 85 @GetMapping("/batchRemove")
85 - public ResultJson batchRemoveBusQueue(String ids) 86 + public ResultJson batchRemoveBusQueue(String ids) throws IOException, TimeoutException
86 { 87 {
87 -  
88 return busQueueService.deleteByPrimaryKey(ids) > 0 88 return busQueueService.deleteByPrimaryKey(ids) > 0
89 ? new ResultJson<>("200", "批量删除消息队列,成功") 89 ? new ResultJson<>("200", "批量删除消息队列,成功")
90 : new ResultJson<>("500", "批量删除消息队列,失败"); 90 : new ResultJson<>("500", "批量删除消息队列,失败");
@@ -115,7 +115,7 @@ public class BusQueueController { @@ -115,7 +115,7 @@ public class BusQueueController {
115 * @return {@link ResultJson} 115 * @return {@link ResultJson}
116 */ 116 */
117 @PostMapping("/insert") 117 @PostMapping("/insert")
118 - public ResultJson insertBusQueue(@RequestBody BusQueue busQueue) 118 + public ResultJson insertBusQueue(@RequestBody BusQueue busQueue) throws IOException, TimeoutException
119 { 119 {
120 //先验证,增加的虚拟主机的核心信息(交换机名称)是否已存在 120 //先验证,增加的虚拟主机的核心信息(交换机名称)是否已存在
121 String message = validateBusQueue(busQueue); 121 String message = validateBusQueue(busQueue);
@@ -3,10 +3,8 @@ package com.sunyo.wlpt.message.bus.service.controller; @@ -3,10 +3,8 @@ package com.sunyo.wlpt.message.bus.service.controller;
3 import com.sunyo.wlpt.message.bus.service.domain.XmlData; 3 import com.sunyo.wlpt.message.bus.service.domain.XmlData;
4 import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType; 4 import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType;
5 import com.sunyo.wlpt.message.bus.service.rabbit.utils.DirectUtils; 5 import com.sunyo.wlpt.message.bus.service.rabbit.utils.DirectUtils;
6 -import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils;  
7 import com.sunyo.wlpt.message.bus.service.response.ResultJson; 6 import com.sunyo.wlpt.message.bus.service.response.ResultJson;
8 import com.sunyo.wlpt.message.bus.service.service.AsyncTaskService; 7 import com.sunyo.wlpt.message.bus.service.service.AsyncTaskService;
9 -import com.sunyo.wlpt.message.bus.service.service.MessageNoteService;  
10 import com.sunyo.wlpt.message.bus.service.service.UserMessageBindingService; 8 import com.sunyo.wlpt.message.bus.service.service.UserMessageBindingService;
11 import com.sunyo.wlpt.message.bus.service.utils.XmlUtils; 9 import com.sunyo.wlpt.message.bus.service.utils.XmlUtils;
12 import lombok.extern.slf4j.Slf4j; 10 import lombok.extern.slf4j.Slf4j;
@@ -36,12 +34,6 @@ public class RabbitController { @@ -36,12 +34,6 @@ public class RabbitController {
36 private XmlUtils xmlUtils; 34 private XmlUtils xmlUtils;
37 35
38 @Resource 36 @Resource
39 - private MessageNoteService messageNoteService;  
40 -  
41 - @Resource  
42 - private RabbitUtils rabbitUtils;  
43 -  
44 - @Resource  
45 private DirectUtils directUtils; 37 private DirectUtils directUtils;
46 38
47 @Resource 39 @Resource
1 -package com.sunyo.wlpt.message.bus.service.rabbit.config;  
2 -  
3 -import lombok.extern.slf4j.Slf4j;  
4 -import org.springframework.amqp.core.Message;  
5 -import org.springframework.amqp.rabbit.connection.CorrelationData;  
6 -import org.springframework.amqp.rabbit.core.RabbitTemplate;  
7 -  
8 -import javax.annotation.Resource;  
9 -  
10 -/**  
11 - * @author 子诚  
12 - * Description:开启消息可靠性(confirm和return机制)  
13 - * 时间:2020/7/20 9:30  
14 - */  
15 -@Slf4j  
16 -//@Component  
17 -public class PublisherConfirmAndReturnConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {  
18 -  
19 - @Resource  
20 - private RabbitTemplate rabbitTemplate;  
21 -  
22 - /**  
23 - * 初始化的方法,设置当前上下文(下面的两个方法),为confirm和return机制的回滚事件  
24 - */  
25 -// @PostConstruct  
26 - public void initMethod() {  
27 - rabbitTemplate.setConfirmCallback(this);  
28 - rabbitTemplate.setReturnCallback(this);  
29 - }  
30 -  
31 - @Override  
32 - public void confirm(CorrelationData correlationData, boolean ack, String cause) {  
33 - // 根据手动ack来确定  
34 - if (ack) {  
35 - log.info("消息发送至exchange,成功");  
36 - } else {  
37 - log.error("消息发送至exchange,失败");  
38 - }  
39 - }  
40 -  
41 - @Override  
42 - public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {  
43 - log.error("消息从exchange分发到queue,失败");  
44 - }  
45 -}  
1 -package com.sunyo.wlpt.message.bus.service.rabbit.test;  
2 -  
3 -import com.rabbitmq.client.Channel;  
4 -import lombok.extern.slf4j.Slf4j;  
5 -import org.springframework.amqp.core.Message;  
6 -import org.springframework.beans.factory.annotation.Autowired;  
7 -import org.springframework.data.redis.core.StringRedisTemplate;  
8 -import org.springframework.stereotype.Component;  
9 -  
10 -import java.io.IOException;  
11 -import java.util.concurrent.TimeUnit;  
12 -  
13 -  
14 -/**  
15 - * @author 子诚  
16 - * Description:测试-消息-消费者  
17 - * 时间:2020/7/20 11:09  
18 - */  
19 -@Slf4j  
20 -@Component  
21 -public class TestConsumer {  
22 -  
23 - @Autowired  
24 - private StringRedisTemplate redisTemplate;  
25 -  
26 -  
27 -// @RabbitListener(queues = "Q_test")  
28 - public void getMessage(String msg, Channel channel, Message message) throws IOException {  
29 - try {  
30 - // 0、获取messageId  
31 - String messageId = (String) message.getMessageProperties().getHeaders().get("spring_returned_message_correlation");  
32 - // 1、设置key到redis,设置缓存时间为10秒  
33 - if (redisTemplate.opsForValue().setIfAbsent(messageId, "0", 10, TimeUnit.SECONDS)) {  
34 - // 2、消费消息  
35 - log.info("成功消费了消息:" + msg);  
36 - // 3、设置key的value为1  
37 - redisTemplate.opsForValue().set(messageId, "1", 10, TimeUnit.SECONDS);  
38 - // 4、手动ack  
39 - channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);  
40 - } else {  
41 - // 获取redis中的value,如果是1,就手动ack。如果是0,就什么也不做(是0代表着,正在被消费中)  
42 - if ("1".equalsIgnoreCase(redisTemplate.opsForValue().get(messageId))) {  
43 - // 手动ack,第一个参数是所确认消息的标识,第二参数是是否批量确认  
44 - channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);  
45 - }  
46 - }  
47 -  
48 -  
49 - /**  
50 - * 设置Qos机制  
51 - * 第一个参数:单条消息的大小(0表示即无限制)  
52 - * 第二个参数:每次处理消息的数量  
53 - * 第三个参数:是否为consumer级别(false表示仅当前channel有效)  
54 - */  
55 -  
56 - // channel.basicQos(0, 1, false);  
57 - } catch (Exception e) {  
58 - channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);  
59 - log.error("消息ID:" + message.getMessageProperties().getHeaders().get("spring_returned_message_correlation"));  
60 - log.error("接收消息发送错误:" + e.getMessage());  
61 - }  
62 -  
63 - }  
64 -}  
1 -package com.sunyo.wlpt.message.bus.service.rabbit.test;  
2 -  
3 -import com.sunyo.wlpt.message.bus.service.utils.IdUtils;  
4 -import lombok.extern.slf4j.Slf4j;  
5 -import org.springframework.amqp.rabbit.connection.CorrelationData;  
6 -import org.springframework.amqp.rabbit.core.RabbitTemplate;  
7 -import org.springframework.stereotype.Component;  
8 -  
9 -import javax.annotation.Resource;  
10 -import java.io.IOException;  
11 -  
12 -/**  
13 - * @author 子诚  
14 - * Description:测试-消息-生产者  
15 - * 时间:2020/7/20 11:08  
16 - */  
17 -@Slf4j  
18 -@Component  
19 -public class TestProduct {  
20 -  
21 - @Resource  
22 - private RabbitTemplate rabbitTemplate;  
23 -  
24 - /**  
25 - * @param exchangeName 交换机名称  
26 - * @param routingKeyName 路由键名称  
27 - * @param msg 发送的消息  
28 - */  
29 - public void sentMessage(String exchangeName, String routingKeyName, String msg) throws IOException {  
30 - CorrelationData messageId = new CorrelationData(IdUtils.generateId());  
31 - rabbitTemplate.convertAndSend(exchangeName, routingKeyName, msg, messageId);  
32 - log.info("成功发送消息-> {};到交换机->{};路由键为 ->{};", msg, exchangeName, routingKeyName);  
33 -  
34 - }  
35 -  
36 -}  
  1 +package com.sunyo.wlpt.message.bus.service.rabbit.utils;
  2 +
  3 +import com.sunyo.wlpt.message.bus.service.domain.BusExchange;
  4 +import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
  5 +import com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding;
  6 +import lombok.extern.slf4j.Slf4j;
  7 +import org.springframework.amqp.core.*;
  8 +import org.springframework.amqp.rabbit.core.RabbitTemplate;
  9 +import org.springframework.cloud.context.config.annotation.RefreshScope;
  10 +import org.springframework.stereotype.Component;
  11 +
  12 +import javax.annotation.Resource;
  13 +
  14 +/**
  15 + * @author 子诚
  16 + * Description:
  17 + * 时间:2020/7/16 16:32
  18 + */
  19 +@Slf4j
  20 +@RefreshScope
  21 +@Component
  22 +public class BootRabbitUtils {
  23 + @Resource
  24 + private AmqpAdmin amqpAdmin;
  25 +
  26 + @Resource
  27 + private RabbitTemplate rabbitTemplate;
  28 +
  29 + /**
  30 + * 创建交换机(交换机名称,是否持久化,是否删除)
  31 + *
  32 + * @param busExchange {@link BusExchange}
  33 + */
  34 + public void createExchange(BusExchange busExchange) {
  35 +
  36 + // 类型-直连路由
  37 + String type_direct = "direct";
  38 + // 类型-动态路由
  39 + String type_topic = "topic";
  40 + // 类型-广播
  41 + String type_fanout = "fanout";
  42 + // 类型-头部
  43 + String type_headers = "headers";
  44 +
  45 + // 创建交换机,直连接类型
  46 + if (type_direct.equals(busExchange.getExchangeType())) {
  47 + amqpAdmin.declareExchange(
  48 + new DirectExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
  49 + );
  50 + log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_direct);
  51 + }
  52 + // 创建交换机,扇形交换机
  53 + if (type_topic.equals(busExchange.getExchangeType())) {
  54 + amqpAdmin.declareExchange(
  55 + new TopicExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
  56 + );
  57 + log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_topic);
  58 + }
  59 + // 创建交换机,广播(主题)交换机
  60 + if (type_fanout.equals(busExchange.getExchangeType())) {
  61 + amqpAdmin.declareExchange(
  62 + new FanoutExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
  63 + );
  64 + log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_fanout);
  65 + }
  66 + // 创建交换机,首部交换机
  67 + if (type_headers.equals(busExchange.getExchangeType())) {
  68 + amqpAdmin.declareExchange(
  69 + new HeadersExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
  70 + );
  71 + log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_headers);
  72 + }
  73 + }
  74 +
  75 + /**
  76 + * 创建交换机,通过 exchangeName 创建
  77 + */
  78 + public void createExchange(String exchangeName) {
  79 + amqpAdmin.declareExchange(
  80 + new DirectExchange(exchangeName)
  81 + );
  82 + log.info("创建了交换机:{};类型:{};", exchangeName, "DirectExchange");
  83 + }
  84 +
  85 + /**
  86 + * 根据交换机名称,删除虚拟机
  87 + *
  88 + * @param exchangeName 交换机名称
  89 + */
  90 + public void deleteExchange(String exchangeName) {
  91 + boolean flag = amqpAdmin.deleteExchange(exchangeName);
  92 + }
  93 +
  94 + /**
  95 + * 创建队列
  96 + *
  97 + * @param busQueue {@link BusQueue}
  98 + */
  99 + public void createQueue(BusQueue busQueue) {
  100 + amqpAdmin.declareQueue(
  101 + new Queue(busQueue.getQueueName(), busQueue.getDurability(), false, busQueue.getAutoDelete())
  102 + );
  103 + log.info("创建了队列,队列名称->{}", busQueue.getQueueName());
  104 + }
  105 +
  106 + /**
  107 + * 创建队列
  108 + *
  109 + * @param queueName 队列名称
  110 + */
  111 + public void createQueue(String queueName) {
  112 + amqpAdmin.declareQueue(
  113 + new Queue(queueName)
  114 + );
  115 + log.info("创建了队列,队列名称->{}", queueName);
  116 + }
  117 +
  118 + /**
  119 + * 删除队列,根据队列名称
  120 + *
  121 + * @param queueName 队列名称
  122 + */
  123 + public void deleteQueue(String queueName) {
  124 + boolean flag = amqpAdmin.deleteQueue(queueName);
  125 + }
  126 +
  127 + /**
  128 + * 创建绑定关系
  129 + *
  130 + * @param userMessageBinding {@link UserMessageBinding}
  131 + */
  132 + public void createBing(UserMessageBinding userMessageBinding) {
  133 + amqpAdmin.declareBinding(
  134 + new Binding(userMessageBinding.getQueueName(), Binding.DestinationType.QUEUE, userMessageBinding.getExchangeName(), userMessageBinding.getRoutingKeyName(), null)
  135 + );
  136 + log.info("创建了绑定关系,交换机->{};队列->{};路由键->{}", userMessageBinding.getQueueName(), userMessageBinding.getExchangeName(), userMessageBinding.getRoutingKeyName());
  137 + }
  138 +
  139 + /**
  140 + * 创建绑定关系
  141 + *
  142 + * @param exchangeName 交换机名称
  143 + * @param queueName 队列名称
  144 + * @param routingKeyName 路由键名称
  145 + */
  146 + public void createBing(String exchangeName, String queueName, String routingKeyName) {
  147 + amqpAdmin.declareBinding(
  148 + new Binding(queueName, Binding.DestinationType.QUEUE, exchangeName, routingKeyName, null)
  149 + );
  150 + log.info("创建了绑定关系,交换机->{};队列->{};路由键->{}", exchangeName, queueName, routingKeyName);
  151 + }
  152 +}
1 package com.sunyo.wlpt.message.bus.service.rabbit.utils; 1 package com.sunyo.wlpt.message.bus.service.rabbit.utils;
2 2
3 -import com.sunyo.wlpt.message.bus.service.domain.BusExchange;  
4 -import com.sunyo.wlpt.message.bus.service.domain.BusQueue;  
5 -import com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding; 3 +import com.rabbitmq.client.AMQP;
  4 +import com.rabbitmq.client.Channel;
  5 +import com.rabbitmq.client.Connection;
  6 +import com.rabbitmq.client.ConnectionFactory;
  7 +import com.sunyo.wlpt.message.bus.service.domain.*;
  8 +import com.sunyo.wlpt.message.bus.service.service.BusServerService;
  9 +import com.sunyo.wlpt.message.bus.service.service.VirtualHostService;
6 import lombok.extern.slf4j.Slf4j; 10 import lombok.extern.slf4j.Slf4j;
7 -import org.springframework.amqp.core.*;  
8 -import org.springframework.amqp.rabbit.core.RabbitTemplate;  
9 -import org.springframework.cloud.context.config.annotation.RefreshScope; 11 +import org.springframework.beans.factory.annotation.Value;
10 import org.springframework.stereotype.Component; 12 import org.springframework.stereotype.Component;
11 13
12 import javax.annotation.Resource; 14 import javax.annotation.Resource;
  15 +import java.io.IOException;
  16 +import java.util.concurrent.TimeoutException;
13 17
14 /** 18 /**
15 * @author 子诚 19 * @author 子诚
16 * Description: 20 * Description:
17 - * 时间:2020/7/16 16:32 21 + * 时间:2020/7/30 17:21
18 */ 22 */
19 @Slf4j 23 @Slf4j
20 -@RefreshScope  
21 @Component 24 @Component
22 public class RabbitUtils { 25 public class RabbitUtils {
  26 +
23 @Resource 27 @Resource
24 - private AmqpAdmin amqpAdmin; 28 + private VirtualHostService virtualHostService;
25 29
26 @Resource 30 @Resource
27 - private RabbitTemplate rabbitTemplate; 31 + private BusServerService busServerService;
  32 +
  33 + @Value("${spring.rabbitmq.host}")
  34 + private String host;
  35 +
  36 + @Value("${spring.rabbitmq.port}")
  37 + private int port;
  38 +
  39 + @Value("${spring.rabbitmq.username}")
  40 + private String username;
  41 +
  42 + @Value("${spring.rabbitmq.password}")
  43 + private String password;
  44 +
  45 + @Value("${spring.rabbitmq.virtual-host}")
  46 + private String vHost;
28 47
29 /** 48 /**
30 - * 创建交换机(交换机名称,是否持久化,是否删除) 49 + * 关闭连接与通道
31 * 50 *
32 - * @param busExchange {@link BusExchange} 51 + * @param channel 通道
  52 + * @param conn 连接
33 */ 53 */
34 - public void createExchange(BusExchange busExchange) {  
35 -  
36 - // 类型-直连路由  
37 - String type_direct = "direct";  
38 - // 类型-动态路由  
39 - String type_topic = "topic";  
40 - // 类型-广播  
41 - String type_fanout = "fanout";  
42 - // 类型-头部  
43 - String type_headers = "headers";  
44 -  
45 - // 创建交换机,直连接类型  
46 - if (type_direct.equals(busExchange.getExchangeType())) {  
47 - amqpAdmin.declareExchange(  
48 - new DirectExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())  
49 - );  
50 - log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_direct); 54 + public static void closeConnectionAndChanel(Channel channel, Connection conn)
  55 + {
  56 + try {
  57 + if (channel != null) {
  58 + channel.close();
51 } 59 }
52 - // 创建交换机,扇形交换机  
53 - if (type_topic.equals(busExchange.getExchangeType())) {  
54 - amqpAdmin.declareExchange(  
55 - new TopicExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())  
56 - );  
57 - log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_topic); 60 + if (conn != null) {
  61 + conn.close();
58 } 62 }
59 - // 创建交换机,广播(主题)交换机  
60 - if (type_fanout.equals(busExchange.getExchangeType())) {  
61 - amqpAdmin.declareExchange(  
62 - new FanoutExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())  
63 - );  
64 - log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_fanout); 63 + } catch (Exception e) {
  64 + e.printStackTrace();
65 } 65 }
66 - // 创建交换机,首部交换机  
67 - if (type_headers.equals(busExchange.getExchangeType())) {  
68 - amqpAdmin.declareExchange(  
69 - new HeadersExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())  
70 - );  
71 - log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_headers);  
72 } 66 }
  67 +
  68 + /**
  69 + * 获取 rabbitMq 的连接,重载
  70 + */
  71 + public Connection getConnection() throws IOException, TimeoutException
  72 + {
  73 + // 定义连接工厂
  74 + ConnectionFactory factory = new ConnectionFactory();
  75 + // 设置服务地址
  76 + factory.setHost(host);
  77 + // 端口,amqp协议 端口 类似与mysql的3306
  78 + factory.setPort(port);
  79 + // 设置账号信息,用户名、密码、vhost
  80 + factory.setVirtualHost(vHost);
  81 + factory.setUsername(username);
  82 + factory.setPassword(password);
  83 + // 通过工程获取连接
  84 + Connection connection = factory.newConnection();
  85 + return connection;
73 } 86 }
74 87
75 /** 88 /**
76 - * 创建交换机,通过 exchangeName 创建 89 + * 获取 rabbitMq 的连接,重载
77 */ 90 */
78 - public void createExchange(String exchangeName) {  
79 - amqpAdmin.declareExchange(  
80 - new DirectExchange(exchangeName)  
81 - );  
82 - log.info("创建了交换机:{};类型:{};", exchangeName, "DirectExchange"); 91 + public Connection getConnection(String virtualHostName) throws IOException, TimeoutException
  92 + {
  93 + ConnectionFactory factory = new ConnectionFactory();
  94 + factory.setHost(host);
  95 + factory.setPort(port);
  96 + factory.setVirtualHost(virtualHostName);
  97 + factory.setUsername(username);
  98 + factory.setPassword(password);
  99 + Connection connection = factory.newConnection();
  100 + return connection;
83 } 101 }
84 102
85 /** 103 /**
86 - * 根据交换机名称,删除虚拟机  
87 - *  
88 - * @param exchangeName 交换机名称 104 + * 获取 rabbitMq 的连接,重载
89 */ 105 */
90 - public void deleteExchange(String exchangeName) {  
91 - boolean flag = amqpAdmin.deleteExchange(exchangeName); 106 + public Connection getConnection(String serverIp, Integer serverPort, String virtualHostName)
  107 + throws IOException, TimeoutException
  108 + {
  109 + ConnectionFactory factory = new ConnectionFactory();
  110 + factory.setHost(serverIp);
  111 + factory.setPort(serverPort);
  112 + factory.setVirtualHost(virtualHostName);
  113 + factory.setUsername(username);
  114 + factory.setPassword(password);
  115 + Connection connection = factory.newConnection();
  116 + return connection;
92 } 117 }
93 118
94 /** 119 /**
95 - * 创建队列 120 + * 获取 rabbitMq 的连接,重载
96 * 121 *
97 - * @param busQueue {@link BusQueue} 122 + * @param hostIp 服务器ip
  123 + * @param hostPort 服务器端口号
  124 + * @param vHostName 虚拟主机名称
  125 + * @param userName 用户名
  126 + * @param password 密码
  127 + * @return
  128 + * @throws Exception
98 */ 129 */
99 - public void createQueue(BusQueue busQueue) {  
100 - amqpAdmin.declareQueue(  
101 - new Queue(busQueue.getQueueName(), busQueue.getDurability(), false, busQueue.getAutoDelete())  
102 - );  
103 - log.info("创建了队列,队列名称->{}", busQueue.getQueueName()); 130 + public static Connection getConnection(String hostIp, int hostPort, String vHostName, String userName, String password)
  131 + throws Exception
  132 + {
  133 + ConnectionFactory factory = new ConnectionFactory();
  134 + factory.setHost(hostIp);
  135 + factory.setPort(hostPort);
  136 + factory.setVirtualHost(vHostName);
  137 + factory.setUsername(userName);
  138 + factory.setPassword(password);
  139 + return factory.newConnection();
104 } 140 }
105 141
106 /** 142 /**
107 - * 创建队列  
108 - *  
109 - * @param queueName 队列名称 143 + * 添加交换机
110 */ 144 */
111 - public void createQueue(String queueName) {  
112 - amqpAdmin.declareQueue(  
113 - new Queue(queueName)  
114 - );  
115 - log.info("创建了队列,队列名称->{}", queueName); 145 + public void createExchange(String serverIp, Integer serverPort, String virtualHostName, BusExchange busExchange)
  146 + throws IOException, TimeoutException
  147 + {
  148 + Connection connection = getConnection(serverIp, serverPort, virtualHostName);
  149 + Channel channel = connection.createChannel();
  150 + AMQP.Exchange.DeclareOk declareOk = channel.exchangeDeclare(busExchange.getExchangeName(), busExchange.getExchangeType(), busExchange.getDurability(),
  151 + busExchange.getAutoDelete(), busExchange.getInternal(), null);
  152 + log.info("创建交换机的返回值<----->" + declareOk);
  153 + closeConnectionAndChanel(channel, connection);
116 } 154 }
117 155
118 /** 156 /**
119 - * 删除队列,根据队列名称  
120 - *  
121 - * @param queueName 队列名称 157 + * 删除交换机 channel.exchangeDelete(exchangeName);
  158 + */
  159 + public void removeExchange(String serverIp, Integer serverPort, String virtualHostName, BusExchange busExchange)
  160 + throws IOException, TimeoutException
  161 + {
  162 + Connection connection = getConnection(serverIp, serverPort, virtualHostName);
  163 + Channel channel = connection.createChannel();
  164 + channel.exchangeDelete(busExchange.getExchangeName());
  165 + closeConnectionAndChanel(channel, connection);
  166 + }
  167 +
  168 + /**
  169 + * 添加队列(默认设置参数为null)
  170 + */
  171 + public void createQueue(String serverIp, Integer serverPort, String virtualHostName, BusQueue busQueue)
  172 + throws IOException, TimeoutException
  173 + {
  174 + Connection connection = getConnection(serverIp, serverPort, virtualHostName);
  175 + Channel channel = connection.createChannel();
  176 + channel.queueDeclare(busQueue.getQueueName(), busQueue.getDurability(), false, busQueue.getAutoDelete(), null);
  177 + closeConnectionAndChanel(channel, connection);
  178 + }
  179 +
  180 + /**
  181 + * 删除队列 channel.queueDelete(queueName);
122 */ 182 */
123 - public void deleteQueue(String queueName) {  
124 - boolean flag = amqpAdmin.deleteQueue(queueName); 183 + public void removeQueue(String serverIp, Integer serverPort, String virtualHostName, BusQueue busQueue)
  184 + throws IOException, TimeoutException
  185 + {
  186 + Connection connection = getConnection(serverIp, serverPort, virtualHostName);
  187 + Channel channel = connection.createChannel();
  188 + channel.queueDelete(busQueue.getQueueName());
  189 + closeConnectionAndChanel(channel, connection);
125 } 190 }
126 191
127 /** 192 /**
128 * 创建绑定关系 193 * 创建绑定关系
  194 + */
  195 + public void createBinding(String serverIp, Integer serverPort, String virtualHostName, UserMessageBinding userMessageBinding)
  196 + throws IOException, TimeoutException
  197 + {
  198 + Connection connection = getConnection(serverIp, serverPort, virtualHostName);
  199 + Channel channel = connection.createChannel();
  200 + }
  201 +
  202 + /**
  203 + * 清空队列 channel.queuePurge(queueName);
  204 + */
  205 +
  206 + /**
  207 + * 解除绑定 channel.queueUnbind("queueName", "exchangeName","routingKey");
  208 + */
  209 +
  210 + /**
  211 + * 前往创建交换机的路上
  212 + */
  213 + public void toCreateExchange(BusExchange busExchange) throws IOException, TimeoutException
  214 + {
  215 + VirtualHost virtualHost = getVirtualHost(busExchange.getVirtualHostId());
  216 + BusServer busServer = getBusServer(virtualHost.getServerId());
  217 + createExchange(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), busExchange);
  218 + }
  219 +
  220 + /**
  221 + * 前往删除交换机的路上
  222 + */
  223 + public void toRemoveExchange(BusExchange busExchange) throws IOException, TimeoutException
  224 + {
  225 + VirtualHost virtualHost = getVirtualHost(busExchange.getVirtualHostId());
  226 + BusServer busServer = getBusServer(virtualHost.getServerId());
  227 + removeExchange(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), busExchange);
  228 + }
  229 +
  230 + /**
  231 + * 前往创建队列的路上
  232 + */
  233 + public void toCreateQueue(BusQueue BusQueue) throws IOException, TimeoutException
  234 + {
  235 + VirtualHost virtualHost = getVirtualHost(BusQueue.getVirtualHostId());
  236 + BusServer busServer = getBusServer(virtualHost.getServerId());
  237 + createQueue(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), BusQueue);
  238 + }
  239 +
  240 + /**
  241 + * 前往删除队列的路上
  242 + */
  243 + public void toRemoveQueue(BusQueue BusQueue) throws IOException, TimeoutException
  244 + {
  245 + VirtualHost virtualHost = getVirtualHost(BusQueue.getVirtualHostId());
  246 + BusServer busServer = getBusServer(virtualHost.getServerId());
  247 + removeQueue(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), BusQueue);
  248 + }
  249 +
  250 + /**
  251 + * 根据虚拟主机id,获取虚拟主机信息
129 * 252 *
130 - * @param userMessageBinding {@link UserMessageBinding} 253 + * @param virtualHostId 虚拟主机id
  254 + * @return {@link VirtualHost}
131 */ 255 */
132 - public void createBing(UserMessageBinding userMessageBinding) {  
133 - amqpAdmin.declareBinding(  
134 - new Binding(userMessageBinding.getQueueName(), Binding.DestinationType.QUEUE, userMessageBinding.getExchangeName(), userMessageBinding.getRoutingKeyName(), null)  
135 - );  
136 - log.info("创建了绑定关系,交换机->{};队列->{};路由键->{}", userMessageBinding.getQueueName(), userMessageBinding.getExchangeName(), userMessageBinding.getRoutingKeyName()); 256 + public VirtualHost getVirtualHost(String virtualHostId)
  257 + {
  258 + return virtualHostService.selectByPrimaryKey(virtualHostId);
137 } 259 }
138 260
139 /** 261 /**
140 - * 创建绑定关系 262 + * 根据服务器id,获取服务器信息
141 * 263 *
142 - * @param exchangeName 交换机名称  
143 - * @param queueName 队列名称  
144 - * @param routingKeyName 路由键名称  
145 - */  
146 - public void createBing(String exchangeName, String queueName, String routingKeyName) {  
147 - amqpAdmin.declareBinding(  
148 - new Binding(queueName, Binding.DestinationType.QUEUE, exchangeName, routingKeyName, null)  
149 - );  
150 - log.info("创建了绑定关系,交换机->{};队列->{};路由键->{}", exchangeName, queueName, routingKeyName); 264 + * @param serverId 服务器id
  265 + * @return {@link BusServer}
  266 + */
  267 + public BusServer getBusServer(String serverId)
  268 + {
  269 + return busServerService.selectByPrimaryKey(serverId);
151 } 270 }
152 } 271 }
@@ -24,4 +24,5 @@ public class AsyncTaskService { @@ -24,4 +24,5 @@ public class AsyncTaskService {
24 // 无论消息是否发送成功,将消息存储于数据库 24 // 无论消息是否发送成功,将消息存储于数据库
25 messageNoteService.insertMessageSelective(sentData); 25 messageNoteService.insertMessageSelective(sentData);
26 } 26 }
  27 +
27 } 28 }
@@ -3,7 +3,9 @@ package com.sunyo.wlpt.message.bus.service.service; @@ -3,7 +3,9 @@ package com.sunyo.wlpt.message.bus.service.service;
3 import com.github.pagehelper.PageInfo; 3 import com.github.pagehelper.PageInfo;
4 import com.sunyo.wlpt.message.bus.service.domain.BusExchange; 4 import com.sunyo.wlpt.message.bus.service.domain.BusExchange;
5 5
  6 +import java.io.IOException;
6 import java.util.List; 7 import java.util.List;
  8 +import java.util.concurrent.TimeoutException;
7 9
8 /** 10 /**
9 * @author 子诚 11 * @author 子诚
@@ -19,7 +21,7 @@ public interface BusExchangeService { @@ -19,7 +21,7 @@ public interface BusExchangeService {
19 * @param id primaryKey 21 * @param id primaryKey
20 * @return deleteCount 22 * @return deleteCount
21 */ 23 */
22 - int deleteByPrimaryKey(String id); 24 + int deleteByPrimaryKey(String id) throws IOException, TimeoutException;
23 25
24 /** 26 /**
25 * 新增 27 * 新增
@@ -35,7 +37,7 @@ public interface BusExchangeService { @@ -35,7 +37,7 @@ public interface BusExchangeService {
35 * @param record the record 37 * @param record the record
36 * @return insert count 38 * @return insert count
37 */ 39 */
38 - int insertSelective(BusExchange record); 40 + int insertSelective(BusExchange record) throws IOException, TimeoutException;
39 41
40 /** 42 /**
41 * 查询,根据主键 43 * 查询,根据主键
@@ -3,7 +3,9 @@ package com.sunyo.wlpt.message.bus.service.service; @@ -3,7 +3,9 @@ package com.sunyo.wlpt.message.bus.service.service;
3 import com.github.pagehelper.PageInfo; 3 import com.github.pagehelper.PageInfo;
4 import com.sunyo.wlpt.message.bus.service.domain.BusQueue; 4 import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
5 5
  6 +import java.io.IOException;
6 import java.util.List; 7 import java.util.List;
  8 +import java.util.concurrent.TimeoutException;
7 9
8 /** 10 /**
9 * @author 子诚 11 * @author 子诚
@@ -18,7 +20,7 @@ public interface BusQueueService { @@ -18,7 +20,7 @@ public interface BusQueueService {
18 * @param id primaryKey 20 * @param id primaryKey
19 * @return deleteCount 21 * @return deleteCount
20 */ 22 */
21 - int deleteByPrimaryKey(String id); 23 + int deleteByPrimaryKey(String id) throws IOException, TimeoutException;
22 24
23 /** 25 /**
24 * 新增 26 * 新增
@@ -34,7 +36,7 @@ public interface BusQueueService { @@ -34,7 +36,7 @@ public interface BusQueueService {
34 * @param record the record 36 * @param record the record
35 * @return insert count 37 * @return insert count
36 */ 38 */
37 - int insertSelective(BusQueue record); 39 + int insertSelective(BusQueue record) throws IOException, TimeoutException;
38 40
39 /** 41 /**
40 * 查询,根据主键 42 * 查询,根据主键
@@ -4,13 +4,16 @@ import com.github.pagehelper.PageHelper; @@ -4,13 +4,16 @@ import com.github.pagehelper.PageHelper;
4 import com.github.pagehelper.PageInfo; 4 import com.github.pagehelper.PageInfo;
5 import com.sunyo.wlpt.message.bus.service.domain.BusExchange; 5 import com.sunyo.wlpt.message.bus.service.domain.BusExchange;
6 import com.sunyo.wlpt.message.bus.service.mapper.BusExchangeMapper; 6 import com.sunyo.wlpt.message.bus.service.mapper.BusExchangeMapper;
  7 +import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils;
7 import com.sunyo.wlpt.message.bus.service.service.BusExchangeService; 8 import com.sunyo.wlpt.message.bus.service.service.BusExchangeService;
8 import org.springframework.stereotype.Service; 9 import org.springframework.stereotype.Service;
9 import org.springframework.transaction.annotation.Propagation; 10 import org.springframework.transaction.annotation.Propagation;
10 import org.springframework.transaction.annotation.Transactional; 11 import org.springframework.transaction.annotation.Transactional;
11 12
12 import javax.annotation.Resource; 13 import javax.annotation.Resource;
  14 +import java.io.IOException;
13 import java.util.List; 15 import java.util.List;
  16 +import java.util.concurrent.TimeoutException;
14 17
15 /** 18 /**
16 * @author 子诚 19 * @author 子诚
@@ -23,9 +26,12 @@ public class BusExchangeServiceImpl implements BusExchangeService { @@ -23,9 +26,12 @@ public class BusExchangeServiceImpl implements BusExchangeService {
23 @Resource 26 @Resource
24 private BusExchangeMapper busExchangeMapper; 27 private BusExchangeMapper busExchangeMapper;
25 28
  29 + @Resource
  30 + RabbitUtils rabbitUtils;
  31 +
26 @Override 32 @Override
27 @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) 33 @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
28 - public int deleteByPrimaryKey(String id) 34 + public int deleteByPrimaryKey(String id) throws IOException, TimeoutException
29 { 35 {
30 // 判断删除的个数,需被删除的个数是否一致 36 // 判断删除的个数,需被删除的个数是否一致
31 int index = 0; 37 int index = 0;
@@ -34,7 +40,9 @@ public class BusExchangeServiceImpl implements BusExchangeService { @@ -34,7 +40,9 @@ public class BusExchangeServiceImpl implements BusExchangeService {
34 if (id.contains(splitItem)) { 40 if (id.contains(splitItem)) {
35 String[] split = id.split(splitItem); 41 String[] split = id.split(splitItem);
36 for (int i = 0; i < split.length; i++) { 42 for (int i = 0; i < split.length; i++) {
  43 + BusExchange busExchange = selectByPrimaryKey(split[i]);
37 int num = busExchangeMapper.deleteByPrimaryKey(split[i]); 44 int num = busExchangeMapper.deleteByPrimaryKey(split[i]);
  45 + deleteExchange(busExchange);
38 if (num > 0) { 46 if (num > 0) {
39 index = index + num; 47 index = index + num;
40 } 48 }
@@ -45,8 +53,19 @@ public class BusExchangeServiceImpl implements BusExchangeService { @@ -45,8 +53,19 @@ public class BusExchangeServiceImpl implements BusExchangeService {
45 return 0; 53 return 0;
46 } 54 }
47 } else { 55 } else {
48 - return busExchangeMapper.deleteByPrimaryKey(id); 56 + BusExchange busExchange = selectByPrimaryKey(id);
  57 + int num = busExchangeMapper.deleteByPrimaryKey(id);
  58 + deleteExchange(busExchange);
  59 + return num;
  60 + }
49 } 61 }
  62 +
  63 + /**
  64 + * 删除MQ服务器上的交换机
  65 + */
  66 + public void deleteExchange(BusExchange busExchange) throws IOException, TimeoutException
  67 + {
  68 + rabbitUtils.toRemoveExchange(busExchange);
50 } 69 }
51 70
52 @Override 71 @Override
@@ -56,8 +75,9 @@ public class BusExchangeServiceImpl implements BusExchangeService { @@ -56,8 +75,9 @@ public class BusExchangeServiceImpl implements BusExchangeService {
56 } 75 }
57 76
58 @Override 77 @Override
59 - public int insertSelective(BusExchange record) 78 + public int insertSelective(BusExchange record) throws IOException, TimeoutException
60 { 79 {
  80 + rabbitUtils.toCreateExchange(record);
61 return busExchangeMapper.insertSelective(record); 81 return busExchangeMapper.insertSelective(record);
62 } 82 }
63 83
@@ -4,6 +4,7 @@ import com.github.pagehelper.PageHelper; @@ -4,6 +4,7 @@ import com.github.pagehelper.PageHelper;
4 import com.github.pagehelper.PageInfo; 4 import com.github.pagehelper.PageInfo;
5 import com.sunyo.wlpt.message.bus.service.domain.BusQueue; 5 import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
6 import com.sunyo.wlpt.message.bus.service.mapper.BusQueueMapper; 6 import com.sunyo.wlpt.message.bus.service.mapper.BusQueueMapper;
  7 +import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils;
7 import com.sunyo.wlpt.message.bus.service.service.BusQueueService; 8 import com.sunyo.wlpt.message.bus.service.service.BusQueueService;
8 import io.netty.util.internal.StringUtil; 9 import io.netty.util.internal.StringUtil;
9 import org.springframework.stereotype.Service; 10 import org.springframework.stereotype.Service;
@@ -11,8 +12,10 @@ import org.springframework.transaction.annotation.Propagation; @@ -11,8 +12,10 @@ import org.springframework.transaction.annotation.Propagation;
11 import org.springframework.transaction.annotation.Transactional; 12 import org.springframework.transaction.annotation.Transactional;
12 13
13 import javax.annotation.Resource; 14 import javax.annotation.Resource;
  15 +import java.io.IOException;
14 import java.util.ArrayList; 16 import java.util.ArrayList;
15 import java.util.List; 17 import java.util.List;
  18 +import java.util.concurrent.TimeoutException;
16 19
17 /** 20 /**
18 * @author 子诚 21 * @author 子诚
@@ -25,9 +28,13 @@ public class BusQueueServiceImpl implements BusQueueService { @@ -25,9 +28,13 @@ public class BusQueueServiceImpl implements BusQueueService {
25 @Resource 28 @Resource
26 private BusQueueMapper busQueueMapper; 29 private BusQueueMapper busQueueMapper;
27 30
  31 + @Resource
  32 + private RabbitUtils rabbitUtils;
  33 +
  34 +
28 @Override 35 @Override
29 @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) 36 @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
30 - public int deleteByPrimaryKey(String id) 37 + public int deleteByPrimaryKey(String id) throws IOException, TimeoutException
31 { 38 {
32 // 判断删除的个数,需被删除的个数是否一致 39 // 判断删除的个数,需被删除的个数是否一致
33 int index = 0; 40 int index = 0;
@@ -36,7 +43,9 @@ public class BusQueueServiceImpl implements BusQueueService { @@ -36,7 +43,9 @@ public class BusQueueServiceImpl implements BusQueueService {
36 if (id.contains(splitItem)) { 43 if (id.contains(splitItem)) {
37 String[] split = id.split(splitItem); 44 String[] split = id.split(splitItem);
38 for (int i = 0; i < split.length; i++) { 45 for (int i = 0; i < split.length; i++) {
  46 + BusQueue busQueue = selectByPrimaryKey(split[i]);
39 int num = busQueueMapper.deleteByPrimaryKey(split[i]); 47 int num = busQueueMapper.deleteByPrimaryKey(split[i]);
  48 + deleteQueue(busQueue);
40 if (num > 0) { 49 if (num > 0) {
41 index = index + num; 50 index = index + num;
42 } 51 }
@@ -47,8 +56,19 @@ public class BusQueueServiceImpl implements BusQueueService { @@ -47,8 +56,19 @@ public class BusQueueServiceImpl implements BusQueueService {
47 return 0; 56 return 0;
48 } 57 }
49 } else { 58 } else {
50 - return busQueueMapper.deleteByPrimaryKey(id); 59 + BusQueue busQueue = selectByPrimaryKey(id);
  60 + int num = busQueueMapper.deleteByPrimaryKey(id);
  61 + deleteQueue(busQueue);
  62 + return num;
  63 + }
51 } 64 }
  65 +
  66 + /**
  67 + * 删除MQ服务器上的队列
  68 + */
  69 + public void deleteQueue(BusQueue busQueue) throws IOException, TimeoutException
  70 + {
  71 + rabbitUtils.toRemoveQueue(busQueue);
52 } 72 }
53 73
54 @Override 74 @Override
@@ -58,8 +78,9 @@ public class BusQueueServiceImpl implements BusQueueService { @@ -58,8 +78,9 @@ public class BusQueueServiceImpl implements BusQueueService {
58 } 78 }
59 79
60 @Override 80 @Override
61 - public int insertSelective(BusQueue record) 81 + public int insertSelective(BusQueue record) throws IOException, TimeoutException
62 { 82 {
  83 + rabbitUtils.toCreateQueue(record);
63 return busQueueMapper.insertSelective(record); 84 return busQueueMapper.insertSelective(record);
64 } 85 }
65 86
@@ -102,7 +123,7 @@ public class BusQueueServiceImpl implements BusQueueService { @@ -102,7 +123,7 @@ public class BusQueueServiceImpl implements BusQueueService {
102 List<BusQueue> list = new ArrayList<>(); 123 List<BusQueue> list = new ArrayList<>();
103 String userIds = busQueue.getUserId(); 124 String userIds = busQueue.getUserId();
104 String splitItem = ","; 125 String splitItem = ",";
105 - if(!StringUtil.isNullOrEmpty(userIds)){ 126 + if (!StringUtil.isNullOrEmpty(userIds)) {
106 if (userIds.contains(splitItem)) { 127 if (userIds.contains(splitItem)) {
107 String[] split = userIds.split(splitItem); 128 String[] split = userIds.split(splitItem);
108 for (String userId : split) { 129 for (String userId : split) {