Timer & TimerTask sample

invoke:

    private static Timer timer = new Timer(true);
    private static DiscoveryTask discover = new DiscoveryTask();

 

    DiscoveryTask.productModuleGuid = productModuleGuid;
    timer.schedule(discover, 0, 60*1000);

 

 

package com.sybase.uep.sysadmin.management.runtime;

import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sybase.scc.sdk.ManagedObjectType;
import com.sybase.scc.sdk.SccApplication;
import com.sybase.scc.sdk.SccSdk;
import com.sybase.scc.sdk.SccSdkException;
import com.sybase.sup.admin.scc.vo.ServerVO;
import com.sybase.ua.AgentConnection;
import com.sybase.ua.AgentConnectionFactory;
import com.sybase.ua.AgentInfo;
import com.sybase.ua.AgentURL;
import com.sybase.ua.PluginInfo;
import com.sybase.ua.service.AgentServiceException;
import com.sybase.ua.services.Agent;

public class DiscoveryTask extends TimerTask {
    private Log log = LogFactory.getLog(getClass());
    private static final String REQUIRED_SCC_VERSION = "3.0.0";
    private static boolean RUNNING = false;
    public static String productModuleGuid;
   
    @Override
    public void run() {
        if (RUNNING == true) {
            // previous schedule is still running, exit it.
            return;
        } else {
            RUNNING = true;
        }
        try {
            // get SelfDiscoveryService object name
            SccApplication sccApplication =
                 SccSdk.getSccApplication(null,Agent.getInstance().getMBeanServer());
            ManagedObjectType childType = sccApplication.getProductModule(productModuleGuid).getManagedObjectType(
                    "com.sybase.uep.sysadmin.management.mo.ClusterMO", "2.0.0.0");
            String agentPluginId = "com.sybase.uep.admin.agent.plugin";
            String agentPluginVersion = "2.0.0";
            String discoveryServiceId = "SelfDiscoveryService";
            List<AgentInfo> agentInfoList = (List<AgentInfo>)Agent.getInstance().invokeAgentServiceMethod(
                    discoveryServiceId,
                    "discoverAgents",
                    new Object[] {},
                    new String[] {});
            for (AgentInfo agentInfo:agentInfoList) {
                String version = agentInfo.getVersion();
                if (REQUIRED_SCC_VERSION.compareToIgnoreCase(version) > 0) {
                    continue;
                }
                String url = agentInfo.getConnectionURL();
                AgentURL aUrl = new AgentURL(url);
                AgentConnection ac = AgentConnectionFactory.createAgentConnection(
                        aUrl.getProtocol(), aUrl.getHost(), aUrl.getPort());
                try {
                    ac.connect(500);
                } catch (Exception e) {
                    log.error("Discover: Fail to connect " + aUrl.getHost());
                    continue;
                }
                AgentInfo ai = ac.getAgentInfo();
                List<PluginInfo> pis = ai.getPluginInfos(agentPluginId, agentPluginVersion);
                String supServerPath = "";
                for (Iterator iter = pis.iterator(); iter.hasNext();) {
                    PluginInfo pi = (PluginInfo) iter.next();
                    // only allow Instance number as 1
                    if (pi.getInstanceNumberAsString().equals("1")) {
                        Map targetProperties = (Map) ac.getAgentPluginProperty(
                                agentPluginId, agentPluginVersion, 1, "properties");
                        supServerPath = (String)targetProperties.get("sup.server.path");
                    } else {
                        log.error(ai.getHost() + ":plugin Instance Number is more than 1.");
                    }
                }
                if (supServerPath == null || supServerPath.isEmpty()) {
                    log.error("Discover: " + ai.getHost() + ":sup.server.path is not defined in agent-plugin.xml.");
                    continue;
                }
                // TODO invokeAgentPluginMethod to get remote agent infos
                ServerVO svo = (ServerVO)ac.invokeAgentPluginMethod(
                        agentPluginId,
                        agentPluginVersion,
                        1,
                        "getServerVO",
                        new Object[] {supServerPath},
                        new String[] {"java.lang.String"});
                log.info("Discover: " + agentInfo.getHost() + "/" + svo.getPort());
//                ManagedObject managedObject =
//                    sccApplication.createManagedObject(productModuleGuid,
//                            childType.getTypeClass(),
//                            childType.getVersion(),
//                            agentInfo.getHost(),
//                            "SUP Cluster");
//              
//                Properties connectionProperties = new Properties();
//                connectionProperties.put("host", agentInfo.getHost());
//                connectionProperties.put("port", Integer.toString(port));
//                managedObject.addConnectionProfile("Server", connectionProperties);
//                managedObject.getClass();
//                foundDataserverGuids.add(managedObject.getGuid());
            }
        } catch (AgentServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SccSdkException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        RUNNING = false;
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章