2015년 10월 12일 월요일

Quartz를 이용한 Schduler를 JMX를 이용하여 제어하기(3) - LoaderScheduler

Quartz를 이용한 Schduler를 JMX를 이용하여 제어하기(3)
이번 강좌에서는 MBean에서 호출하는 Scheduler를 민드는데 … 여기서 작성할 스케쥴러는 디렉토리의 파일을 주기적으로 체크하는 역할을 하는데 이러한 주기적으로 반복되는 일을 처리하기 위해 Quartz라는 Framework을 사용하였습니다. 결국 이번 강좌에서의 예제는 이전 강좌에서 기술한 JMX MBean이 Quartz라는 Framework을 이용한 스케쥴러를 제어하는데 이 Scheduler를 만드는 것입니다.Scheduler는 크게 3가지가 인데… LoaderJobSchduler, BatchJobSchduler, BatchScheduler, 여기서는 LoaderJobSchduler에 대해 살펴보도록 하겠습니다.아래 소스는 박종호님께서 작성하신 예제 입니다.

Quartz에 대해서는 이전 강좌를 참고 하세요
 

==============================================================
1. LoaderJobSchduler.java
==============================================================
package oraclejava.processmgr;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.UnableToInterruptJobException;
import org.quartz.impl.StdSchedulerFactory;
import com.skcc.base.ComponentRegistry;
import oraclejava.dao.LoaderDao;
import oraclejava.util.Str;

