com.aliyun.openservices.ons.api.exactlyonce.manager.TransactionManager Maven / Gradle / Ivy
package com.aliyun.openservices.ons.api.exactlyonce.manager;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.logging.InternalLogger;
import com.aliyun.openservices.ons.api.exactlyonce.aop.model.MQTxContext;
import com.aliyun.openservices.ons.api.exactlyonce.datasource.DataSourceConfig;
import com.aliyun.openservices.ons.api.exactlyonce.datasource.core.MQTxConnection;
import com.aliyun.openservices.ons.api.exactlyonce.manager.util.LogUtil;
import com.aliyun.openservices.ons.api.impl.util.ClientLoggerUtil;
import com.aliyun.openservices.shade.io.netty.util.internal.ConcurrentSet;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
/**
* @author gongshi
*/
public class TransactionManager {
private static final InternalLogger LOGGER = ClientLoggerUtil.getClientLogger();
private static ConcurrentMap> consumerMap = new ConcurrentHashMap();
private static ConcurrentMap> consumerSessionMap = new ConcurrentHashMap>();
private static volatile AtomicBoolean started = new AtomicBoolean(false);
private static TxRecordManager txRecordManager;
public static void start(Properties properties) {
if (!started.compareAndSet(false, true)) {
return;
}
LogUtil.info(LOGGER, "start TransactionManager...");
txRecordManager = new TxRecordManager(properties);
txRecordManager.start();
MetricService.getInstance().start();
}
public static void stop() {
if (!started.compareAndSet(true, false)) {
return;
}
txRecordManager.stop();
MetricService.getInstance().stop();
}
public static void addConsumer(String consumerGroup, DefaultMQPushConsumer defaultMQPushConsumer) {
ConcurrentSet consumerSet = consumerMap.get(consumerGroup);
if (consumerSet == null) {
ConcurrentSet newConsumerSet = new ConcurrentSet();
newConsumerSet.add(defaultMQPushConsumer);
ConcurrentSet old = consumerMap.putIfAbsent(consumerGroup, newConsumerSet);
if (old != null) {
old.add(defaultMQPushConsumer);
}
} else {
consumerSet.add(defaultMQPushConsumer);
}
}
public static Set getConsumer(String consumerGroup) {
return consumerMap.get(consumerGroup);
}
public static void flushTxRecord(MQTxConnection connection, MQTxContext context) throws Exception {
txRecordManager.flushTxRecord(connection, context);
}
public static void updateConsumeSessionMap(DataSourceConfig config, String consumerGroup) {
if (config == null || StringUtils.isEmpty(consumerGroup)) {
return;
}
ConcurrentSet cidSet = consumerSessionMap.get(config);
if (cidSet == null) {
ConcurrentSet newCidSet = new ConcurrentSet();
newCidSet.add(consumerGroup);
ConcurrentSet old = consumerSessionMap.putIfAbsent(config, newCidSet);
if (old != null) {
old.add(consumerGroup);
}
} else {
cidSet.add(consumerGroup);
}
}
public static ConcurrentMap> getConsumerSessionMap() {
return consumerSessionMap;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy