Quartz를 이용한 Schduler를 JMX를 이용하여 제어하기(5) - BatchJobSchduler
BatchJobScheduler에 대한 내용 입니다.
아래 소스는 박종호님께서 작성하신 겁니다~
==============================================================
1. BatchJobSchduler.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.BatchJobResultDao;
public class BatchJobScheduler {
private static final Log LOG = LogFactory.getLog(BatchJobScheduler.class);
private static String GROUP_NAME = "BT";
private static SchedulerFactory schedFact = null;
private static Scheduler sched = null;
private static JobDetail jobDetail = null;
private static CronTrigger trigger = null;
private static List batchInfoList = null;
// private static Map mdInfoMap = null;
private static boolean isStart = false;
private static Map getLoaderConfigMap(String jobName) {
Map map = null;
for (Iterator all = batchInfoList.iterator(); all.hasNext();) {
map = (Map) all.next();
if (((String)map.get("BATCH_NAME")).equals(jobName)) {
break;
}
}
return map;
}
private static List getBatchInfoAll(){
if( batchInfoList != null && batchInfoList.size() > 0)
batchInfoList.clear();
batchInfoList = new BatchJobResultDao().selectBatchInfo();
return batchInfoList;
}
/**
* JOB이 현재 실행 중이면 true를 return.
*
* @param dtName
* @return
*/
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) {
se.printStackTrace();
}
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 < batchInfoList.size();i++) {
Map returnMap = new HashMap();
String status = "S";
map = (Map)batchInfoList.get(i);
returnMap.put("GROUP_NAME", GROUP_NAME );
String batchName = map.get("BATCH_NAME").toString();
returnMap.put("BATCH_NAME", batchName );
returnMap.put("CLASS_NAME" , map.get("CLASS_NAME").toString());
returnMap.put("PACKAGE_PATH", map.get("PACKAGE_PATH").toString());
returnMap.put("CHECK_PERIOD", map.get("CHECK_PERIOD").toString());
for(int j = 0;j < jobList.length;j++) {
if(batchName.equals(jobList[j])) {
status = "R";
break;
}
}
returnMap.put("STATUS", status);
returnList.add(returnMap);
}
} else {
ComponentRegistry.init("config/component/base-component.xml");
getBatchInfoAll();
for(int i = 0;i < batchInfoList.size();i++) {
Map returnMap = new HashMap();
String status = "S";
map = (Map)batchInfoList.get(i);
returnMap.put("GROUP_NAME", GROUP_NAME );
returnMap.put("BATCH_NAME", map.get("BATCH_NAME").toString());
returnMap.put("CLASS_NAME" , map.get("CLASS_NAME").toString());
returnMap.put("CHECK_PERIOD", map.get("CHECK_PERIOD").toString());
returnMap.put("PACKAGE_PATH", map.get("PACKAGE_PATH").toString());
returnMap.put("STATUS", status);
returnList.add(returnMap);
}
}
} catch(SchedulerException se) {
LOG.error(se);
} catch(Exception e) {
LOG.error(e);
}
return returnList;
}
/**
* 중지시킬 JOB이 실행중이 아니면 false를 return. 실행 중이면 중지 후 true return.
*
* @param jobName
*/
public static boolean stopJob(String jobName ) {
boolean stoped = false;
try {
if(isJobRun(jobName)) {
sched.deleteJob(jobName, GROUP_NAME);
System.out.println("stop job " + jobName);
}
stoped = true;
} catch (UnableToInterruptJobException ue) {
LOG.error(ue);
ue.printStackTrace();
stoped = false;
} catch(Exception e) {
LOG.error(e);
e.printStackTrace();
stoped = false;
}
return stoped;
}
/**
* scheduler를 중단한다.
*
* @param stopMode - 'A'면 abort, 'N'이면 normal
* @return 중단되면 false, 중단되지 않으면 true를 return.
*/
public static boolean shutdownScheduler() {
try {
sched.shutdown(false);
return true;
} catch (SchedulerException se) {
LOG.error(se);
se.printStackTrace();
return false;
} catch(Exception e) {
LOG.error(e);
e.printStackTrace();
return false;
}
}
/**
* 스케즐러에 잡을 붙인다.
* @return
*/
public static boolean startJob(String jobName) {
boolean started = false;
try {
if(sched == null) {
startScheduler();
}
if ("A".equals(jobName)) {
LOG.info("batchInfoList is count : " + batchInfoList.size());
for (Iterator all = batchInfoList.iterator(); all.hasNext();) {
Map map = (Map) all.next();
String batchName = (String)map.get("BATCH_NAME");
String packagePath = map.get("PACKAGE_PATH").toString();
String className = map.get("CLASS_NAME").toString();
LOG.info("map config : " + map);
if(!isJobRun( batchName ) ){
Class c = Class.forName(packagePath +"."+ className);
jobDetail = new JobDetail(batchName, GROUP_NAME,
c );
jobDetail.getJobDataMap().put(batchName, map);
trigger = new CronTrigger(batchName, GROUP_NAME);
trigger.setCronExpression("0/"
+ map.get("CHECK_PERIOD").toString()
+ " * * * * ?");
sched.scheduleJob(jobDetail, trigger);
}
}
} else {
if( !isJobRun(jobName)){
// 작업 이름에 해당하는 값을 가져온다.
Map map = getLoaderConfigMap(jobName);
String packagePath = map.get("PACKAGE_PATH").toString();
String className = map.get("CLASS_NAME").toString();
Class c = Class.forName(packagePath +"."+ className);
jobDetail = new JobDetail(jobName, GROUP_NAME,
c );
jobDetail.getJobDataMap().put(jobName, map);
trigger = new CronTrigger("CHECKING_PERIOD", jobName);
trigger.setCronExpression("0/"
+ map.get("CHECK_PERIOD").toString()
+ " * * * * ?");
sched.scheduleJob(jobDetail, trigger);
}
}
started = true;
} catch (Exception e) {
LOG.error(e);
started = false;
}
return started;
}
public static boolean startScheduler() {
try {
ComponentRegistry.init("config/component/base-component.xml");
// Scheduler가 중단되어 있는 경우에만 start
if (!isStart) {
schedFact = new StdSchedulerFactory();
sched = schedFact.getScheduler();
sched.start();
getBatchInfoAll();
isStart = true;
}
return isStart;
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
isStart = false;
return isStart;
}
}
public static void main(String[] args) {
startScheduler();
startJob("A");
/*// startJob("DAILY_WORK");
List list = getRunningJob();
for(int j = 0 ; j < list.size(); j++){
Map map = (Map)list.get(j);
System.out.println();
System.out.println("map info " + map );
}
// stopJob("DAILY_WORK");
List list1 = getRunningJob();
for(int j = 0 ; j < list1.size(); j++){
Map map = (Map)list1.get(j);
System.out.println();
System.out.println("map info " + map );
}
// shutdownScheduler();
*/ }
}
BatchJobScheduler에 대한 내용 입니다.
아래 소스는 박종호님께서 작성하신 겁니다~
==============================================================
1. BatchJobSchduler.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.BatchJobResultDao;
public class BatchJobScheduler {
private static final Log LOG = LogFactory.getLog(BatchJobScheduler.class);
private static String GROUP_NAME = "BT";
private static SchedulerFactory schedFact = null;
private static Scheduler sched = null;
private static JobDetail jobDetail = null;
private static CronTrigger trigger = null;
private static List batchInfoList = null;
// private static Map mdInfoMap = null;
private static boolean isStart = false;
private static Map getLoaderConfigMap(String jobName) {
Map map = null;
for (Iterator all = batchInfoList.iterator(); all.hasNext();) {
map = (Map) all.next();
if (((String)map.get("BATCH_NAME")).equals(jobName)) {
break;
}
}
return map;
}
private static List getBatchInfoAll(){
if( batchInfoList != null && batchInfoList.size() > 0)
batchInfoList.clear();
batchInfoList = new BatchJobResultDao().selectBatchInfo();
return batchInfoList;
}
/**
* JOB이 현재 실행 중이면 true를 return.
*
* @param dtName
* @return
*/
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) {
se.printStackTrace();
}
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 < batchInfoList.size();i++) {
Map returnMap = new HashMap();
String status = "S";
map = (Map)batchInfoList.get(i);
returnMap.put("GROUP_NAME", GROUP_NAME );
String batchName = map.get("BATCH_NAME").toString();
returnMap.put("BATCH_NAME", batchName );
returnMap.put("CLASS_NAME" , map.get("CLASS_NAME").toString());
returnMap.put("PACKAGE_PATH", map.get("PACKAGE_PATH").toString());
returnMap.put("CHECK_PERIOD", map.get("CHECK_PERIOD").toString());
for(int j = 0;j < jobList.length;j++) {
if(batchName.equals(jobList[j])) {
status = "R";
break;
}
}
returnMap.put("STATUS", status);
returnList.add(returnMap);
}
} else {
ComponentRegistry.init("config/component/base-component.xml");
getBatchInfoAll();
for(int i = 0;i < batchInfoList.size();i++) {
Map returnMap = new HashMap();
String status = "S";
map = (Map)batchInfoList.get(i);
returnMap.put("GROUP_NAME", GROUP_NAME );
returnMap.put("BATCH_NAME", map.get("BATCH_NAME").toString());
returnMap.put("CLASS_NAME" , map.get("CLASS_NAME").toString());
returnMap.put("CHECK_PERIOD", map.get("CHECK_PERIOD").toString());
returnMap.put("PACKAGE_PATH", map.get("PACKAGE_PATH").toString());
returnMap.put("STATUS", status);
returnList.add(returnMap);
}
}
} catch(SchedulerException se) {
LOG.error(se);
} catch(Exception e) {
LOG.error(e);
}
return returnList;
}
/**
* 중지시킬 JOB이 실행중이 아니면 false를 return. 실행 중이면 중지 후 true return.
*
* @param jobName
*/
public static boolean stopJob(String jobName ) {
boolean stoped = false;
try {
if(isJobRun(jobName)) {
sched.deleteJob(jobName, GROUP_NAME);
System.out.println("stop job " + jobName);
}
stoped = true;
} catch (UnableToInterruptJobException ue) {
LOG.error(ue);
ue.printStackTrace();
stoped = false;
} catch(Exception e) {
LOG.error(e);
e.printStackTrace();
stoped = false;
}
return stoped;
}
/**
* scheduler를 중단한다.
*
* @param stopMode - 'A'면 abort, 'N'이면 normal
* @return 중단되면 false, 중단되지 않으면 true를 return.
*/
public static boolean shutdownScheduler() {
try {
sched.shutdown(false);
return true;
} catch (SchedulerException se) {
LOG.error(se);
se.printStackTrace();
return false;
} catch(Exception e) {
LOG.error(e);
e.printStackTrace();
return false;
}
}
/**
* 스케즐러에 잡을 붙인다.
* @return
*/
public static boolean startJob(String jobName) {
boolean started = false;
try {
if(sched == null) {
startScheduler();
}
if ("A".equals(jobName)) {
LOG.info("batchInfoList is count : " + batchInfoList.size());
for (Iterator all = batchInfoList.iterator(); all.hasNext();) {
Map map = (Map) all.next();
String batchName = (String)map.get("BATCH_NAME");
String packagePath = map.get("PACKAGE_PATH").toString();
String className = map.get("CLASS_NAME").toString();
LOG.info("map config : " + map);
if(!isJobRun( batchName ) ){
Class c = Class.forName(packagePath +"."+ className);
jobDetail = new JobDetail(batchName, GROUP_NAME,
c );
jobDetail.getJobDataMap().put(batchName, map);
trigger = new CronTrigger(batchName, GROUP_NAME);
trigger.setCronExpression("0/"
+ map.get("CHECK_PERIOD").toString()
+ " * * * * ?");
sched.scheduleJob(jobDetail, trigger);
}
}
} else {
if( !isJobRun(jobName)){
// 작업 이름에 해당하는 값을 가져온다.
Map map = getLoaderConfigMap(jobName);
String packagePath = map.get("PACKAGE_PATH").toString();
String className = map.get("CLASS_NAME").toString();
Class c = Class.forName(packagePath +"."+ className);
jobDetail = new JobDetail(jobName, GROUP_NAME,
c );
jobDetail.getJobDataMap().put(jobName, map);
trigger = new CronTrigger("CHECKING_PERIOD", jobName);
trigger.setCronExpression("0/"
+ map.get("CHECK_PERIOD").toString()
+ " * * * * ?");
sched.scheduleJob(jobDetail, trigger);
}
}
started = true;
} catch (Exception e) {
LOG.error(e);
started = false;
}
return started;
}
public static boolean startScheduler() {
try {
ComponentRegistry.init("config/component/base-component.xml");
// Scheduler가 중단되어 있는 경우에만 start
if (!isStart) {
schedFact = new StdSchedulerFactory();
sched = schedFact.getScheduler();
sched.start();
getBatchInfoAll();
isStart = true;
}
return isStart;
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
isStart = false;
return isStart;
}
}
public static void main(String[] args) {
startScheduler();
startJob("A");
/*// startJob("DAILY_WORK");
List list = getRunningJob();
for(int j = 0 ; j < list.size(); j++){
Map map = (Map)list.get(j);
System.out.println();
System.out.println("map info " + map );
}
// stopJob("DAILY_WORK");
List list1 = getRunningJob();
for(int j = 0 ; j < list1.size(); j++){
Map map = (Map)list1.get(j);
System.out.println();
System.out.println("map info " + map );
}
// shutdownScheduler();
*/ }
}
댓글 없음:
댓글 쓰기