|
31 | 31 | import org.apache.rocketmq.common.message.MessageBatch; |
32 | 32 | import org.apache.rocketmq.common.message.MessageClientIDSetter; |
33 | 33 | import org.apache.rocketmq.common.message.MessageExt; |
| 34 | +import org.apache.rocketmq.spring.support.DelayMode; |
34 | 35 | import org.apache.rocketmq.spring.support.RocketMQMessageConverter; |
35 | 36 | import org.apache.rocketmq.spring.support.RocketMQUtil; |
36 | 37 | import org.slf4j.Logger; |
@@ -535,6 +536,77 @@ public <T extends Message> SendResult syncSend(String destination, Collection<T> |
535 | 536 | } |
536 | 537 | } |
537 | 538 |
|
| 539 | + /** |
| 540 | + * Same to {@link #syncSend(String, Message)} with send delay time specified in addition. |
| 541 | + * |
| 542 | + * @param destination formats: `topicName:tags` |
| 543 | + * @param message {@link org.springframework.messaging.Message} |
| 544 | + * @param delayTime delay time in seconds for message |
| 545 | + * @return {@link SendResult} |
| 546 | + */ |
| 547 | + public SendResult syncSendDelayTimeSeconds(String destination, Message<?> message, long delayTime) { |
| 548 | + return syncSend(destination, message, producer.getSendMsgTimeout(), delayTime, DelayMode.DELAY_SECONDS); |
| 549 | + } |
| 550 | + |
| 551 | + /** |
| 552 | + * Same to {@link #syncSend(String, Object)} with send delayTime specified in addition. |
| 553 | + * |
| 554 | + * @param destination formats: `topicName:tags` |
| 555 | + * @param payload the Object to use as payload |
| 556 | + * @param delayTime delay time in seconds for message |
| 557 | + * @return {@link SendResult} |
| 558 | + */ |
| 559 | + public SendResult syncSendDelayTimeSeconds(String destination, Object payload, long delayTime) { |
| 560 | + Message<?> message = MessageBuilder.withPayload(payload).build(); |
| 561 | + return syncSend(destination, message, producer.getSendMsgTimeout(), delayTime, DelayMode.DELAY_SECONDS); |
| 562 | + } |
| 563 | + |
| 564 | + /** |
| 565 | + * Same to {@link #syncSend(String, Message)} with send timeout and delay time specified in addition. |
| 566 | + * |
| 567 | + * @param destination formats: `topicName:tags` |
| 568 | + * @param message {@link org.springframework.messaging.Message} |
| 569 | + * @param timeout send timeout with millis |
| 570 | + * @param delayTime delay time for message |
| 571 | + * @return {@link SendResult} |
| 572 | + */ |
| 573 | + public SendResult syncSend(String destination, Message<?> message, long timeout, long delayTime, DelayMode mode) { |
| 574 | + if (Objects.isNull(message) || Objects.isNull(message.getPayload())) { |
| 575 | + log.error("syncSend failed. destination:{}, message is null ", destination); |
| 576 | + throw new IllegalArgumentException("`message` and `message.payload` cannot be null"); |
| 577 | + } |
| 578 | + try { |
| 579 | + long now = System.currentTimeMillis(); |
| 580 | + org.apache.rocketmq.common.message.Message rocketMsg = this.createRocketMqMessage(destination, message); |
| 581 | + if (delayTime > 0 && Objects.nonNull(mode)) { |
| 582 | + switch (mode) { |
| 583 | + case DELAY_SECONDS: |
| 584 | + rocketMsg.setDelayTimeSec(delayTime); |
| 585 | + break; |
| 586 | + case DELAY_MILLISECONDS: |
| 587 | + rocketMsg.setDelayTimeMs(delayTime); |
| 588 | + break; |
| 589 | + case DELIVER_TIME_MILLISECONDS: |
| 590 | + rocketMsg.setDeliverTimeMs(delayTime); |
| 591 | + break; |
| 592 | + default: |
| 593 | + log.warn("delay mode: {} not support", mode); |
| 594 | + } |
| 595 | + } |
| 596 | + SendResult sendResult = producer.send(rocketMsg, timeout); |
| 597 | + long costTime = System.currentTimeMillis() - now; |
| 598 | + if (log.isDebugEnabled()) { |
| 599 | + log.debug("send message cost: {} ms, msgId:{}", costTime, sendResult.getMsgId()); |
| 600 | + } |
| 601 | + return sendResult; |
| 602 | + } catch (Exception e) { |
| 603 | + log.error("syncSend failed. destination:{}, message:{}, detail exception info: ", destination, message, e); |
| 604 | + throw new MessagingException(e.getMessage(), e); |
| 605 | + } |
| 606 | + } |
| 607 | + |
| 608 | + |
| 609 | + |
538 | 610 | /** |
539 | 611 | * Same to {@link #syncSend(String, Message)} with send timeout specified in addition. |
540 | 612 | * |
|
0 commit comments