打造自己的Linux服务器监控小工具
作者:网络转载 发布时间:[ 2014/7/18 14:26:40 ] 推荐标签:linux 操作系统
2 LinuxConnectionPool
|
package com.sunfan.monitor.manager.pool;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.sunfan.monitor.manager.IConnectionPool;
import com.sunfan.monitor.platform.IConnectable;
import com.sunfan.monitor.platform.linux.LinuxConnection;
/**
*
* @author sunfan
*
*/
@Component
public class LinuxConnectionPool implements IConnectionPool {
private Logger logger = LoggerFactory.getLogger(getClass());
private Map<String, IConnectable> connectionPool = new HashMap<String, IConnectable>();
/**
* save connecion in the connectionPool,if conn is already exist return
* false else return true
*
* @param conn
* @return
*/
public synchronized Boolean saveObject(IConnectable conn) {
String key = rewardConnectionKey(conn);
if (isExist(key)) {
logger.info("this key{} has already exist", key);
return false;
}
connectionPool.put(key, conn);
return true;
}
/**
* borrow connection object in the connect-pool
*
* @param key
* @return
*/
public IConnectable borrowObject(String key) {
if (!isExist(key)) {
throw new IllegalArgumentException("key not found:" + key);
}
return connectionPool.get(key);
}
public IConnectable borrowObject(String url,String user,String password){
String key = this.rewardConnectionKey(url, user, password);
if (!isExist(key)){
try {
LinuxConnection connect = new LinuxConnection(url, user, password);
connectionPool.put(key, connect);
return connect;
} catch (IOException e) {
throw new RuntimeException("connection error"+url,e);
}
}else {
return connectionPool.get(key);
}
}
/**
* borrow connection object in the connect-pool if the connection hasn't in
* the connectionPool return null,else return connect object
*
* @param conn
* @return
*/
public IConnectable borrowObject(IConnectable conn) {
String key = rewardConnectionKey(conn);
return borrowObject(key);
}
/**
* close single connection in the connection-pool and close/release of this
* connection
*
* @param conn
* @throws IOException
*/
public void remove(IConnectable conn) throws IOException {
String key = rewardConnectionKey(conn);
remove(key);
}
/**
* close single connection in the connection-pool and close/release of this
* connection
*
* @param conn
* @throws IOException
*/
public synchronized void remove(String key) throws IOException {
if (!isExist(key)) {
throw new IllegalArgumentException(key + "is not exist");
}
connectionPool.get(key).close();
connectionPool.remove(key);
}
/**
* delete every connection in the connection-pool and also close/release of
* all connection
*
* @throws IOException
*/
public void clear() throws IOException {
for (String keyString : connectionPool.keySet()) {
connectionPool.get(keyString).close();
}
connectionPool.clear();
}
/**
* according to the connection to generate key if the connecion is not equal
* null return url/usr/password
*
* @param conn
* @return
*/
public String rewardConnectionKey(IConnectable conn) {
return conn.getUrl() + "/" + conn.getUser() + "/" + conn.getPassword();
}
public String rewardConnectionKey(String url, String user, String password) {
return url + "/" + user + "/" + password;
}
/**
* To confirm whether the connectionPool has this key if already has return
* true else return false
*
* @param key
* @return
*/
public Boolean isExist(String key) {
return connectionPool.containsKey(key);
}
}
|
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
Linux下开源的DDR压力测试工具曝Linux恶意软件:让树莓派设备挖掘数字货币linux系统中不同颜色的文件夹及根目录介绍软件测试工程师必知必会Linux命令Linux下DNS服务器配置如何成为不可替代的Linux运维工程师?详解Linux进程(作业)的查看和杀死Linux 日志定时轮询流程详解比特币勒索病毒不只Windows系统有,Linux版的来了Linux日志定时轮询流程详解Linux iommu和vfio概念空间解构Linux系统如何低于TCP洪水攻击Linux无损调整分区大小Linux下防火墙配置实例Linux使用Jexus托管Asp.Net Core应用程序Linux中引号的那些事

sales@spasvo.com