[size=medium]RocketMQ支持延迟/定时消息,但并不支持任意的时间精度,而是支持特定的level,例如5s,10s,1m等。其中level=0表示不延时,level=1表示1级延时,level=2表示2级延时,以此类推。[/size]
[size=medium][color=red]延迟级别配置[/color][/size]
[size=medium]在rocketmq的broker端的属性配置文件中加入以下行:
messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
描述了各级别与延时时间的对应映射关系。
1、这个配置项配置了从1级开始,各级延时的时间,可以修改这个指定级别的延时时间;
2、时间单位支持:s、m、h、d,分别表示秒、分、时、天;
3、默认值就是上面声明的,可手工调整[/size]
[size=medium][b]可能存在的问题[/b]:在conf/broker.conf中添加messageDelayLevel配置,并修改为自定义的延时级别。测试时发现实际延迟时间仍按默认的配置生效
[b]解决[/b]:如borker在启动时未指定配置文件路径,则使用默认配置,而不是使用conf/broker.conf。可以在启动命令中增加-c指定配置文件路径,如:
nohup sh bin/mqbroker -n localhost:9876 autoCreateTopicEnable=true -c conf/broker.conf &
[color=red]消息发送[/color]
只需要在生产者客户端中设置待发送消息的延时级别即可发送延时消息。
注意:如果项目中引入了spring-boot-starter-rocketmq,那么无法直接使用RocketMQTemplate设置延时级别。
可参考如下代码:[/size]
[size=medium][color=red]扩展[/color]
在RocketMQ中,消息体存储格式为byte[],spring-boot-starter-rocketmq对发送对消息体处理如下:
* 1、如果发送的消息体为String,则将String转化为UTF-8格式的byte[]
* 2、如果发送的消息体不是String,则首先将对象通过jackson-databind转化为JSON String,再将JSON String转化为UTF-8格式的byte[]
[b]原文如下:[/b]
How is the message content body being serialized and deserialized?
RocketMQ's message body is stored as byte[]. When the business system message content body if it is java.lang.String type, unified in accordance with utf-8 code into byte []; If the business system message content is not java.lang.String Type, then use jackson-databind serialized into the JSON format string, and then unified in accordance with utf-8 code into byte[].
[b]参考:[/b][url]https://blog.youkuaiyun.com/u014380653/article/details/52883356[/url][/size]
[size=medium][color=red]延迟级别配置[/color][/size]
[size=medium]在rocketmq的broker端的属性配置文件中加入以下行:
messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
描述了各级别与延时时间的对应映射关系。
1、这个配置项配置了从1级开始,各级延时的时间,可以修改这个指定级别的延时时间;
2、时间单位支持:s、m、h、d,分别表示秒、分、时、天;
3、默认值就是上面声明的,可手工调整[/size]
[size=medium][b]可能存在的问题[/b]:在conf/broker.conf中添加messageDelayLevel配置,并修改为自定义的延时级别。测试时发现实际延迟时间仍按默认的配置生效
[b]解决[/b]:如borker在启动时未指定配置文件路径,则使用默认配置,而不是使用conf/broker.conf。可以在启动命令中增加-c指定配置文件路径,如:
nohup sh bin/mqbroker -n localhost:9876 autoCreateTopicEnable=true -c conf/broker.conf &
[color=red]消息发送[/color]
只需要在生产者客户端中设置待发送消息的延时级别即可发送延时消息。
注意:如果项目中引入了spring-boot-starter-rocketmq,那么无法直接使用RocketMQTemplate设置延时级别。
可参考如下代码:[/size]
EventDTO eventDTO = new EventDTO();
// 设置eventDTO相关属性
String jsonstr = rocketMQTemplate.getObjectMapper().writeValueAsString(eventDTO);
byte[] bytes = jsonstr.getBytes(Charset.forName("UTF-8"));
DefaultMQProducer producer = rocketMQTemplate.getProducer();
org.apache.rocketmq.common.message.Message message =
new org.apache.rocketmq.common.message.Message("topic", "", bytes);
// 设置延迟级别
message.setDelayTimeLevel(4);
producer.send(message, (long)producer.getSendMsgTimeout());
[size=medium][color=red]扩展[/color]
在RocketMQ中,消息体存储格式为byte[],spring-boot-starter-rocketmq对发送对消息体处理如下:
* 1、如果发送的消息体为String,则将String转化为UTF-8格式的byte[]
* 2、如果发送的消息体不是String,则首先将对象通过jackson-databind转化为JSON String,再将JSON String转化为UTF-8格式的byte[]
[b]原文如下:[/b]
How is the message content body being serialized and deserialized?
RocketMQ's message body is stored as byte[]. When the business system message content body if it is java.lang.String type, unified in accordance with utf-8 code into byte []; If the business system message content is not java.lang.String Type, then use jackson-databind serialized into the JSON format string, and then unified in accordance with utf-8 code into byte[].
[b]参考:[/b][url]https://blog.youkuaiyun.com/u014380653/article/details/52883356[/url][/size]