|
@@ -0,0 +1,124 @@
|
|
|
+package com.caimei365.tools.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.caimei365.tools.model.dto.MessageDto;
|
|
|
+import com.caimei365.tools.service.RocketMqService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.rocketmq.client.producer.SendCallback;
|
|
|
+import org.apache.rocketmq.client.producer.SendResult;
|
|
|
+import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.messaging.Message;
|
|
|
+import org.springframework.messaging.support.MessageBuilder;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+/**
|
|
|
+ * RocketMQ消息中间件实现类
|
|
|
+ *
|
|
|
+ * @author : Charles
|
|
|
+ * @date : 2021/6/17
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class RocketMqServiceImpl implements RocketMqService {
|
|
|
+
|
|
|
+ @Value("${rocketmq.producer.send-message-timeout}")
|
|
|
+ private Long timeout;
|
|
|
+
|
|
|
+ private final RocketMQTemplate rocketMQTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送消息
|
|
|
+ *
|
|
|
+ * @param messageDto {
|
|
|
+ * topic 消息主题
|
|
|
+ * content 消息内容
|
|
|
+ * tag 消息标签(可选)
|
|
|
+ * sort 有序消息(可选):1是,0否
|
|
|
+ * async 异步消息(可选):1是,0否
|
|
|
+ * oneway 单向消息(可选):1是,0否
|
|
|
+ * delay 延时消息等级(可选):1-18,0否,对应时间依次:1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
|
|
|
+ * }
|
|
|
+ * @return SendResult
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public SendResult sendCommonMessage(MessageDto messageDto) {
|
|
|
+ String destination = messageDto.getTopic();
|
|
|
+ if (StringUtils.isNotEmpty(messageDto.getTag())) {
|
|
|
+ destination += ":" + messageDto.getTag();
|
|
|
+ }
|
|
|
+ Message<String> message = MessageBuilder.withPayload(messageDto.getContent()).build();
|
|
|
+ SendResult returnResult = null;
|
|
|
+ boolean isAsync = (messageDto.getAsync() != null && messageDto.getAsync() == 1);
|
|
|
+ boolean isSort = (messageDto.getSort() != null && messageDto.getSort() == 1);
|
|
|
+ boolean isOneway = (messageDto.getOneway() != null && messageDto.getOneway() == 1);
|
|
|
+ boolean isDelay = (messageDto.getDelay() != null && messageDto.getDelay() > 0);
|
|
|
+ if (!isAsync) {
|
|
|
+ // 同步消息
|
|
|
+ if (isDelay) {
|
|
|
+ // 延时
|
|
|
+ returnResult = rocketMQTemplate.syncSend(destination, message, timeout, messageDto.getDelay());
|
|
|
+ } else {
|
|
|
+ if (isSort && isOneway) {
|
|
|
+ // 单向有序
|
|
|
+ rocketMQTemplate.sendOneWayOrderly(destination, message, "hashkey");
|
|
|
+ } else if (isSort) {
|
|
|
+ // 有序
|
|
|
+ returnResult = rocketMQTemplate.syncSendOrderly(destination, message, "hashkey");
|
|
|
+ } else if (isOneway) {
|
|
|
+ // 单向
|
|
|
+ rocketMQTemplate.sendOneWay(destination, message);
|
|
|
+ } else {
|
|
|
+ returnResult = rocketMQTemplate.syncSend(destination, message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 异步消息
|
|
|
+ if (isDelay) {
|
|
|
+ // 异步延时
|
|
|
+ rocketMQTemplate.asyncSend(destination, message, new SendCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(SendResult sendResult) {
|
|
|
+ log.info("异步消息发送成功:{}", JSON.toJSONString(sendResult));
|
|
|
+ //可以处理相应的业务
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void onException(Throwable throwable) {
|
|
|
+ //可以处理相应的业务
|
|
|
+ }
|
|
|
+ }, timeout, messageDto.getDelay());
|
|
|
+ } else {
|
|
|
+ if (isSort) {
|
|
|
+ // 异步有序
|
|
|
+ rocketMQTemplate.asyncSendOrderly(destination, message, "hashkey", new SendCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(SendResult sendResult) {
|
|
|
+ log.info("异步消息发送成功:{}", JSON.toJSONString(sendResult));
|
|
|
+ //可以处理相应的业务
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void onException(Throwable throwable) {
|
|
|
+ //可以处理相应的业务
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ rocketMQTemplate.asyncSend(destination, message, new SendCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(SendResult sendResult) {
|
|
|
+ log.info("异步消息发送成功:{}", JSON.toJSONString(sendResult));
|
|
|
+ //可以处理相应的业务
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void onException(Throwable throwable) {
|
|
|
+ //可以处理相应的业务
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return returnResult;
|
|
|
+ }
|
|
|
+}
|