// Path: src/main/java/org/mycat/web/task/server/CheckMycatSuspend.java
// @Service
// public class CheckMycatSuspend extends BaseService implements ITask {
// 	private String dbName;
// 	
// 	private static final String NAMESPACE = "MYCATSHOWPROCESSOR";
// 	
// 	private static final int timeout = 10;  //宕机timeout：10S
// 	
// 	@Override
// 	public void excute(String dbName, Date nowDate) {
// 		
// 		DataSource dbSource = (DataSource)SpringApplicationContext.getBean(dbName + MycatPortType.MYCAT_MANGER + "dataSource");
// 		Connection conn = null;
// 		try {
// 			conn = dbSource.getConnection();
// 			if(!conn.isValid(timeout)){
// 				MailUtil.send("Mycat死机", dbName+"死机");
// 				return;
// 			}
// 		} catch (Exception e) {
// 			e.printStackTrace();
// 			try {
// 				MailUtil.send("Mycat死机", dbName+"死机");
// 			} catch (MessagingException e1) {
// 				e1.printStackTrace();
// 			}
// 			return;			
// 		}finally{
// 			if(conn != null){
// 				try {
// 					conn.close();
// 				} catch (SQLException e) {
// 					e.printStackTrace();
// 				}
// 			}
// 		}
// 		 
// 		this.dbName = dbName;
// 
// 		ShowService showService = (ShowService)SpringApplicationContext.getBean("showService"); 
// 		showService.getDao().query(dbName + MycatPortType.MYCAT_MANGER, NAMESPACE, "testMycat");
// 	}
// 
// 	public String getDbName(){
// 		return this.dbName;
// 	}
// 	
// }


import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.mycat.web.task.server.CheckMycatSuspend;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

		}
	}
	
	public void addDBName(String dbName){
		if(dbName != null){
			if(!dbNames.contains(dbName)){
				dbNames.add(dbName);
			}
		}
	}
	
	public void addTask(final ITask task,final long period, String taskName,final int timeout){
		//间隔一定时间，触发一次监测事件
		ScheduledFuture<?> sf = scheduler.scheduleAtFixedRate(new Runnable() {  
            public void run() {
            	//一次监测事件，根据DB数量，一个DB，开一个线程监测
				final Date nowDate = new Date();
        		for(final String dbName : dbNames){
        			try{
		        		ScheduledFuture<?> sf = scheduler.schedule(new Runnable() {  
		        			public void run() {
		                		task.excute(dbName, nowDate);		
		        			}
		        		}, 0, TimeUnit.MILLISECONDS);  
		        		try {
		        			if (timeout != -1)
		        				sf.get(timeout, TimeUnit.SECONDS);
		        		} catch (TimeoutException e) {
		        			//主线程等待计算结果超时，因此中断任务线程！
		        			e.printStackTrace();

// Path: src/main/java/org/mycat/web/task/server/CheckMycatSuspend.java
// @Service
// public class CheckMycatSuspend extends BaseService implements ITask {
// 	private String dbName;
// 	
// 	private static final String NAMESPACE = "MYCATSHOWPROCESSOR";
// 	
// 	private static final int timeout = 10;  //宕机timeout：10S
// 	
// 	@Override
// 	public void excute(String dbName, Date nowDate) {
// 		
// 		DataSource dbSource = (DataSource)SpringApplicationContext.getBean(dbName + MycatPortType.MYCAT_MANGER + "dataSource");
// 		Connection conn = null;
// 		try {
// 			conn = dbSource.getConnection();
// 			if(!conn.isValid(timeout)){
// 				MailUtil.send("Mycat死机", dbName+"死机");
// 				return;
// 			}
// 		} catch (Exception e) {
// 			e.printStackTrace();
// 			try {
// 				MailUtil.send("Mycat死机", dbName+"死机");
// 			} catch (MessagingException e1) {
// 				e1.printStackTrace();
// 			}
// 			return;			
// 		}finally{
// 			if(conn != null){
// 				try {
// 					conn.close();
// 				} catch (SQLException e) {
// 					e.printStackTrace();
// 				}
// 			}
// 		}
// 		 
// 		this.dbName = dbName;
// 
// 		ShowService showService = (ShowService)SpringApplicationContext.getBean("showService"); 
// 		showService.getDao().query(dbName + MycatPortType.MYCAT_MANGER, NAMESPACE, "testMycat");
// 	}
// 
// 	public String getDbName(){
// 		return this.dbName;
// 	}
// 	
// }


// Path: src/main/java/org/mycat/web/task/common/TaskManger.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.mycat.web.task.server.CheckMycatSuspend;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

		}
	}
	
	public void addDBName(String dbName){
		if(dbName != null){
			if(!dbNames.contains(dbName)){
				dbNames.add(dbName);
			}
		}
	}
	
	public void addTask(final ITask task,final long period, String taskName,final int timeout){
		//间隔一定时间，触发一次监测事件
		ScheduledFuture<?> sf = scheduler.scheduleAtFixedRate(new Runnable() {  
            public void run() {
            	//一次监测事件，根据DB数量，一个DB，开一个线程监测
				final Date nowDate = new Date();
        		for(final String dbName : dbNames){
        			try{
		        		ScheduledFuture<?> sf = scheduler.schedule(new Runnable() {  
		        			public void run() {
		                		task.excute(dbName, nowDate);		
		        			}
		        		}, 0, TimeUnit.MILLISECONDS);  
		        		try {
		        			if (timeout != -1)
		        				sf.get(timeout, TimeUnit.SECONDS);
		        		} catch (TimeoutException e) {
		        			//主线程等待计算结果超时，因此中断任务线程！
		        			e.printStackTrace();

		        			if (task instanceof CheckMycatSuspend){
