Quartz를 이용한 Schduler를 JMX를 이용하여 제어하기(4) - DtSchduler
DtJobScheduler에 대한 내용 입니다.
아래 소스는 손정호님께서 작성하신 겁니다~
============================================================== 1.
DtJobSchduler.java
============================================================== /**
* */ package oraclejava.processmgr;
import
java.util.ArrayList; import java.util.Date; import java.util.HashMap;
import java.util.Iterator; import java.util.List; import
java.util.Map;
import org.quartz.JobDetail; import
org.quartz.Scheduler; import org.quartz.SchedulerException; import
org.quartz.SchedulerFactory; import org.quartz.Trigger; import
org.quartz.TriggerUtils; import org.quartz.UnableToInterruptJobException;
import org.quartz.impl.StdSchedulerFactory;
import
com.skcc.base.ComponentRegistry; import oraclejava.dao.DtDao; import
oraclejava.errormgr.ApplicationErrorMgr; import
oraclejava.jmx.util.MBeanUtil; import oraclejava.util.Str;
/**
* @author 손정호 * */ public class DtJobScheduler {
private static final ApplicationErrorMgr LOG =
ApplicationErrorMgr.getInstance(DtJobScheduler.class);
private
static SchedulerFactory schedFact = null; private static Scheduler
sched = null; private static JobDetail jobDetail = null;
private static Trigger trigger = null; private static
Map dtMap = null; private static Map bssMap = null;
private static List dtList = null; private static List bssList =
null; private static String interval = "";
private static final String GROUP_NAME = "DT"; private
static boolean isStart = false; /** *
* */ public DtJobScheduler() {
ComponentRegistry.init("config/component/base-component.xml");
getAllData();
} /** * DT에 대한 모든 정보를 읽어 온다.
* */ private static void getAllData() {
if(dtList != null && dtList.size() > 0)
dtList.clear(); if(bssList !=
null && bssList.size() > 0)
bssList.clear(); DtDao dtDao =
DtDao.getInstance(); dtList = dtDao.getAllDtDef();
bssList = dtDao.getAllBssInfo();
LOG.info("select needed data for All DT"); }
/** * dtName에 맞는 정보를 읽어 온다. * *
@param dtName */ private static void getData(String
dtName) { DtDao dtDao =
DtDao.getInstance(); dtMap = dtDao.getDtDef(dtName);
bssMap = dtDao.getBssInfo((String)dtMap.get("BSS_NAME"));
LOG.info("select needed data for DT [ " + dtName +
" ]"); } /** * 원하는 JOB에 새로운
data를 넣어 준다. * * @param dtName */
public static void refreshJobData(String dtName){
try {
if(dtName.equals("A")) { getAllData();
String[]
jobList = sched.getJobNames(GROUP_NAME);
for(int i = 0;i < jobList.length;i++) {
dtMap = getDtMap(jobList[i]);
bssMap =
getBssMap((String)dtMap.get("BSS_NAME"));
sched.getJobDetail(jobList[i],
GROUP_NAME).getJobDataMap().put("DT_INFO", dtMap);
sched.getJobDetail(jobList[i],
GROUP_NAME).getJobDataMap().put("BSS_INFO", bssMap);
interval =
(String)dtMap.get("DIR_FILE_CHECK_PERIOD");
trigger =
TriggerUtils.makeSecondlyTrigger(Integer.parseInt(interval));
trigger.setStartTime(new Date());
trigger.setName((String)dtMap.get("DT_NAME"));
//
trigger = new CronTrigger(jobList[i], GROUP_NAME); //
trigger.setCronExpression("0/"+inteval+" * * * * ?"); //
trigger.setCronExpression((String)dtMap.get("DIR_FILE_CHECK_PERIOD"));
sched.rescheduleJob(jobList[i], GROUP_NAME, trigger);
}
LOG.info("All DT data refresh..."); } else {
getData(dtName);
sched.getJobDetail(dtName,
GROUP_NAME).getJobDataMap().put("DT_INFO", dtMap);
sched.getJobDetail(dtName, GROUP_NAME).getJobDataMap().put("BSS_INFO",
bssMap);
interval = (String)dtMap.get("DIR_FILE_CHECK_PERIOD");
trigger =
TriggerUtils.makeSecondlyTrigger(Integer.parseInt(interval));
trigger.setStartTime(new Date());
trigger.setName(dtName); //
trigger = new CronTrigger(dtName, GROUP_NAME); //
trigger.setCronExpression("0/"+inteval+" * * * *
?"); //
trigger.setCronExpression((String)dtMap.get("DIR_FILE_CHECK_PERIOD"));
sched.rescheduleJob(dtName, GROUP_NAME, trigger); }
LOG.info("DT [ " +
dtName + " ] data refresh..."); } catch(SchedulerException
se) { LOG.error(se);
se.printStackTrace(); } catch(Exception e) {
LOG.error(e); e.printStackTrace();
} } /** * 중지시킬
JOB이 실행중이 아니면 false를 return. 실행 중이면 중지 후 true return. *
* @param dtName * @param stopMode - 'A'면 abort, 'N'이면 normal
*/ public static boolean stopJob(String dtName, String stopMode)
{ boolean stoped = false;
try {
if(isJobRun(dtName)) {
if(stopMode.equals("A"))
sched.interrupt(dtName, GROUP_NAME);
sched.deleteJob(dtName, GROUP_NAME);
}
stoped = true;
LOG.info("DT [ " + dtName + " ] stoped...");
} catch (UnableToInterruptJobException ue) {
LOG.error(ue); ue.printStackTrace();
stoped = false; }
catch(Exception e) { LOG.error(e);
e.printStackTrace(); stoped = false;
} return stoped;
} /** * 실행시킬 JOB이 이미 실행 중이면 false를 return.
실행중이 아니면 실행 후 true return. * * @param dtName
*/ public static boolean startJob(String dtName) {
boolean started = false;
if(sched == null) startScheduler();
try { if(dtName.equals("A"))
{
for(int i = 0;i < dtList.size();i++) {
dtMap = (Map)dtList.get(i);
if(!isJobRun((String)dtMap.get("DT_NAME"))) {
bssMap = getBssMap((String)dtMap.get("BSS_NAME"));
jobDetail = new JobDetail((String)dtMap.get("DT_NAME"), GROUP_NAME,
DtJob.class);
jobDetail.getJobDataMap().put("DT_INFO", dtMap);
jobDetail.getJobDataMap().put("BSS_INFO", bssMap);
interval =
(String)dtMap.get("DIR_FILE_CHECK_PERIOD");
trigger =
TriggerUtils.makeSecondlyTrigger(Integer.parseInt(interval));
trigger.setStartTime(new Date());
trigger.setName((String)dtMap.get("DT_NAME"));
// trigger
= new CronTrigger((String)dtMap.get("DT_NAME"), GROUP_NAME); //
trigger.setCronExpression("0/"+inteval+" * * *
* ?"); //
trigger.setCronExpression((String)dtMap.get("DIR_FILE_CHECK_PERIOD"));
sched.scheduleJob(jobDetail, trigger);
}
started = true;
LOG.info("All DT started...");
} } else {
if(!isJobRun(dtName)) {
dtMap = getDtMap(dtName);
bssMap = getBssMap((String)dtMap.get("BSS_NAME"));
jobDetail = new JobDetail(dtName, GROUP_NAME, DtJob.class);
jobDetail.getJobDataMap().put("DT_INFO", dtMap);
jobDetail.getJobDataMap().put("BSS_INFO", bssMap);
interval = (String)dtMap.get("DIR_FILE_CHECK_PERIOD");
trigger = TriggerUtils.makeSecondlyTrigger(Integer.parseInt(interval));
trigger.setStartTime(new Date());
trigger.setName((String)dtMap.get("DT_NAME"));
//
trigger = new CronTrigger(dtName, GROUP_NAME); //
trigger.setCronExpression("0/"+inteval+" * * * * ?");
//
trigger.setCronExpression((String)dtMap.get("DIR_FILE_CHECK_PERIOD"));
sched.scheduleJob(jobDetail, trigger); }
started
= true;
LOG.info("DT [ " + dtName + " ] started..."); }
} catch(SchedulerException se) {
LOG.error(se); se.printStackTrace();
started = false; } catch(Exception e)
{ LOG.error(e);
e.printStackTrace(); started = false;
} return started; }
/** * scheduler를 중단한다. *
* @param stopMode - 'A'면 abort, 'N'이면 normal * @return 중단되면
false, 중단되지 않으면 true를 return. */ public static boolean
shutdownScheduler(String stopMode) { try
{ if (sched != null) {
if(stopMode.equals("A"))
sched.shutdown(false); else
sched.shutdown(true);
isStart = false;
sched = null; }
LOG.info("DT Scheduler shutdown...");
} catch (SchedulerException se) {
LOG.error(se); se.printStackTrace();
isStart = true; } catch(Exception e) {
LOG.error(e); e.printStackTrace();
isStart = true; }
return isStart; }
/** * scheduler를 실행한다. * *
@return 실행이 되면 true, 실행이 되지 않으면 false를 return. */ public
static boolean startScheduler() {
try {
if (sched ==
null) { new DtJobScheduler();
schedFact = new StdSchedulerFactory();
sched =
schedFact.getScheduler();
sched.start();
isStart = true;
} LOG.info("DT Scheduler
started..."); } catch(SchedulerException se) {
LOG.error(se); se.printStackTrace();
isStart = false;
} catch(Exception e) { LOG.error(e);
e.printStackTrace(); isStart =
false; }
return isStart; }
/** * * @param dtName * @return
*/ private static Map getDtMap(String dtName) {
Map returnMap = null;
for(int i = 0;i < dtList.size();i++) {
returnMap = (Map)dtList.get(i);
if(((String)returnMap.get("DT_NAME")).equals(dtName))
break; }
return returnMap; }
/** * * @param bssName *
@return */ private static Map getBssMap(String bssName)
{ Map returnMap = null;
for(int i = 0;i < bssList.size();i++) {
returnMap = (Map)bssList.get(i);
if(((String)returnMap.get("BSS_NAME")).equals(bssName))
break; }
return returnMap; }
/** * JOB이 현재 실행 중이면 true를 return. *
* @param dtName * @return */
private static boolean isJobRun(String dtName) {
boolean isRun = false; try {
String[] jobList =
sched.getJobNames(GROUP_NAME);
for(int i = 0;i < jobList.length;i++) {
if(dtName.equals(jobList[i])) {
isRun = true;
break; }
}
} catch(SchedulerException se) {
se.printStackTrace(); } return isRun;
} /** * 현재 scheduler에서 실행 중인
job의 목록을 return. * * @return */
public static List getRunningJob() { Map
map = null; String status = null;
String dtName = null; List
returnList = new ArrayList(); try{
if(sched != null) {
&n
|
댓글 없음:
댓글 쓰기