|
@@ -0,0 +1,76 @@
|
|
|
|
|
+package com.mrxu.framework.starter.rocketmq;
|
|
|
|
|
+
|
|
|
|
|
+import org.apache.rocketmq.client.producer.SendCallback;
|
|
|
|
|
+import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.messaging.support.MessageBuilder;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 功能概要:[] <br>
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author 上研院 zhuzhoutong
|
|
|
|
|
+ * @date 2021/11/17
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+public class RocketMQSender {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * rockemq template
|
|
|
|
|
+ */
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private RocketMQTemplate rocketMQTemplate;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 默认超时时间
|
|
|
|
|
+ */
|
|
|
|
|
+ @Value("${rocketmq.producer.send-message-timeout:3000}")
|
|
|
|
|
+ private Long messageTimeOut;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送普通消息
|
|
|
|
|
+ * @param destination 发送队列
|
|
|
|
|
+ * @param message 发送内容
|
|
|
|
|
+ */
|
|
|
|
|
+ public void sendMsg(String destination, Object message){
|
|
|
|
|
+ this.sendMsg(destination,message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送消息
|
|
|
|
|
+ * @param destination 队列
|
|
|
|
|
+ * @param message 消息
|
|
|
|
|
+ * @param timeOut 超时
|
|
|
|
|
+ */
|
|
|
|
|
+ public void sendMsg(String destination, Object message,Long timeOut){
|
|
|
|
|
+ rocketMQTemplate.syncSend(destination,MessageBuilder.withPayload(message).build(),timeOut);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送异步消息 在SendCallback中可处理相关成功失败时的逻辑
|
|
|
|
|
+ */
|
|
|
|
|
+ public void sendAsyncMsg(String destination, Object message,SendCallback sendCallback){
|
|
|
|
|
+ this.sendAsyncMsg(destination,message,sendCallback,messageTimeOut);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 异步发送消息
|
|
|
|
|
+ * @param destination 队列
|
|
|
|
|
+ * @param message 消息
|
|
|
|
|
+ * @param sendCallback 回调
|
|
|
|
|
+ * @param timeOut 超时
|
|
|
|
|
+ */
|
|
|
|
|
+ public void sendAsyncMsg(String destination, Object message,SendCallback sendCallback,Long timeOut){
|
|
|
|
|
+ rocketMQTemplate.asyncSend(destination,message,sendCallback,timeOut);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送延时消息<br/>
|
|
|
|
|
+ * 在start版本中 延时消息一共分为18个等级分别为:1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h<br/>
|
|
|
|
|
+ */
|
|
|
|
|
+ public void sendDelayMsg(String destination, Object msgBody, Integer delayLevel){
|
|
|
|
|
+ rocketMQTemplate.syncSend(destination,MessageBuilder.withPayload(msgBody).build(),messageTimeOut,delayLevel);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|