系统中一个简单的边缘功能要用消息系统,不值得再接入各种 mq,redis 也可以实现消息的发布订阅

发布消息(消息生产者 MessageProducer)

1
2
public static final String CHANNEL_KEY = "channel:1";
jedis.publish(CHANNEL_KEY, message);

订阅消息(消息消费者)

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
/**
* 消息消费者
*
* @author hxx
*/
public class MessageConsumer implements Runnable {

/**
* 频道
*/
public static final String CHANNEL_KEY = "channel:1";

/**
* 处理接收消息
*/
private AlarmJedisPubSub alarmJedisPubSub = new AlarmJedisPubSub();

/**
* Logger
*/
private static Logger LOG = LoggerFactory.getLogger(MessageConsumer.class);

@Override
public void run() {
Jedis jedis = null;
try {
jedis = JedisPoolUtils.getPublicJedis();
jedis.subscribe(alarmJedisPubSub, CHANNEL_KEY);
}
catch (Exception e) {
LOG.error("消费消息失败:" + e.getMessage(), e);
}
finally {
JedisPoolUtils.returnRes(jedis);
}
}

/**
* 接收到消息后推给前端
*
* @author ab
*/
class AlarmJedisPubSub extends JedisPubSub {
@Override
public void onMessage(String channel, String message) {
LOG.info("接收到来自频道" + channel + "的消息:" + message);
ResinWebSocketListener.sendToOneUser(JSON.parseObject(message), message, "msg");
}
}

/**
* 测试
*
* @param args args
*/
public static void main(String[] args) {
MessageConsumer messageConsumer = new MessageConsumer();
Thread t1 = new Thread(messageConsumer);
t1.start();
}
}

参考:https://www.cnblogs.com/qlqwjy/p/9763754.html