|
@@ -0,0 +1,53 @@
|
|
|
+package com.tzld.crawler.etl.mq;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.aliyun.openservices.ons.api.Action;
|
|
|
+import com.aliyun.openservices.ons.api.ConsumeContext;
|
|
|
+import com.aliyun.openservices.ons.api.Message;
|
|
|
+import com.aliyun.openservices.ons.api.batch.BatchMessageListener;
|
|
|
+import com.tzld.crawler.etl.model.vo.CrawlerEtlParam;
|
|
|
+import com.tzld.crawler.etl.service.EtlService;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+ * 批量消费
|
|
|
+ *
|
|
|
+ * @author supeng
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class BatchEtlMessageListener implements BatchMessageListener {
|
|
|
+
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(BatchEtlMessageListener.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ EtlService etlService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Action consume(final List<Message> messages, final ConsumeContext context) {
|
|
|
+ LOGGER.info("Receive message size = {}", messages.size());
|
|
|
+ try {
|
|
|
+ for (Message message : messages) {
|
|
|
+ try {
|
|
|
+ String body = new String(message.getBody(), StandardCharsets.UTF_8);
|
|
|
+ LOGGER.info("message body = {}", body);
|
|
|
+ CrawlerEtlParam param = JSONObject.parseObject(body, CrawlerEtlParam.class);
|
|
|
+ param.setMessageId(message.getMsgID());
|
|
|
+ etlService.deal(param);
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.info("loop message error {}", JSON.toJSONString(message), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Action.CommitMessage;
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ return Action.ReconsumeLater;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|