|
@@ -6,10 +6,12 @@ import com.alibaba.fastjson2.TypeReference;
|
|
|
import com.aliyun.mns.client.CloudAccount;
|
|
import com.aliyun.mns.client.CloudAccount;
|
|
|
import com.aliyun.mns.client.CloudQueue;
|
|
import com.aliyun.mns.client.CloudQueue;
|
|
|
import com.aliyun.mns.client.MNSClient;
|
|
import com.aliyun.mns.client.MNSClient;
|
|
|
|
|
+import com.aliyun.mns.common.ClientException;
|
|
|
|
|
+import com.aliyun.mns.common.ServiceException;
|
|
|
import com.aliyun.mns.model.Message;
|
|
import com.aliyun.mns.model.Message;
|
|
|
-import com.kym.service.awoara.factory.AwoaraEventHandlerFactory;
|
|
|
|
|
import com.kym.entity.awoara.Event;
|
|
import com.kym.entity.awoara.Event;
|
|
|
import com.kym.entity.awoara.MessageBody;
|
|
import com.kym.entity.awoara.MessageBody;
|
|
|
|
|
+import com.kym.service.awoara.factory.AwoaraEventHandlerFactory;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.text.StringEscapeUtils;
|
|
import org.apache.commons.text.StringEscapeUtils;
|
|
|
import org.springframework.context.event.ContextRefreshedEvent;
|
|
import org.springframework.context.event.ContextRefreshedEvent;
|
|
@@ -55,31 +57,51 @@ public class MnsHandler {
|
|
|
MNSClient client = account.getMNSClient();
|
|
MNSClient client = account.getMNSClient();
|
|
|
CloudQueue queue = client.getQueueRef(queueName);
|
|
CloudQueue queue = client.getQueueRef(queueName);
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private static void batchReceive(CloudQueue queue) {
|
|
|
|
|
+ executorService.execute(() -> {
|
|
|
|
|
+ while (true) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ longPollingBatchReceive(queue);
|
|
|
|
|
+ } catch (ClientException ce) {
|
|
|
|
|
+ System.out.println("Something wrong with the network connection between client and MNS service."
|
|
|
|
|
+ + "Please check your network and DNS availablity.");
|
|
|
|
|
+ ce.printStackTrace();
|
|
|
|
|
+ } catch (ServiceException se) {
|
|
|
|
|
+ if (se.getErrorCode().equals("QueueNotExist")) {
|
|
|
|
|
+ System.out.println("Queue is not exist.Please create queue before use");
|
|
|
|
|
+ } else if (se.getErrorCode().equals("TimeExpired")) {
|
|
|
|
|
+ System.out.println("The request is time expired. Please check your local machine timeclock");
|
|
|
|
|
+ }
|
|
|
|
|
+ se.printStackTrace();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.out.println("Unknown exception happened!");
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 长轮询批量获取消息
|
|
* 长轮询批量获取消息
|
|
|
*
|
|
*
|
|
|
* @param queue
|
|
* @param queue
|
|
|
*/
|
|
*/
|
|
|
- @SuppressWarnings("InfiniteLoopStatement")
|
|
|
|
|
private static void longPollingBatchReceive(CloudQueue queue) {
|
|
private static void longPollingBatchReceive(CloudQueue queue) {
|
|
|
- executorService.execute(() -> {
|
|
|
|
|
- while (true) {
|
|
|
|
|
- log.info("=============开始 长轮询批量获取消息=============");
|
|
|
|
|
|
|
+ log.info("=============开始 长轮询批量获取消息=============");
|
|
|
|
|
|
|
|
- // 一次性拉取最多xx条消息
|
|
|
|
|
- int batchSize = 15;
|
|
|
|
|
- // 长轮询时间为 xx s
|
|
|
|
|
- int waitSeconds = 15;
|
|
|
|
|
|
|
+ // 一次性拉取最多xx条消息
|
|
|
|
|
+ int batchSize = 15;
|
|
|
|
|
+ // 长轮询时间为 xx s
|
|
|
|
|
+ int waitSeconds = 15;
|
|
|
|
|
|
|
|
- List<Message> messages = queue.batchPopMessage(batchSize, waitSeconds);
|
|
|
|
|
- if (messages != null && !messages.isEmpty()) {
|
|
|
|
|
- for (Message message : messages) {
|
|
|
|
|
- printMsgAndDelete(queue, message);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- log.info("=============结束 长轮询批量获取消息=============");
|
|
|
|
|
|
|
+ List<Message> messages = queue.batchPopMessage(batchSize, waitSeconds);
|
|
|
|
|
+ if (messages != null && !messages.isEmpty()) {
|
|
|
|
|
+ for (Message message : messages) {
|
|
|
|
|
+ printMsgAndDelete(queue, message);
|
|
|
}
|
|
}
|
|
|
- });
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("=============结束 长轮询批量获取消息=============");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -129,9 +151,9 @@ public class MnsHandler {
|
|
|
log.info("消息内容:{}", json.toJSONString());
|
|
log.info("消息内容:{}", json.toJSONString());
|
|
|
if ("upload".equals(json.getString("messagetype"))) {
|
|
if ("upload".equals(json.getString("messagetype"))) {
|
|
|
var event = json.getJSONObject("payload").getString("event");
|
|
var event = json.getJSONObject("payload").getString("event");
|
|
|
- var handler = AwoaraEventHandlerFactory.getEventHandler(event);
|
|
|
|
|
- var message = parseMessageBody(json.toJSONString(), Event.getClazz(event));
|
|
|
|
|
- handler.handle(message);
|
|
|
|
|
|
|
+ var handler = AwoaraEventHandlerFactory.getEventHandler(event);
|
|
|
|
|
+ var message = parseMessageBody(json.toJSONString(), Event.getClazz(event));
|
|
|
|
|
+ handler.handle(message);
|
|
|
} else {
|
|
} else {
|
|
|
log.info("message type is not upload");
|
|
log.info("message type is not upload");
|
|
|
}
|
|
}
|
|
@@ -150,7 +172,7 @@ public class MnsHandler {
|
|
|
@EventListener(classes = {ContextRefreshedEvent.class})
|
|
@EventListener(classes = {ContextRefreshedEvent.class})
|
|
|
@Async
|
|
@Async
|
|
|
public void init() {
|
|
public void init() {
|
|
|
- longPollingBatchReceive(queue);
|
|
|
|
|
|
|
+ batchReceive(queue);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|