Java tutorial
/* Copyright (C) 2006 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.monitor.run.factory; import java.io.Serializable; import java.util.Collection; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.bean.HinemosModuleConstant; import com.clustercontrol.commons.scheduler.TriggerSchedulerException; import com.clustercontrol.commons.util.EmptyJpaTransactionCallback; import com.clustercontrol.commons.util.JpaTransactionManager; import com.clustercontrol.fault.HinemosUnknown; import com.clustercontrol.fault.MonitorNotFound; import com.clustercontrol.monitor.run.bean.QuartzConstant; import com.clustercontrol.monitor.run.model.MonitorInfo; import com.clustercontrol.monitor.run.session.MonitorRunManagementBean; import com.clustercontrol.monitor.run.util.QueryUtil; import com.clustercontrol.plugin.impl.SchedulerPlugin; import com.clustercontrol.plugin.impl.SchedulerPlugin.SchedulerType; import com.clustercontrol.plugin.impl.SchedulerPlugin.TriggerType; import com.clustercontrol.util.HinemosTime; /** * ?<BR> * * @version 4.0.0 * @since 2.0.0 */ public class ModifySchedule { /** ? */ private static Log m_log = LogFactory.getLog(ModifySchedule.class); /** * DB??????????? * @throws TriggerSchedulerException * * @since 4.0.0 */ public void updateScheduleAll() throws HinemosUnknown { m_log.debug("updateScheduleAll()"); JpaTransactionManager jtm = null; Throwable exception = null; Collection<MonitorInfo> entityList = null; try { jtm = new JpaTransactionManager(); jtm.begin(); entityList = QueryUtil.getAllMonitorInfo(); jtm.commit(); } catch (Exception e) { String msg = "updateScheduleAll() " + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.warn(msg, e); if (jtm != null) jtm.rollback(); exception = e; } finally { if (jtm != null) jtm.close(); } if (entityList != null) { for (MonitorInfo entity : entityList) { try { // ???????? if (entity.getMonitorFlg() || entity.getCollectorFlg()) { updateSchedule(entity, true); } } catch (Exception e) { m_log.info("updateScheduleAll() scheduleJob : monitorId = " + entity.getMonitorId() + ", " + e.getClass().getSimpleName() + ", " + e.getMessage()); // ??????throw???? exception = e; } } } if (exception != null) { throw new HinemosUnknown("An error occurred while scheduling the trigger.", exception); } } /** * ????? * <p> * <ol> * <li>ID?ID????????</li> * <li>???????????</li> * <dl> * <dt></dt> * <dd>ID</dd> * </dl> * <li>?????</li> * </ol> * * @param monitorId ID * @throws TriggerSchedulerException * @since 2.0.0 */ protected void updateSchedule(String monitorId) throws HinemosUnknown { m_log.debug("updateSchedule() : monitorId=" + monitorId); MonitorInfo entity = null; try { entity = QueryUtil.getMonitorInfoPK_NONE(monitorId); } catch (MonitorNotFound e) { String msg = "updateSchedule() found no scheduleJob : monitorId = " + monitorId; TriggerSchedulerException e1 = new TriggerSchedulerException(msg); throw new HinemosUnknown(msg, e1); } catch (Exception e) { String msg = "updateSchedule() scheduleJob : monitorId = " + monitorId + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.warn(msg, e); throw new HinemosUnknown(msg, e); } final MonitorInfo entityCopy = entity; JpaTransactionManager jtm = new JpaTransactionManager(); jtm.addCallback(new EmptyJpaTransactionCallback() { @Override public void postCommit() { try { updateSchedule(entityCopy, false); } catch (HinemosUnknown e) { m_log.error(e); } } }); } private void updateSchedule(MonitorInfo entity, boolean isInitManager) throws HinemosUnknown { String monitorId = entity.getMonitorId(); String monitorTypeId = entity.getMonitorTypeId(); TriggerType type = null; try { type = TriggerType.valueOf(entity.getTriggerType()); } catch (IllegalArgumentException e) { m_log.info("updateSchedule() Invalid TRIGGER_TYPE. monitorTypeId = " + monitorTypeId + ", + monitorId = " + monitorId); return; } switch (type) { case SIMPLE: int interval = entity.getRunInterval(); m_log.debug("Schedule SimpleTrigger. monitorId = " + monitorId); switch (monitorTypeId) { case HinemosModuleConstant.MONITOR_PROCESS: case HinemosModuleConstant.MONITOR_PERFORMANCE: // ????????????Quartz??????????? break; default: //JobDetail???? // ID Serializable[] jdArgs = new Serializable[3]; @SuppressWarnings("unchecked") Class<? extends Serializable>[] jdArgsType = new Class[3]; // 1 jdArgsType[0] = String.class; jdArgs[0] = monitorTypeId; // 2ID jdArgsType[1] = String.class; jdArgs[1] = monitorId; // 3: jdArgsType[2] = Integer.class; jdArgs[2] = entity.getMonitorType(); // SimpleTrigger ? // ????????? if (entity.getMonitorFlg() || entity.getCollectorFlg()) { SchedulerPlugin.scheduleSimpleJob(SchedulerType.RAM, monitorId, monitorTypeId, calcSimpleTriggerStartTime(interval, entity.getDelayTime()), interval, true, MonitorRunManagementBean.class.getName(), QuartzConstant.MONITOR_METHOD_MONITOR_AGGREGATED, jdArgsType, jdArgs); } break; } break; case CRON: // CRON????? throw new UnsupportedOperationException(); case NONE: // ??? break; } } /** * ???Quartz???? * * @param monitorTypeId ID * @param monitorId ID * @throws TriggerSchedulerException * * @see com.clustercontrol.monitor.run.bean.QuartzConstant * @see com.clustercontrol.commons.util.QuartzUtil#getQuartzManager() * @see com.clustercontrol.quartzmanager.ejb.session.QuartzManager#deleteSchedule(java.lang.String, java.lang.String) */ protected void deleteSchedule(final String monitorTypeId, final String monitorId) throws HinemosUnknown { m_log.debug("deleteSchedule() : type =" + monitorTypeId + ", id=" + monitorId); JpaTransactionManager jtm = new JpaTransactionManager(); jtm.addCallback(new EmptyJpaTransactionCallback() { @Override public void postCommit() { try { SchedulerPlugin.deleteJob(SchedulerType.RAM, monitorId, monitorTypeId); } catch (HinemosUnknown e) { m_log.error(e); } } }); } // /** // * Cron????? // * // * @param schedule // * @return Cron? // */ // private String getCronString(int interval, int delayTime){ // String cronString = null; // if(interval > 0 && interval < 3600){ // int minute = interval / 60; // // // Quartz?cron? 30 */1 * * * ? * // cronString = delayTime + " */" + minute + " * * * ? *"; // } else if(interval > 0 && interval >= 3600){ // int hour = interval / 3600; // // // Quartz?cron? 30 0 */1 * * ? * // cronString = delayTime + " 0 */" + hour + " * * ? *"; // } // // m_log.debug("getCronString() interval = " + interval + ", delayTime = " + delayTime + ", cronString = " + cronString); // return cronString; // } /** * ??? * @param intervalSec * @param delayTimeSec ? * @return ?? */ public static long calcSimpleTriggerStartTime(int intervalSec, int delayTimeSec) { // ?????Quartz?Trigger????startTime? long now = HinemosTime.currentTimeMillis() + 1000l; // pauseFlag ? true???????????? long intervalMilliSecond = intervalSec * 1000; // 1) ?????????? // 22:32:05 ? 5?????22:35:00 long roundout = (now / intervalMilliSecond + 1) * intervalMilliSecond; // 2) 1)??DelayTime??????startTime???? long startTimeMillis = roundout + delayTimeSec * 1000; // 3) ??????+5???????startTime?? if ((HinemosTime.currentTimeMillis() + 5 * 1000l) < (startTimeMillis - intervalMilliSecond)) { m_log.debug("reset time before : " + new Date(startTimeMillis)); startTimeMillis = startTimeMillis - intervalMilliSecond; m_log.debug("reset time after : " + new Date(startTimeMillis)); } return startTimeMillis; } }