public class LoaderJobScheduler {
private static final Log LOG = LogFactory.getLog(LoaderJobScheduler.class);
private static String GROUP_NAME = "LO";
private static SchedulerFactory schedFact = null;
private static Scheduler sched = null;
private static JobDetail jobDetail = null;
private static CronTrigger trigger = null;
private static List loadingConfigList = null;
private static Map mdInfoMap = null;
private static boolean isStart = false;
private static String makeJobName(String type , String no){
return type + "_" + "MD" + no;
}
private static String getType(Map map){return map.get("Engine_TYPE").toString(); }
private static String getEngineNo(Map map){return map.get("Engine_NO").toString(); }
private static Map getLoaderConfigMap(String jobName) {
Map map = null;
for (Iterator all = loadingConfigList.iterator(); all.hasNext();) {
map = (Map) all.next();
String totalName = map.get("Engine_TYPE").toString()
+"_MD"+ map.get("Engine_NO").toString();
if (totalName.equals(jobName)) break;\
}
return map;
}
private static void LoaderSchedulerConfigAll() {
if (loadingConfigList != null && loadingConfigList.size() > 0)
loadingConfigList.clear();
if (mdInfoMap != null && mdInfoMap.size() > 0)
mdInfoMap.clear();
LoaderDao dao = LoaderDao.getInstance();
loadingConfigList = dao.getLoderConfig();
mdInfoMap = dao.getAllLoderPassCollection();
}

public static boolean stopJob(String jobName, String stopMode) {
try { if (Str.nvl(stopMode).equals("A")) sched.interrupt(jobName, GROUP_NAME);
sched.deleteJob(jobName, GROUP_NAME);
return true;
} catch (UnableToInterruptJobException ue) {
LOG.error(ue);
return false;
} catch (Exception e) {
LOG.error(e);
return false;
}
}

private static boolean isJobRun(String jobName) {
boolean isRun = false;
try {
String[] jobList = sched.getJobNames(GROUP_NAME);
for(int i = 0;i < jobList.length;i++) {
if(jobName.equals(jobList[i])) {
isRun = true; break;
}
}
} catch(SchedulerException se) {
LOG.error(se);
} catch(Exception e){
LOG.error(e);
}
return isRun;
}


public static List getRunningJob() {
Map map = null;
List returnList = new ArrayList();
try{ if(sched != null) {
String[] jobList = sched.getJobNames(GROUP_NAME);
for(int i = 0;i < loadingConfigList.size();i++) {
Map returnMap = new HashMap(); String status = "S";
map = (Map)loadingConfigList.get(i);
String Type = getType(map);
String EngineNo = getEngineNo(map);
String jobName = makeJobName(Type , EngineNo);

returnMap.put("Engine_TYPE", Type );
returnMap.put("Engine_NO", EngineNo );
returnMap.put("GROUP_NAME", GROUP_NAME );
returnMap.put("JOB_NAME", jobName );

for(int j = 0;j < jobList.length;j++) {
if(jobName.equals(jobList[j])) {
status = "R";
break;
}
}
returnMap.put("STATUS", status);
returnList.add(returnMap);
}
} else {
ComponentRegistry.init("config/component/base-component.xml");
LoaderSchedulerConfigAll();
for(int i = 0;i < loadingConfigList.size();i++) {
Map returnMap = new HashMap();
String status = "S";
map = (Map)loadingConfigList.get(i);
String Type = getType(map);
String EngineNo = getEngineNo(map);
String jobName = makeJobName(Type , EngineNo);
returnMap.put("Engine_TYPE", Type );
returnMap.put("Engine_NO", EngineNo );
returnMap.put("GROUP_NAME", GROUP_NAME );
returnMap.put("JOB_NAME", jobName);
returnMap.put("STATUS", status);
returnList.add(returnMap);
}
}
} catch(SchedulerException se) { } catch(Exception e){}
return returnList;
}

public static void refreshJob(String jobName) {
try {
if (jobName.equals("A")) {
LoaderSchedulerConfigAll();
String[] jobList = sched.getJobNames(GROUP_NAME);
for (int i = 0; i < jobList.length; i++) {
Map map = getLoaderConfigMap(jobList[i]);
sched.getJobDetail(jobList[i], GROUP_NAME).getJobDataMap()
.put(jobList[i], map);
sched.getJobDetail(jobList[i], GROUP_NAME).getJobDataMap()
.put("Engine_TYPE", getType(map));
sched.getJobDetail(jobList[i], GROUP_NAME).getJobDataMap()
.put("Engine_NO", getEngineNo(map));
sched.getJobDetail(jobList[i], GROUP_NAME).getJobDataMap()
.put("MD_INFO_MAP", mdInfoMap);
trigger = new CronTrigger(jobList[i], GROUP_NAME);
trigger.setCron__EXPRESSION__("0/"
+ map.get("CHECKING_PERIOD").toString()
+ " * * * * ?");
sched.rescheduleJob(jobList[i], GROUP_NAME, trigger);
}
} else {
Map map = getLoaderConfigMap(jobName);
sched.getJobDetail(jobName, GROUP_NAME).getJobDataMap()
.put(jobName, map);
sched.getJobDetail(jobName, GROUP_NAME).getJobDataMap()
.put("Engine_TYPE", getType(map));
sched.getJobDetail(jobName, GROUP_NAME).getJobDataMap()
.put("Engine_NO", getEngineNo(map));
sched.getJobDetail(jobName, GROUP_NAME).getJobDataMap()
.put("MD_INFO_MAP", mdInfoMap);

trigger = new CronTrigger(jobName, GROUP_NAME);
trigger.setCron__EXPRESSION__("0/"
+ map.get("CHECKING_PERIOD").toString()
+ " * * * * ?");
sched.rescheduleJob(jobName, GROUP_NAME, trigger);
}
} catch (SchedulerException se) {
LOG.error(se);
} catch (Exception e) {
LOG.error(e);
}
}

public static boolean startJob(String jobName) {
boolean started = false;
try {
if(sched == null){
startScheduler();
}
if ("A".equals(jobName)) {
for (Iterator all = loadingConfigList.iterator(); all.hasNext();) {
Map map = (Map) all.next();
String Type = getType(map);
String EngineNo = getEngineNo(map);
String totalName = makeJobName(Type, EngineNo);
if(!isJobRun(totalName) ){
jobDetail = new JobDetail(totalName, GROUP_NAME, oraclejava.load.LoaderJob.class);
jobDetail.getJobDataMap().put(totalName, map); jobDetail.getJobDataMap().put("Engine_TYPE", Type); jobDetail.getJobDataMap().put("Engine_NO", EngineNo); jobDetail.getJobDataMap().put("MD_INFO_MAP", mdInfoMap);
trigger = new CronTrigger(totalName, GROUP_NAME);
trigger.setCron__EXPRESSION__("0/"
+ map.get("CHECKING_PERIOD").toString()
+ " * * * * ?");

sched.scheduleJob(jobDetail, trigger);
}
started = true;
}
} else {

if( !isJobRun(jobName)){
// 작업 이름에 해당하는 값을 가져온다.
Map map = getLoaderConfigMap(jobName);
jobDetail = new JobDetail(jobName, GROUP_NAME, oraclejava.load.LoaderJob.class);
jobDetail.getJobDataMap().put(jobName, map);
jobDetail.getJobDataMap()
.put("Engine_TYPE", getType(map));
jobDetail.getJobDataMap().put("Engine_NO", getEngineNo(map));
jobDetail.getJobDataMap().put("MD_INFO_MAP", mdInfoMap);

trigger = new CronTrigger("CHECKING_PERIOD", jobName);
trigger
.setCron__EXPRESSION__("0/"
+ map.get("CHECKING_PERIOD").toString()
+ " * * * * ?");

sched.scheduleJob(jobDetail, trigger);
}
started = true;
}
} catch (Exception e) {
LOG.error(e);
started = false;
}
return started;
}

public static boolean shutdownScheduler(String stopMode) {
try {
if (isStart) {
if (Str.nvl(stopMode).equals("A"))
sched.shutdown(false);
else sched.shutdown(true);
isStart = false; }
return true;
} catch (SchedulerException se) {
LOG.error(se);
return false;
} catch (Exception e) {
LOG.error(e);
return false;
}
}

public static boolean startScheduler() {
try {
ComponentRegistry.init("config/component/base-component.xml");
// Scheduler가 중단되어 있는 경우에만 start
if (!isStart) {
schedFact = new StdSchedulerFactory();
sched = schedFact.getScheduler();
sched.start();

LoaderSchedulerConfigAll();
isStart = true;
}
return isStart;
} catch (Exception e) {
LOG.error(e);
isStart = false;
return isStart;
}
}
}

