MessageProducer.java
1.56 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
package com.topdraw.mq.producer;
import com.topdraw.business.process.service.impl.PointsOperationServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.aop.framework.AopContext;
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("#{rabbitMqConfig.getExchange()}")
private String exchange;
@Value("#{rabbitMqConfig.getQueue()}")
private String queue;
public void sendMessage(String msg){
this.sendMessage(msg, null, null);
}
public void sendMessage(String msg, String queue){
// 管理侧
if (StringUtils.isEmpty(queue)) {
queue = this.queue;
}
this.sendMessage(msg, null, queue);
}
public void sendMessage(String msg, String exchange, String queue){
if (StringUtils.isEmpty(exchange)) {
exchange = this.exchange;
}
if (StringUtils.isEmpty(queue)) {
queue = this.queue;
}
this.sendDirectMessage(msg, exchange, queue);
}
private void sendDirectMessage(String msg, String exchange, String queue) {
amqpTemplate.convertAndSend(exchange, queue, msg);
log.info("send sendMessage msg || exchange: {} || queue: {} || msg:{} ", exchange, queue, msg);
}
}