单例模式
/**
* @author WangChao
* @create 2020/12/7 6:30
*/
public class SocketManager {
private static class SocketManagerHolder {
static final SocketManager INSTANCE = createSocketManager();
private static SocketManager createSocketManager() {
return new SocketManager();
}
}
public static SocketManager instance() {
return SocketManagerHolder.INSTANCE;
}
/**
* 标记socket连接状态
*/
private volatile boolean netState;
public void flagClose() {
this.netState = false;
}
public void flagOpen() {
this.netState = true;
}
public boolean isNetState() {
return netState;
}
}
策略模式+工厂模式
策略类
package org.tx.factory.bean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.tx.aop.entity.TxTransactionInfo;
/**
* @author wangchao
* @description: TODO
* @date 2020/12/7 13:17
*/
public interface TransactionServer {
public Object execute(ProceedingJoinPoint point, TxTransactionInfo info) throws Throwable;
}
package org.tx.factory.bean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.tx.aop.entity.TxTransactionInfo;
import org.tx.factory.TransactionServerFactory;
import org.tx.factory.TransactionServerType;
/**
* @author wangchao
* @description: TODO
* @date 2020/12/7 13:16
*/
@Component
public class DefaultTransactionServer implements TransactionServer , InitializingBean {
@Override
public Object execute(ProceedingJoinPoint point, TxTransactionInfo info) throws Throwable {
return point.proceed();
}
@Override
public void afterPropertiesSet() throws Exception {
TransactionServerFactory.register(TransactionServerType.DefaultTransactionServer,this);
}
}
package org.tx.factory.bean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.tx.aop.entity.TxTransactionInfo;
import org.tx.factory.TransactionServerFactory;
import org.tx.factory.TransactionServerType;
/**
* @author wangchao
* @description: TODO
* @date 2020/12/7 13:31
*/
@Component
public class B implements TransactionServer , InitializingBean {
@Override
public Object execute(ProceedingJoinPoint point, TxTransactionInfo info) throws Throwable {
return point.proceed();
}
@Override
public void afterPropertiesSet() throws Exception {
TransactionServerFactory.register(TransactionServerType.B,this);
}
}
package org.tx.factory.bean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.tx.aop.entity.TxTransactionInfo;
import org.tx.factory.TransactionServerFactory;
import org.tx.factory.TransactionServerType;
/**
* @author wangchao
* @description: TODO
* @date 2020/12/8 8:31
*/
@Component
public class A implements TransactionServer , InitializingBean {
@Override
public Object execute(ProceedingJoinPoint point, TxTransactionInfo info) throws Throwable {
return point.proceed();
}
@Override
public void afterPropertiesSet() throws Exception {
TransactionServerFactory.register(TransactionServerType.A,this);
}
}
工厂类
package org.tx.factory;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Component;
import org.tx.aop.entity.TxTransactionInfo;
import org.tx.factory.bean.TransactionServer;
import org.tx.netty.SocketManager;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author wangchao
* @description: TODO
* @date 2020/12/7 12:54
*/
@Component
public class TransactionServerFactory
// implements FactoryBean
{
private static Map<TransactionServerType, TransactionServer> transactionServerMap = new ConcurrentHashMap<>();
public TransactionServer getTransactionServer(TxTransactionInfo info) {
if (!SocketManager.instance().isNetState()) {
return transactionServerMap.get(TransactionServerType.DefaultTransactionServer);
}
return null;
}
public static void register(TransactionServerType transactionServerType, TransactionServer transactionServer) {
transactionServerMap.put(transactionServerType, transactionServer);
}
/**
* 获取事务处理类
*
* @return
* @throws BeansException
*/
// @Override
// public TransactionServer getObject() throws BeansException {
// if (!SocketManager.instance().isNetState()) {
// return new DefaultTransactionServer();
// }
// return null;
// }
//
// @Override
// public Class<?> getObjectType() {
// return null;
// }
//
// @Override
// public boolean isSingleton() {
// return true;
// }
}
枚举类
package org.tx.factory;
/**
* @author wangchao
* @description: TODO
* @date 2020/12/8 10:23
*/
public enum TransactionServerType {
DefaultTransactionServer, A, B;
}