|
@@ -1,106 +1,106 @@
|
|
|
-/*
|
|
|
- * The MIT License (MIT)
|
|
|
- *
|
|
|
- * Copyright © 2023 xrv <xrv@live.com>
|
|
|
- *
|
|
|
- * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
- * of this software and associated documentation files (the "Software"), to deal
|
|
|
- * in the Software without restriction, including without limitation the rights
|
|
|
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
- * copies of the Software, and to permit persons to whom the Software is
|
|
|
- * furnished to do so, subject to the following conditions:
|
|
|
- *
|
|
|
- * The above copyright notice and this permission notice shall be included in
|
|
|
- * all copies or substantial portions of the Software.
|
|
|
- *
|
|
|
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
- * THE SOFTWARE.
|
|
|
- */
|
|
|
-
|
|
|
-package com.tzld.crawler.etl.mq;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.aliyun.mq.http.MQClient;
|
|
|
-import com.aliyun.mq.http.MQConsumer;
|
|
|
-import com.aliyun.mq.http.model.Message;
|
|
|
-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.Value;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author ehlxr
|
|
|
- * @since 2023-06-09 15:24.
|
|
|
- */
|
|
|
-@Component
|
|
|
-public class EtlMQConsumer {
|
|
|
- private static final Logger log = LoggerFactory.getLogger(EtlMQConsumer.class);
|
|
|
- private final EtlService etlService;
|
|
|
- @Value("${rocketmq.accessKey:}")
|
|
|
- private String accessKey;
|
|
|
- @Value("${rocketmq.secretKey:}")
|
|
|
- private String secretKey;
|
|
|
- @Value("${rocketmq.httpEndpoint:}")
|
|
|
- private String httpEndpoint;
|
|
|
- @Value("${rocketmq.instanceId:}")
|
|
|
- private String instanceId;
|
|
|
- @Value("${rocketmq.crawler.etl.topic:}")
|
|
|
- private String topic;
|
|
|
- @Value("${rocketmq.crawler.etl.groupid:}")
|
|
|
- private String groupId;
|
|
|
- @Value("${consumer.thread.size:32}")
|
|
|
- private Integer threadSize;
|
|
|
-
|
|
|
- private MQConsumer consumer;
|
|
|
-
|
|
|
- public EtlMQConsumer(EtlService etlService) {
|
|
|
- this.etlService = etlService;
|
|
|
- }
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void init() {
|
|
|
- MQClient mqClient = new MQClient(httpEndpoint, accessKey, secretKey);
|
|
|
- consumer = mqClient.getConsumer(instanceId, topic, groupId, null);
|
|
|
-
|
|
|
- ExecutorService service = Executors.newFixedThreadPool(threadSize);
|
|
|
- for (int i = 0; i < threadSize; i++) {
|
|
|
- service.submit(this::consumeMsg);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void consumeMsg() {
|
|
|
- do {
|
|
|
- try {
|
|
|
- List<Message> messages = consumer.consumeMessage(1, 10);
|
|
|
- if (messages == null || messages.isEmpty()) {
|
|
|
- log.info("No new message, continue");
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- messages.forEach(message -> {
|
|
|
- log.info("Receive message: {} from topic: {}, group: {}", message, topic, groupId);
|
|
|
- CrawlerEtlParam param = JSONObject.parseObject(message.getMessageBodyString(), CrawlerEtlParam.class);
|
|
|
- param.setMessageId(message.getMessageId());
|
|
|
- etlService.deal(param);
|
|
|
- log.info("Deal done of message: {} from topic: {}, group: {}", message, topic, groupId);
|
|
|
- consumer.ackMessage(Collections.singletonList(message.getReceiptHandle()));
|
|
|
- });
|
|
|
- } catch (Throwable e) {
|
|
|
- log.error("Consume message from topic: {}, group: {} error", topic, groupId, e);
|
|
|
- }
|
|
|
- } while (true);
|
|
|
- }
|
|
|
-}
|
|
|
+///*
|
|
|
+// * The MIT License (MIT)
|
|
|
+// *
|
|
|
+// * Copyright © 2023 xrv <xrv@live.com>
|
|
|
+// *
|
|
|
+// * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
+// * of this software and associated documentation files (the "Software"), to deal
|
|
|
+// * in the Software without restriction, including without limitation the rights
|
|
|
+// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
+// * copies of the Software, and to permit persons to whom the Software is
|
|
|
+// * furnished to do so, subject to the following conditions:
|
|
|
+// *
|
|
|
+// * The above copyright notice and this permission notice shall be included in
|
|
|
+// * all copies or substantial portions of the Software.
|
|
|
+// *
|
|
|
+// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
+// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
+// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
+// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
+// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
+// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
+// * THE SOFTWARE.
|
|
|
+// */
|
|
|
+//
|
|
|
+//package com.tzld.crawler.etl.mq;
|
|
|
+//
|
|
|
+//import com.alibaba.fastjson.JSONObject;
|
|
|
+//import com.aliyun.mq.http.MQClient;
|
|
|
+//import com.aliyun.mq.http.MQConsumer;
|
|
|
+//import com.aliyun.mq.http.model.Message;
|
|
|
+//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.Value;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//
|
|
|
+//import javax.annotation.PostConstruct;
|
|
|
+//import java.util.Collections;
|
|
|
+//import java.util.List;
|
|
|
+//import java.util.concurrent.ExecutorService;
|
|
|
+//import java.util.concurrent.Executors;
|
|
|
+//
|
|
|
+///**
|
|
|
+// * @author ehlxr
|
|
|
+// * @since 2023-06-09 15:24.
|
|
|
+// */
|
|
|
+//@Component
|
|
|
+//public class EtlMQConsumer {
|
|
|
+// private static final Logger log = LoggerFactory.getLogger(EtlMQConsumer.class);
|
|
|
+// private final EtlService etlService;
|
|
|
+// @Value("${rocketmq.accessKey:}")
|
|
|
+// private String accessKey;
|
|
|
+// @Value("${rocketmq.secretKey:}")
|
|
|
+// private String secretKey;
|
|
|
+// @Value("${rocketmq.httpEndpoint:}")
|
|
|
+// private String httpEndpoint;
|
|
|
+// @Value("${rocketmq.instanceId:}")
|
|
|
+// private String instanceId;
|
|
|
+// @Value("${rocketmq.crawler.etl.topic:}")
|
|
|
+// private String topic;
|
|
|
+// @Value("${rocketmq.crawler.etl.groupid:}")
|
|
|
+// private String groupId;
|
|
|
+// @Value("${consumer.thread.size:32}")
|
|
|
+// private Integer threadSize;
|
|
|
+//
|
|
|
+// private MQConsumer consumer;
|
|
|
+//
|
|
|
+// public EtlMQConsumer(EtlService etlService) {
|
|
|
+// this.etlService = etlService;
|
|
|
+// }
|
|
|
+//
|
|
|
+// @PostConstruct
|
|
|
+// public void init() {
|
|
|
+// MQClient mqClient = new MQClient(httpEndpoint, accessKey, secretKey);
|
|
|
+// consumer = mqClient.getConsumer(instanceId, topic, groupId, null);
|
|
|
+//
|
|
|
+// ExecutorService service = Executors.newFixedThreadPool(threadSize);
|
|
|
+// for (int i = 0; i < threadSize; i++) {
|
|
|
+// service.submit(this::consumeMsg);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// private void consumeMsg() {
|
|
|
+// do {
|
|
|
+// try {
|
|
|
+// List<Message> messages = consumer.consumeMessage(1, 10);
|
|
|
+// if (messages == null || messages.isEmpty()) {
|
|
|
+// log.info("No new message, continue");
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// messages.forEach(message -> {
|
|
|
+// log.info("Receive message: {} from topic: {}, group: {}", message, topic, groupId);
|
|
|
+// CrawlerEtlParam param = JSONObject.parseObject(message.getMessageBodyString(), CrawlerEtlParam.class);
|
|
|
+// param.setMessageId(message.getMessageId());
|
|
|
+// etlService.deal(param);
|
|
|
+// log.info("Deal done of message: {} from topic: {}, group: {}", message, topic, groupId);
|
|
|
+// consumer.ackMessage(Collections.singletonList(message.getReceiptHandle()));
|
|
|
+// });
|
|
|
+// } catch (Throwable e) {
|
|
|
+// log.error("Consume message from topic: {}, group: {} error", topic, groupId, e);
|
|
|
+// }
|
|
|
+// } while (true);
|
|
|
+// }
|
|
|
+//}
|