MessageProducer.java
1.36 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
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("#{rabbitMqConfig.getRoutingKey()}")
private String routingKey;
public void sendMessage(String msg,String exchangeName){
// 管理侧
if (StringUtils.isEmpty(exchangeName)) {
exchangeName = this.routingKey;
}
this.sendDirectMessage(msg,exchangeName);
}
/**
* 直连
* @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 sendMessage msg || routingkey: {} || msg:{} ", queueName, msg);
}
/**
* 发送
* @param msg
* @author XiangHan
* @date 2021/9/7 11:10 上午
*/
public void sendMessage(String msg) {
this.sendMessage(msg,null);
}
}