From 8d8f3a1e0c7bb35401545e4f0c6b2fef078370ba Mon Sep 17 00:00:00 2001 From: rongtong Date: Mon, 30 Jan 2023 14:51:10 +0800 Subject: [PATCH] Add syncSendDeliverTimeMills and syncSendDelayTimeMills API for RocketMQ 5.0 timer message (#521) --- .../spring/core/RocketMQTemplate.java | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/core/RocketMQTemplate.java b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/core/RocketMQTemplate.java index 8b444e32..a196686d 100644 --- a/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/core/RocketMQTemplate.java +++ b/rocketmq-spring-boot/src/main/java/org/apache/rocketmq/spring/core/RocketMQTemplate.java @@ -538,6 +538,7 @@ public SendResult syncSend(String destination, Collection /** * Same to {@link #syncSend(String, Message)} with send delay time specified in addition. + * This function is only valid when the broker version is 5.0 or above * * @param destination formats: `topicName:tags` * @param message {@link org.springframework.messaging.Message} @@ -550,6 +551,7 @@ public SendResult syncSendDelayTimeSeconds(String destination, Message messag /** * Same to {@link #syncSend(String, Object)} with send delayTime specified in addition. + * This function is only valid when the broker version is 5.0 or above * * @param destination formats: `topicName:tags` * @param payload the Object to use as payload @@ -561,8 +563,64 @@ public SendResult syncSendDelayTimeSeconds(String destination, Object payload, l return syncSend(destination, message, producer.getSendMsgTimeout(), delayTime, DelayMode.DELAY_SECONDS); } + /** + * Same to {@link #syncSend(String, Message)} with send delay time specified in addition. + * This function is only valid when the broker version is 5.0 or above + * + * @param destination formats: `topicName:tags` + * @param message {@link org.springframework.messaging.Message} + * @param delayTime delay time in millisecond for message + * @return {@link SendResult} + */ + public SendResult syncSendDelayTimeMills(String destination, Message message, long delayTime) { + return syncSend(destination, message, producer.getSendMsgTimeout(), delayTime, DelayMode.DELAY_MILLISECONDS); + } + + /** + * Same to {@link #syncSend(String, Object)} with send delayTime specified in addition. + * This function is only valid when the broker version is 5.0 or above + * + * @param destination formats: `topicName:tags` + * @param payload the Object to use as payload + * @param delayTime delay time in millisecond for message + * @return {@link SendResult} + */ + public SendResult syncSendDelayTimeMills(String destination, Object payload, long delayTime) { + Message message = MessageBuilder.withPayload(payload).build(); + return syncSend(destination, message, producer.getSendMsgTimeout(), delayTime, DelayMode.DELAY_MILLISECONDS); + } + + + /** + * Same to {@link #syncSend(String, Message)} with send in a delivered time. + * This function is only valid when the broker version is 5.0 or above + * + * @param destination formats: `topicName:tags` + * @param message {@link org.springframework.messaging.Message} + * @param deliverTimeMills delivered time in millisecond for message + * @return {@link SendResult} + */ + public SendResult syncSendDeliverTimeMills(String destination, Message message, long deliverTimeMills) { + return syncSend(destination, message, producer.getSendMsgTimeout(), deliverTimeMills, DelayMode.DELIVER_TIME_MILLISECONDS); + } + + /** + * Same to {@link #syncSend(String, Object)} with send in a delivered time. + * This function is only valid when the broker version is 5.0 or above + * + * @param destination formats: `topicName:tags` + * @param payload the Object to use as payload + * @param deliverTimeMills delivered time in millisecond for message + * @return {@link SendResult} + */ + public SendResult syncSendDeliverTimeMills(String destination, Object payload, long deliverTimeMills) { + Message message = MessageBuilder.withPayload(payload).build(); + return syncSend(destination, message, producer.getSendMsgTimeout(), deliverTimeMills, DelayMode.DELIVER_TIME_MILLISECONDS); + } + /** * Same to {@link #syncSend(String, Message)} with send timeout and delay time specified in addition. + * This function is only valid when the broker version is 5.0 or above * * @param destination formats: `topicName:tags` * @param message {@link org.springframework.messaging.Message} @@ -570,7 +628,7 @@ public SendResult syncSendDelayTimeSeconds(String destination, Object payload, l * @param delayTime delay time for message * @return {@link SendResult} */ - public SendResult syncSend(String destination, Message message, long timeout, long delayTime, DelayMode mode) { + private SendResult syncSend(String destination, Message message, long timeout, long delayTime, DelayMode mode) { if (Objects.isNull(message) || Objects.isNull(message.getPayload())) { log.error("syncSend failed. destination:{}, message is null ", destination); throw new IllegalArgumentException("`message` and `message.payload` cannot be null");