==========================================
LoaderJob.java
==========================================

package oraclejava.processmgr;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.InterruptableJob;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.UnableToInterruptJobException;

import oraclejava.load.DirFileCheckSch;



public class LoaderJob implements InterruptableJob
{

private Map loaderConfigMap;
private Map mdInfoMap;

private static final Log LOG = LogFactory.getLog(LoaderJob.class);

public void interrupt() throws UnableToInterruptJobException {
LOG.info("Job Loader Interrupted...");

}
public String getJobName(String EngineType , String EngineNo){
return EngineType + "_MD" + EngineNo;
}
public void execute(JobExecutionContext cntxt) throws JobExecutionException {

try {
String EngineType = (String) cntxt.getJobDetail().getJobDataMap()
.get("Engine_TYPE");
String EngineNo = (String) cntxt.getJobDetail().getJobDataMap()
.get("Engine_NO");
String jobName = getJobName(EngineType , EngineNo);
loaderConfigMap = (Map) cntxt.getJobDetail().getJobDataMap().get(
jobName );
mdInfoMap = (Map) cntxt.getJobDetail().getJobDataMap().get("MD_INFO_MAP");
LOG.info("Loader Job Name is " + jobName );

DirFileCheckSch d=new DirFileCheckSch(loaderConfigMap , mdInfoMap);
d.fileCheck();
} catch (Exception e) {
LOG.error(e);
throw new JobExecutionException();
}
}
}

댓글 없음:

댓글 쓰기