|
@@ -1,124 +0,0 @@
|
|
-package com.elab.mq.config;
|
|
|
|
-
|
|
|
|
-import com.aliyun.openservices.ons.api.MessageListener;
|
|
|
|
-import com.aliyun.openservices.ons.api.PropertyKeyConst;
|
|
|
|
-import com.aliyun.openservices.ons.api.PropertyValueConst;
|
|
|
|
-import com.aliyun.openservices.ons.api.bean.Subscription;
|
|
|
|
-import com.elab.mq.listener.DefaultMessageListener;
|
|
|
|
-import com.elab.mq.msg.IConsumeProcessService;
|
|
|
|
-import com.elab.mq.msg.IMsgConsumerFacade;
|
|
|
|
-import com.elab.mq.msg.IMsgProducerFacade;
|
|
|
|
-import com.elab.mq.msg.IMsgTransactionFacade;
|
|
|
|
-import com.elab.mq.msg.adptor.MessageListenerAdaptor;
|
|
|
|
-import com.elab.mq.msg.adptor.TransactionCheckListenerAdaptor;
|
|
|
|
-import com.elab.mq.msg.impl.MsgConsumerImpl;
|
|
|
|
-import com.elab.mq.msg.impl.MsgProducerImpl;
|
|
|
|
-import com.elab.mq.msg.impl.MsgTransactionImpl;
|
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
|
-
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-import java.util.Properties;
|
|
|
|
-
|
|
|
|
-@Configuration
|
|
|
|
-public class RocketMqConfigBean {
|
|
|
|
-
|
|
|
|
- private String accessKey;
|
|
|
|
- private String secretKey;
|
|
|
|
- private String onsAddr;
|
|
|
|
- private String producerId;
|
|
|
|
- private String consumerId;
|
|
|
|
- private String sendMsgTimeoutMillis;
|
|
|
|
-
|
|
|
|
- public RocketMqConfigBean(String accessKey, String secretKey, String onsAddr, String producerId, String consumerId, String sendMsgTimeoutMillis) {
|
|
|
|
- this.accessKey = accessKey;
|
|
|
|
- this.secretKey = secretKey;
|
|
|
|
- this.onsAddr = onsAddr;
|
|
|
|
- this.producerId = producerId;
|
|
|
|
- this.consumerId = consumerId;
|
|
|
|
- this.sendMsgTimeoutMillis = sendMsgTimeoutMillis;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public Properties getProperties() {
|
|
|
|
- Properties properties = new Properties();
|
|
|
|
- // AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
|
|
|
|
- properties.put(PropertyKeyConst.AccessKey, this.accessKey);
|
|
|
|
- // SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
|
|
|
|
- properties.put(PropertyKeyConst.SecretKey, this.secretKey);
|
|
|
|
- //设置发送超时时间,单位毫秒
|
|
|
|
- properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, this.sendMsgTimeoutMillis);
|
|
|
|
- // 设置 TCP 接入域名(此处以公共云生产环境为例)
|
|
|
|
- properties.put(PropertyKeyConst.ONSAddr, this.onsAddr);
|
|
|
|
- return properties;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 创建一个生产者
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public IMsgProducerFacade createProducer() {
|
|
|
|
- //您在控制台创建的 Producer ID
|
|
|
|
- Properties properties = getProperties();
|
|
|
|
- properties.put(PropertyKeyConst.ProducerId, this.producerId);
|
|
|
|
- MsgProducerImpl producer = new MsgProducerImpl();
|
|
|
|
- producer.setProperties(properties);
|
|
|
|
- return producer;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 创建一个消费者
|
|
|
|
- *
|
|
|
|
- * @param topicName 话题名称
|
|
|
|
- * @param tag 标签
|
|
|
|
- * @param messageListener 回调监听接口
|
|
|
|
- * @param isSubscription 是否为订阅类型
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public IMsgConsumerFacade createConsumer(String topicName, String tag, MessageListenerAdaptor messageListener, boolean isSubscription) {
|
|
|
|
- Properties properties = getProperties();
|
|
|
|
- properties.put(PropertyKeyConst.ConsumerId, this.consumerId);
|
|
|
|
- if (isSubscription) {
|
|
|
|
- properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.BROADCASTING);
|
|
|
|
- }
|
|
|
|
- MsgConsumerImpl consumer = new MsgConsumerImpl();
|
|
|
|
- consumer.setProperties(properties);
|
|
|
|
- Map<Subscription, MessageListener> subscriptionTable = new HashMap<>();
|
|
|
|
- Subscription subscription = new Subscription();
|
|
|
|
- subscription.setTopic(topicName);
|
|
|
|
- subscription.setExpression(tag);
|
|
|
|
- subscriptionTable.put(subscription, messageListener);
|
|
|
|
- consumer.setSubscriptionTable(subscriptionTable);
|
|
|
|
- return consumer;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 创建一个默认的消费监听适配者
|
|
|
|
- *
|
|
|
|
- * @param consumeProcessServices 消费执行者列表
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public MessageListenerAdaptor getMessageListenerAdaptor(List<IConsumeProcessService> consumeProcessServices) {
|
|
|
|
- DefaultMessageListener defaultMessageListener = new DefaultMessageListener();
|
|
|
|
- defaultMessageListener.setConsumeProcessServiceList(consumeProcessServices);
|
|
|
|
- defaultMessageListener.init();
|
|
|
|
- return defaultMessageListener;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 创建一个事务消息发送类
|
|
|
|
- *
|
|
|
|
- * @param transactionCheckListener 事务消息回调
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public IMsgTransactionFacade createTransactionProducer(TransactionCheckListenerAdaptor transactionCheckListener) {
|
|
|
|
- Properties properties = getProperties();
|
|
|
|
- properties.put(PropertyKeyConst.ProducerId, this.producerId);
|
|
|
|
- properties.put(PropertyKeyConst.GROUP_ID, this.producerId);
|
|
|
|
-
|
|
|
|
- MsgTransactionImpl transactionProducer = new MsgTransactionImpl(properties, transactionCheckListener);
|
|
|
|
- return transactionProducer;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-}
|
|
|