Java tutorial
/* Copyright (C) 2011 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.agent; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.ws.agent.AgentOutputBasicInfo; import com.clustercontrol.ws.agent.JobInfoNotFound_Exception; import com.clustercontrol.ws.agent.OutputBasicInfo; import com.clustercontrol.ws.jobmanagement.RunResultInfo; /** * ????Queue??<BR> * * ??????? * ?????? * */ public class SendQueue { // static private Log m_log = LogFactory.getLog(SendQueue.class); private static final long SEND_TIMEOUT = 60 * 1000l; private long m_sendQueueReconnectionInterval = 10 * 1000l; /** * * @param props */ public SendQueue() { super(); } /** * ????<BR> * * ?????????<BR> * ?????? * @param msg */ public boolean put(Object info) { m_log.debug("put() start : " + info.getClass().getCanonicalName()); while (!ReceiveTopic.isHistoryClear()) { m_log.debug("put() while (!ReceiveTopic.isHistoryClear()) is true"); boolean sendQueueStatus = false; ExecutorService es = null; Future<Boolean> task = null; try { String id = ""; // Executor?? SenderThreadFactory threadFactory = new SenderThreadFactory(id); es = Executors.newSingleThreadExecutor(threadFactory); // Queue?? // ?Queue?????????????????? // Future.get()???????? m_log.debug("put() submit"); task = es.submit(new Sender(info)); sendQueueStatus = task.get(SEND_TIMEOUT, TimeUnit.MILLISECONDS); } catch (Exception e) { // Queue?????????????Future.get()??????? // ???? // ? m_log.warn("put() : Failed to connect to MGR " + e.getMessage(), e); } finally { // if (task != null) { task.cancel(true); } if (es != null) { es.shutdown(); } m_log.debug("put() end : " + info.getClass().getCanonicalName()); } // ??????????sleep???? // Queue???????? if (sendQueueStatus) { m_log.debug("put() return true : " + info.getClass().getCanonicalName()); return true; } else { // sleep???QueueConnection?QueueSession ?? try { m_log.debug("put() reput interval sleep: " + m_sendQueueReconnectionInterval + " sec"); Thread.sleep(m_sendQueueReconnectionInterval); } catch (InterruptedException e1) { m_log.error("put() reput interval sleep: ", e1); } } } // End While Loop return false; } /** * Queue??? */ private static class Sender implements Callable<Boolean> { //private RunResultInfo m_info; private Object m_info; public Sender(Object info) { m_info = info; } @Override public Boolean call() throws Exception { try { if (m_info instanceof RunResultInfo) { RunResultInfo resultInfo = (RunResultInfo) m_info; m_log.info("Sender Send RunResultInfo : SessionID=" + resultInfo.getSessionId() + ", JobID=" + resultInfo.getJobId() + ", CommandType=" + resultInfo.getCommandType() + ", Status=" + resultInfo.getStatus()); AgentEndPointWrapper.jobResult(resultInfo); } else if (m_info instanceof OutputBasicInfo) { OutputBasicInfo message = (OutputBasicInfo) m_info; m_log.info("Sender Send Message : message =" + message); AgentOutputBasicInfo info = new AgentOutputBasicInfo(); info.setOutputBasicInfo(message); AgentEndPointWrapper.sendMessage(info); } else { m_log.error("Sender Send Object is not unknown = " + m_info.getClass().getName()); return false; } return true; } catch (JobInfoNotFound_Exception e) { m_log.info("call() : JobInfo has been already deleted"); return true; } catch (Exception e) { throw e; } } } /** * Queue????ThreadFactory */ private static class SenderThreadFactory implements ThreadFactory { private final String m_threadName; public SenderThreadFactory(String threadName) { m_threadName = threadName; } @Override public Thread newThread(Runnable r) { return new Thread(r, "Sender-" + m_threadName); } } }