📄 crbtclientcontroller.java
字号:
package com.wireless.crbt.gwif.workthread;
import java.util.*;
import com.wireless.crbt.gwif.global.*;
import com.wireless.crbt.gwif.netty2.*;
//import com.wireless.gwif.socketconn.*;
public class CrbtClientController {
private static Timer timer = null;
private static Vector templetContainer = new Vector();
private static Map templetStatus = new Hashtable();
private static final long MILLISECONDS = 1000;
private static final long SLEEPTIME = 60 * 1000;
//为了防止一次断开引起多次重连时的机制,对重连的对象进行hash映射
//对于在table里存在的对象,不进行重连
public static Hashtable htConnObject = new Hashtable();
public synchronized static void restart(ClientDefaultCrbtConnTemplet templet){
if (templetStatus.containsKey(templet)) {
long time = Long.parseLong( (String) templetStatus.get(templet));
if (System.currentTimeMillis() - time < MILLISECONDS) {
LoggerConstant.log.info("Same templet restart in " + MILLISECONDS + " milliseconds will be descard ...");
return;
}
LoggerConstant.log.info("Templet " + templet + " will be restart !");
}
LoggerConstant.log.info("压入堆栈 :"+templet);
templetContainer.add(templet);
templetStatus.put(templet, System.currentTimeMillis() + "");
}
public static void start(){
if( timer == null ){
LoggerConstant.log.info("------启动连接控制器");
timer = new Timer(true);
timer.schedule(new TimerTask(){
public void run(){
ClientDefaultCrbtConnTemplet templet = null;
try{
if( templetContainer.size() == 0 )
return;
int total = templetContainer.size();
LoggerConstant.log.info("需要重新启动的templet数 :"+total);
for (int i=0 ; i < total; i++) {
templet = (ClientDefaultCrbtConnTemplet) templetContainer.elementAt(i);
LoggerConstant.log.info("准备重启 :"+templet);
restartByTemplet(templet);
}
while( (--total) >= 0 ){
templetContainer.removeElementAt(total);
}
LoggerConstant.log.info("检测容量templetContainer.size() :"+templetContainer.size());
}
catch(Exception e){
CrbtGWIFGlobal.sendMonitor("I000001");
LoggerConstant.log.error("Exception : ", e);
LoggerConstant.log.info("有异常产生,Timer重启 :"+templet);
restart(templet);
}
}
}, 1000, 1000);
}
}
public static void stop(){
if( timer != null ){
timer.cancel();
timer = null;
}
}
public static void restartAll(){
try{
LoggerConstant.log.info("重新启动所有连接 ...");
for (int i = 0; i < CrbtGWIFGlobal.vManage.size(); i++) {
Object tmpClient = CrbtGWIFGlobal.vManage.elementAt(i);
if (tmpClient instanceof CrbtClient) {
CrbtClient client = (CrbtClient) tmpClient;
restart(client.getConnTemplet());
}
}
}catch(Exception e){
LoggerConstant.log.error("Restart All Exception : ", e);
}
}
public static void quickRestart(ClientDefaultCrbtConnTemplet templet){
try{
if(htConnObject.contains(templet)){
String stime = (String) htConnObject.get(templet);
if ( System.currentTimeMillis() - (Integer.parseInt(stime) ) < SLEEPTIME) {
return;
}
}
htConnObject.put(templet, System.currentTimeMillis() + "");
LoggerConstant.log.info("快速重新连接 ...");
for (int i = 0; i < CrbtGWIFGlobal.vManage.size(); i++) {
Object tmpClient = CrbtGWIFGlobal.vManage.elementAt(i);
if (tmpClient instanceof CrbtClient) {
CrbtClient client = (CrbtClient) tmpClient;
if (client.getConnTemplet() == templet) {
client.restart();
break;
}
}
}
}
catch(Exception e){
LoggerConstant.log.error("Quick Restart Exception : ", e);
}
}
public static void quickClose(ClientDefaultCrbtConnTemplet templet){
try{
LoggerConstant.log.info("关闭连接 ...");
for (int i = 0; i < CrbtGWIFGlobal.vManage.size(); i++) {
Object tmpClient = CrbtGWIFGlobal.vManage.elementAt(i);
if (tmpClient instanceof CrbtClient) {
CrbtClient client = (CrbtClient) tmpClient;
if (client.getConnTemplet() == templet) {
client.close();
break;
}
}
}
}
catch(Exception e){
LoggerConstant.log.error("Quick Close Exception : ", e);
}
}
private static void restartByTemplet(ClientDefaultCrbtConnTemplet templet){
LoggerConstant.log.info("重新连接...CrbtGWIFGlobal.vManage.size():"+CrbtGWIFGlobal.vManage.size());
for (int j = 0; j < CrbtGWIFGlobal.vManage.size(); j++) {
Object tmpClient = CrbtGWIFGlobal.vManage.elementAt(j);
LoggerConstant.log.info("CrbtGWIFGlobal.vManage.elementAt(j):"+CrbtGWIFGlobal.vManage.elementAt(j));
if (tmpClient instanceof CrbtClient) {
CrbtClient client = (CrbtClient) tmpClient;
if (client.getConnTemplet() == templet) {
LoggerConstant.log.info("client.getConnTemplet() == templet");
restartClient(client);
break;
}
}
}
}
private static void restartClient(CrbtClient client){
client.stop();
int j = 0;
for (; !client.isFinishStop() && j < 60; j++) {
try {
LoggerConstant.log.info("Thread.sleep(1000):" + j);
Thread.sleep(1000);
}
catch (InterruptedException ex) {}
}
LoggerConstant.log.info("客户端已关闭,等待重起 ... " + j);
try {
Thread.sleep(SLEEPTIME);
LoggerConstant.log.info("Thread.sleep(SLEEPTIME) over!!");
}
catch (Exception e) {
}
client.start();
LoggerConstant.log.info("客户端重起完毕!");
try {
Thread.sleep(5000);
}
catch (Exception e) {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -