从VMware的vCenter中读取事件

从VMware的vCenter中读取事件,每几分钟从vCenter中的事件管理中读取事件,得到事件后,再对事件做处理。上代码。

连接vCenter的连接类



import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.vmware.vim25.mo.ServiceInstance;

/**
 * @Title: vCenter连接池
 */

public class VmwareConnectionUtil  {
	private static Logger logger = LogManager.getLogger(VmwareConnectionUtil.class);

	private static final VmwareConnectionUtil instance = new VmwareConnectionUtil();

	private Map<String, ServiceInstance> poolMap = new HashMap<String, ServiceInstance>(); // Map<ip,ServiceInstance>

	private static final int GETTESTSTATUSTIMEOUT = 60;

	private VmwareConnectionUtil() {
	}

    /**
     * 单例模式
     * @return
     */
    public static synchronized VmwareConnectionUtil getInstance() {
        return instance;
    }

    /**
     * 建立与vCenter的连接
     * @param url数组: IP, port, username, password
     * @return
     */
    public synchronized ServiceInstance getConnection(String[] url) {
        String key = getKey(url);
        ServiceInstance conn = poolMap.get(key);

        if (conn != null) {
            try {
                conn.currentTime();
            } catch (Exception e) {
            	logger.error("Get VmWare vCenter connection exception", e);
            	poolMap.remove(key);
                conn.getServerConnection().logout();
                conn = null;
            }
        } else {
            poolMap.remove(key);
            conn = initConnection(url);
            poolMap.put(key, conn);
        }

        return conn;
    }

    private ServiceInstance initConnection(String[] param) {
    	try {
//			ignoreSsl();
		} catch (Exception e1) {
			logger.error("Ignore SSL error", e1);
		}
    	
        ServiceInstance conn = null;
        URL url = null;
        String spec = "";
        if(param[1] == null || param[1].trim().equals(""))
        	spec = "https://" + param[0] + "/sdk";	//没有端口
        else
        	spec = "https://" + param[0] + ":" + param[1] + "/sdk";
    
        try {
        	url = new URL(spec);
        	logger.info("Connect to VmWare vCenter " + url);
        } catch (MalformedURLException e) {
            logger.error("Connect to VmWare vCenter exception", e);
            return conn;
        }

        try {
            conn = new ServiceInstance(url, param[2], param[3], true);
        } catch (Exception e) {
            if (conn != null) {
                conn.getServerConnection().logout();
            }
            conn = null;
            logger.error("Failed to init VmWare vCenter webservice connection URL " +  spec + ", user:" + param[2], e);
        }

        return conn;
    }

    private ServiceInstance initConnectTimeOut(final String[] param) {
        ServiceInstance conn = null;
        final ExecutorService exec = Executors.newFixedThreadPool(1);
        Callable<ServiceInstance> call = new Callable<ServiceInstance>() {
            
            public ServiceInstance call() throws Exception {
                ServiceInstance conn = initConnection(param);
                return conn;
            }
        };
        try {
            Future<ServiceInstance> future = exec.submit(call);
            conn = future.get(GETTESTSTATUSTIMEOUT, TimeUnit.SECONDS); // 任务处理超时时间设为60 秒
  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值