add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.dobbinsoft.fw.support.mq;
 
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.io.IOException;
import java.io.Serializable;
import java.util.concurrent.TimeoutException;
 
/**
 * ClassName: RabbitReliableMessageQueue
 * Description: TODO
 *
 * @author: e-weichaozheng
 * @date: 2021-05-25
 */
public class RabbitReliableMessageQueue implements ReliableMessageQueue, InitializingBean {
 
    @Autowired
    private ConnectionFactory connectionFactory;
 
    private Channel channel;
 
    private static final Logger logger = LoggerFactory.getLogger(RabbitReliableMessageQueue.class);
 
    @Override
    public void afterPropertiesSet() throws Exception {
        Connection conn = this.connectionFactory.newConnection();
        // 创建消息通道
        this.channel = conn.createChannel();
    }
 
    @Override
    public boolean publish(String topic, Serializable message) {
        try {
            channel.basicPublish(topic, "topic", null, JSONObject.toJSONBytes(message));
            return true;
        } catch (IOException e) {
            logger.error("[消息队列推送] IO异常", e);
            return false;
        }
    }
 
}