fix:主备切换
This commit is contained in:
@@ -1,135 +0,0 @@
|
||||
package com.sunyard.pool;
|
||||
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class ConnectionManager {
|
||||
|
||||
// 单例实例(静态内部类实现线程安全延迟加载)
|
||||
private static class Holder {
|
||||
static final ConnectionManager INSTANCE = new ConnectionManager();
|
||||
}
|
||||
|
||||
// 私有构造函数(禁止外部实例化)
|
||||
private ConnectionManager() {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
|
||||
}
|
||||
|
||||
// 全局访问点
|
||||
public static ConnectionManager getInstance() {
|
||||
return Holder.INSTANCE;
|
||||
}
|
||||
|
||||
// 使用线程安全的ConcurrentHashMap存储连接
|
||||
private final ConcurrentHashMap<String, SydApi4j> connectionCache = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 获取或创建指定URL的连接
|
||||
* @param url 目标服务地址
|
||||
* @return 已存在的或新建的SydReq连接
|
||||
* @throws IOException 当创建连接失败时抛出
|
||||
*/
|
||||
public SydApi4j getConnection(String url, int timeout) throws Exception {
|
||||
// 使用compute方法保证原子操作
|
||||
return connectionCache.compute(url, (key, existingConn) -> {
|
||||
try {
|
||||
// 验证现有连接有效性
|
||||
if (isConnectionValid(existingConn)) {
|
||||
return existingConn;
|
||||
}
|
||||
|
||||
// 关闭失效连接(如果存在)
|
||||
closeConnectionQuietly(existingConn);
|
||||
|
||||
// 创建新连接
|
||||
return createNewConnection(url, timeout);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("连接创建失败", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 连接有效性验证
|
||||
private boolean isConnectionValid(SydApi4j conn) {
|
||||
return conn != null && conn.isActive();
|
||||
}
|
||||
|
||||
// 安全关闭连接
|
||||
private void closeConnectionQuietly(SydApi4j conn) {
|
||||
try {
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 创建新连接(提取共用逻辑)
|
||||
private SydApi4j createNewConnection(String url, int timeout) throws Exception {
|
||||
try {
|
||||
String[] addressParts = parseUrl(url);
|
||||
String ip = addressParts[0];
|
||||
int port = Integer.parseInt(addressParts[1]);
|
||||
|
||||
SydApi4j newConn = new SydApi4j();
|
||||
newConn.connect(ip, port, null, timeout);
|
||||
return newConn;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("连接创建失败: " + url, e);
|
||||
}
|
||||
}
|
||||
|
||||
// URL解析验证
|
||||
private String[] parseUrl(String url) throws IllegalArgumentException {
|
||||
String[] parts = url.split(":");
|
||||
if (parts.length != 2) {
|
||||
throw new IllegalArgumentException("无效的URL格式: " + url);
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭指定URL的连接并从缓存移除
|
||||
* @param url 需要关闭的服务地址
|
||||
*/
|
||||
public void closeConnection(String url) {
|
||||
SydApi4j conn = connectionCache.remove(url);
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.disconnect(); // 假设这是关闭连接的方法
|
||||
System.out.println("连接已关闭: " + url);
|
||||
} catch (Exception e) {
|
||||
System.err.println("关闭连接异常 [" + url + "]: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭所有连接并清空缓存
|
||||
*/
|
||||
public void closeAllConnections() {
|
||||
// 创建当前URL的副本避免并发修改
|
||||
Set<String> urls = ConcurrentHashMap.newKeySet();
|
||||
urls.addAll(connectionCache.keySet());
|
||||
|
||||
urls.forEach(this::closeConnection);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前缓存中的连接数量
|
||||
*/
|
||||
public int size() {
|
||||
return connectionCache.size();
|
||||
}
|
||||
|
||||
private void shutdown() {
|
||||
connectionCache.forEach((url, conn) -> {
|
||||
conn.disconnect();
|
||||
});
|
||||
connectionCache.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user