日期:2014-05-16  浏览次数:20425 次

验证数据库连接,ip端口号连接状态
server1=10.10.10.91:9001
server3=10.10.10.91:8080
server2=10.10.10.60:8080
server4=10.10.10.82:9001

?

public class GetCfg {

	public  List getAllValues(){
	    Properties pro = new Properties();
		try {
			pro.load(this.getClass().getClassLoader().getResourceAsStream("conf.properties"));
		} catch (IOException e1) {
		   e1.printStackTrace();
		}
		List list=new ArrayList();
		Enumeration e = pro.elements();
		while(e.hasMoreElements()){
			String s=(String) e.nextElement();
			list.add(s);
		}
		pro.clear();
		return list;
  }
	
}

?

public class CntvCheckConnection extends ControllerBase {
	
	private final static Logger logger = Logger.getLogger(CntvCheckConnection.class);

	 /**
     * 判断数据库有无连接成功
     */
    public static void cntvCheckConnection_index() {
    	String check="数据库连接成功!"+"<br/><br/>";
    	EntityManagerFactory ef=null;
    	try{
    	   ef=Persistence.createEntityManagerFactory("sqlserver");
    	}catch(Exception e){
    		e.printStackTrace();
    		logger.info(e.getMessage());
    		check="数据库连接失败!"+"<br/>";
    		
    	}finally{
    		ef.close();
    	}
    	String checktemp=checkip();
    	String s1="<div style='font-size:16px'>";
    	String s2="</div>";
    	check=s1+check+checktemp+s2;
    	renderHtml(check);
    }
    
    public static String checkip(){
    	GetCfg gc=new GetCfg();
    	List s=gc.getAllValues();
    	StringBuffer checktemp=new StringBuffer();
		for(int i=0;i<s.size();i++){
			String temp=(String) s.get(i);
			String [] cc=temp.split(":");
		    String host=cc[0];
		    int port=Integer.valueOf(cc[1]);
		    String msg=bindPort(host,port);
		    System.out.println(host+":"+port);
		    
		    checktemp.append(host+":");
		    checktemp.append(port+"&nbsp;&nbsp;&nbsp;");
		    checktemp.append(msg+"    ");
		    checktemp.append("<br/>"); 
		}
		return checktemp.toString();
    }
    
	public static String bindPort(String host, int port) {
		String isBind="正常";
		Socket client = null;
		try{
		    client = new Socket(host, port);
		    client.close();
		}catch(Exception e){
		    isBind="不正常";
		}

		return isBind;
	}
}

?