MessageProducer.java
2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.topdraw.mq.producer;
import com.topdraw.config.LocalConstants;
import com.topdraw.mq.config.RabbitMqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
@Slf4j
public class MessageProducer {
@Autowired
private AmqpTemplate amqpTemplate;
@Value("${uc.service.platform}")
private String platformType;
public void sendMessage(String msg,String exchangeName){
// 管理侧
if (platformType.equalsIgnoreCase(LocalConstants.PLATFORM_TYPE_MANAGEMENT)) {
if (StringUtils.isEmpty(exchangeName)) {
exchangeName = RabbitMqConfig.UC_QUEUE_DIRECT_CCC;
}
this.sendDirectMessage(msg,exchangeName);
}
// 服务侧
if (platformType.equalsIgnoreCase(LocalConstants.PLATFORM_TYPE_SERVICE)) {
if (StringUtils.isEmpty(exchangeName)) {
exchangeName = RabbitMqConfig.UC_QUEUE_DIRECT_BBB;
}
this.sendDirectMessage(msg,exchangeName);
}
}
/**
* 广播
* @param msg
* @param exchangeName
* @author XiangHan
* @date 2021/9/7 11:10 上午
*/
private void sendFanoutMessage(String msg,String exchangeName) {
if (StringUtils.isEmpty(exchangeName)) {
exchangeName = RabbitMqConfig.UC_EXCHANGE_FANOUT;
}
amqpTemplate.convertAndSend(exchangeName, "", msg);
log.info("send sendFanoutMessage msg || entityType: {} || msg:{} ", msg);
}
/**
* 直连
* @param msg
* @param queueName
* @author XiangHan
* @date 2021/9/7 11:10 上午
*/
private void sendDirectMessage(String msg,String queueName) {
amqpTemplate.convertAndSend(queueName, msg);
log.info("send sendFanoutMessage msg || entityType: {} || msg:{} ", msg);
}
/**
* 发送
* @param msg
* @author XiangHan
* @date 2021/9/7 11:10 上午
*/
public void sendMessage(String msg) {
this.sendMessage(msg,null);
}
}