ActiveMq mqtt发送消息统计

对ActiveMq主题消息进行统计。统计每个主题下消息记录或发送消息数量。

上一章,我们介绍了ActiveMq mqtt自定义连接认证,本章将在原有的基础上,进行消息的统计。

实现方式

我们只需要重写send方法,在send方法中做统计业务处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Override
public void send(ProducerBrokerExchange producerExchange, Message messageSend) throws Exception {

//客户id
String clientId = producerExchange.getConnectionContext().getClientId();

//主题名称
String physicalName = messageSend.getDestination().getPhysicalName();

//消息内容
ByteSequence byteSequence = messageSend.getContent();
byteSequence.compact();//去掉空数据
String content = new String(byteSequence.getData(),"UTF-8");

log.info("clientId:{},physicalName:{},content:{}",clientId,physicalName,content);

//业务逻辑处理
//...
//...

super.send(producerExchange, messageSend);
}

源码下载

https://github.com/fallsea/mqtt