|
@@ -0,0 +1,47 @@
|
|
|
+package com.tzld.crawler.etl.mq;
|
|
|
+
|
|
|
+import com.aliyun.openservices.ons.api.PropertyKeyConst;
|
|
|
+import com.aliyun.openservices.ons.api.batch.BatchMessageListener;
|
|
|
+import com.aliyun.openservices.ons.api.bean.BatchConsumerBean;
|
|
|
+import com.aliyun.openservices.ons.api.bean.Subscription;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class BatchConsumerClient {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MqConfig mqConfig;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BatchEtlMessageListener batchEtlMessageListener;
|
|
|
+
|
|
|
+ @Bean(initMethod = "start", destroyMethod = "shutdown")
|
|
|
+ public BatchConsumerBean buildBatchConsumer() {
|
|
|
+ BatchConsumerBean batchConsumerBean = new BatchConsumerBean();
|
|
|
+ //配置文件
|
|
|
+ Properties properties = mqConfig.getMqPropertie();
|
|
|
+ properties.setProperty(PropertyKeyConst.GROUP_ID, mqConfig.getGroupId());
|
|
|
+ //将消费者线程数固定为?个 20为默认值 范围[1,1000]
|
|
|
+ properties.setProperty(PropertyKeyConst.ConsumeThreadNums, "1000");
|
|
|
+ //批量消费的最大消息数量
|
|
|
+ properties.setProperty(PropertyKeyConst.ConsumeMessageBatchMaxSize, "32");
|
|
|
+ batchConsumerBean.setProperties(properties);
|
|
|
+ //订阅关系
|
|
|
+ Map<Subscription, BatchMessageListener> subscriptionTable = new HashMap<>();
|
|
|
+ Subscription subscription = new Subscription();
|
|
|
+ subscription.setTopic(mqConfig.getTopic());
|
|
|
+ subscription.setExpression(mqConfig.getTag());
|
|
|
+ subscriptionTable.put(subscription, batchEtlMessageListener);
|
|
|
+ //订阅多个topic如上面设置
|
|
|
+
|
|
|
+ batchConsumerBean.setSubscriptionTable(subscriptionTable);
|
|
|
+ return batchConsumerBean;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|