1、Interceptor:
packagecom.paic.is.orderSystem.common.quartz.interceptor;
importcom.paic.pafa.app.biz.service.BusinessServiceException;
importcom.paic.pafa.exceptions.BusinessException;
/**
* 公共拦截接口
* @author EX-MAOWEIWEN001
*
*/
public interfaceInterceptor {
/**
* 设置监听名称
* @param Name
*/
publicvoid setTargerName(String name);
/**
* 执行具体业务方法前执行
* @param object
* @return
*/
publicObject beforAction(Object object)throws BusinessServiceException;
/**
* 执行具体业务方法后执行
* @param object
* @return
*/
publicObject afterAction(Object object)throws BusinessServiceException;
/**
* 执行具体业务流程
* @param object
* @return
*/
publicObject exec()throws BusinessServiceException;
/**
* 执行具体业务流程
* @param object
* @return
*/
publicObject exec(Object object)throws BusinessServiceException;
/**
* 执行业务方法
* @param object
* @return
* @throws BusinessException
*/
publicObject action(Object object)throws BusinessServiceException;
/**
* 业务执行方法,出错时直接外抛,数据库不记录错误日志
* @return
* @throws BusinessServiceException
*/
public Object execNoneLog(Objectobject)throws BusinessServiceException;
/**
* 容器关闭前须调用方法
* @throws BusinessServiceException
*/
public void destroy()throwsBusinessServiceException;
/**
* 初始化动作,设置数据库表开关为0
* @throws BusinessServiceException
*/
public void init()throwsBusinessServiceException;
}
2、AbstractInterceptor.java:
packagecom.paic.is.orderSystem.common.quartz.interceptor;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Map;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importcom.paic.is.orderSystem.common.quartz.dto.ErrorLogDTO;
importcom.paic.is.orderSystem.common.quartz.integration.dao.CommonJobDAO;
importcom.paic.is.orderSystem.common.quartz.integration.dao.JobLogDAO;
importcom.paic.is.orderSystem.common.system.util.ProcessDataHelper;
importcom.paic.pafa.app.biz.service.BusinessServiceException;
importcom.paic.pafa.app.integration.dao.PafaDAOException;
importcom.paic.pafa.exceptions.BusinessException;
/**
* 拦截器实现
*
* @author EX-MAOWEIWEN001
*
*/
public abstractclass AbstractInterceptor implements Interceptor {
publicLog log = LogFactory.getLog(this.getClass().getName());
protectedJobLogDAO jobLogDAO;
protectedCommonJobDAO commonJobDAO;
protectedString targerName;
privateString des;
privatestatic boolean switcher = false;// 静态开关
/**
* 任务执行完成后,置状态为可用
*/
publicObject afterAction(Object object) throws BusinessServiceException {
updateQuartzStateCheck(false);
returnobject;
}
/**
* 任务执行前,获取任务执行权限
*/
publicObject beforAction(Object object) throws BusinessServiceException {
if(updateQuartzStateCheck(true) == 0) {
log.info("任务名称:"+ targerName + ",该任务在另外的APP被执行,本次不执行该任务");
thrownew BusinessServiceException("任务名称:" + targerName
+",该任务在另外的APP被执行,本次不执行该任务");
}
returnobject;
}
/**
* 带参数方法,供外部调用时使用
*/
publicObject exec(Object object) throws BusinessServiceException {
Objectrs = null;
Objectafter = null;
Exceptionie = null;
Objectbef = this.beforAction(object);
try{
after= this.action(bef);
}catch (BusinessServiceException e) {
e.printStackTrace();
ie= e;
log.error("监控线程出错",e);
throwe;
}finally {
if(ie != null) {
//try{
StringerrorMsg = ProcessDataHelper.getDate("yyyy-MM-dd",
newDate())
+"日" + (targerName) + "执行过程出现异常,请检查error_log";
log.info(errorMsg);
//insertErrorLog(ie,"20000", targerName);
//}catch (BusinessServiceException e1) {
// e1.printStackTrace();
// log.error("添加错误日志时出错",e1);
// thrownew BusinessServiceException("添加错误日志时出错", e1);
//}finally {
rs= this.afterAction(after);
ie= null;
if(rs != null) {
returnrs;
}
//}
}
rs= this.afterAction(after);
if(rs != null) {
returnrs;
}
}
returnrs;
}
/**
* 自动执行方法,供定时器调用
*
* @throws BusinessException
*/
publicObject exec() throws BusinessServiceException {
Exceptionie = null;
this.beforAction(null);
try{
this.action(null);
}catch (BusinessServiceException e) {
e.printStackTrace();
ie= e;
log.error("监控线程出错",e);
throwe;
}finally {
if(ie != null) {
StringerrorMsg = ProcessDataHelper.getDate("yyyy-MM-dd",
newDate())
+"日" + (targerName) + "执行过程出现异常,请检查error_log";
//添加错误日志
log.info(errorMsg);
//insertErrorLog(ie,"20000", targerName);
this.afterAction(null);
}else{
this.afterAction(null);
}
}
returnnull;
}
/**
* 业务执行方法,出错时直接外抛,数据库不记录错误日志
*
* @return
* @throws BusinessServiceException
*/
publicObject execNoneLog(Object object) throws BusinessServiceException {
Exceptionie = null;
Objectrs = null;
Objectafter = null;
Objectbef = null;
if(switcher == false) {
bef= this.beforAction(object);
switcher= true;// 抢到锁后,一直占有
}
if(bef == null)
bef= object;
try{
after= this.action(bef);
}catch (BusinessServiceException e) {
e.printStackTrace();
log.error("",e);
ie= e;
throwe;
}finally {
if(ie != null) {
rs= this.afterAction(after);
if(rs != null) {
returnrs;
}
switcher= false;
}
}
returnafter;
}
/**
* 更新quartz控制表state_id
*进入时将QUANRTZ并发控制表的当前任务的state_id置为1,此时其他任务就无法进入,等此任务完成时,将state_id置为0
* 由于有两台app服务,故需要用此方式防止任务重复执行
*
* @param isBefore
* @throws BusinessException
*/
privateint updateQuartzStateCheck(boolean isBefore) {
intupdateInt = 0;
Mapmap = new HashMap();
StringquartzJobName = targerName;
StringbeforeState = "0";
StringafterState = "1";
if(isBefore) {
map.put("beginState",beforeState);
map.put("updateState",afterState);
}else {
map.put("beginState",afterState);
map.put("updateState",beforeState);
}
map.put("quartzName",quartzJobName);
try{
//更新quartz控制表的state_id,通过更新条数判断逻辑是否进行
updateInt= commonJobDAO.updateQuartzState(map);
}catch (PafaDAOException e) {
e.printStackTrace();
}
returnupdateInt;
}
/**
* 重新设置quartz控制表state_id为0
*进入时将QUANRTZ并发控制表的当前任务的state_id置为1,此时其他任务就无法进入,等此任务完成时,将state_id置为0
* 由于有两台app服务,故需要用此方式防止任务重复执行
*/
privatevoid reSetQuartzState() throws BusinessServiceException {
Mapmap = new HashMap();
StringquartzJobName = targerName;
StringbeforeState = "1";
StringafterState = "0";
map.put("beginState",beforeState);
map.put("updateState",afterState);
map.put("quartzName",quartzJobName);
//重新设置quartz控制表的state_id
try{
commonJobDAO.updateQuartzState(map);
}catch (PafaDAOException e) {
e.printStackTrace();
thrownew BusinessServiceException(e);
}
}
// publicvoid insertErrorLog(Exception e, String errorCode, String quartzName)
// throwsBusinessServiceException {
// try{
// StringerrorMessage = e.getMessage();
// if(errorMessage.length() > 700) {
// errorMessage= errorMessage.substring(0, 700);
// }
//
// ErrorLogDTOerrorLogDTO = new ErrorLogDTO();
// errorLogDTO.setCalId(quartzName);
// errorLogDTO.setProcedureName(quartzName);
// errorLogDTO.setErrorCode(Double.valueOf(errorCode));
// errorLogDTO.setErrorInfo(errorMessage);
// errorLogDTO.setFcu("system");
// errorLogDTO.setDataState("0");
// errorLogDTO.setDbUser("system");
//
// jobLogDAO.insertErrorLog(errorLogDTO);
// }catch (PafaDAOException e1) {
// e1.printStackTrace();
// thrownew BusinessServiceException("添加错误日志时出错", e1);
// }
//
// }
/**
* 容器关闭前须调用方法
*
* @throws BusinessServiceException
*/
publicvoid destroy() throws BusinessServiceException {
switcher= false;
this.afterAction(null);
}
/**
* 初始化调用该方法
*/
publicvoid init() throws BusinessServiceException {
switcher= false;
reSetQuartzState();
}
publicvoid setDes(String des) {
this.des= des;
}
publicvoid setJobLogDAO(JobLogDAO jobLogDAO) {
this.jobLogDAO= jobLogDAO;
}
publicvoid setTargerName(String targerName) {
this.targerName= targerName;
}
publicvoid setCommonJobDAO(CommonJobDAO commonJobDAO) {
this.commonJobDAO= commonJobDAO;
}
}
3、实现类
packagecom.paic.is.orderSystem.common.quartz.job;
importjava.text.DateFormat;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importcom.paic.is.orderSystem.common.quartz.dto.QuartzJobRunLogDTO;
importcom.paic.is.orderSystem.common.quartz.interceptor.AbstractInterceptor;
importcom.paic.is.orderSystem.taInterface.biz.service.DealSweepCompareService;
importcom.paic.pafa.app.biz.service.BusinessServiceException;
importcom.paic.pafa.app.integration.dao.PafaDAOException;
/**
* 划款对账
* @author YANGMEIJING708
*
*/
public classDealSweepCompareJob extends AbstractInterceptor {
publicLog log = LogFactory.getLog(this.getClass().getName());
privateDealSweepCompareService dealSweepCompareService;
publicDealSweepCompareService getDealSweepCompareService() {
returndealSweepCompareService;
}
publicvoid setDealSweepCompareService(
DealSweepCompareServicedealSweepCompareService) {
this.dealSweepCompareService= dealSweepCompareService;
}
@Override
publicObject action(Object object) throws BusinessServiceException {
Datenow = new Date();
DateFormatdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//记录JOB 运行状态
QuartzJobRunLogDTOquartzJobRunLogDTO = new QuartzJobRunLogDTO();
quartzJobRunLogDTO.setCalId(targerName);
quartzJobRunLogDTO.setJobName(targerName);
quartzJobRunLogDTO.setJobRunInfo(targerName+ " 启动成功!");
try{
jobLogDAO.insertJobRunLog(quartzJobRunLogDTO);
}catch (PafaDAOException e) {
log.error("======="+ df.format(now) + "记录JOB 运行状态时出错========");
}
//处理划款对账
dealSweepCompareService.dealSweepCompare();
now= new Date();
//记录JOB 运行状态
quartzJobRunLogDTO.setJobRunInfo("DealSweepCompareJob关闭成功!");
try{
jobLogDAO.insertJobRunLog(quartzJobRunLogDTO);
}catch (PafaDAOException e) {
log.error("======="+ df.format(now) + "记录JOB 运行状态时出错========");
}
returnnull;
}
}
4、
<bean id="dealSweepCompareJob"class="com.paic.is.orderSystem.common.quartz.job.DealSweepCompareJob"init-method="init" destroy-method="destroy">
<propertyname="targerName">
<value>dealSweepCompareJob</value>
</property>
<propertyname="des">
<value>dealSweepCompareJob运行异常</value>
</property>
<propertyname="dealSweepCompareService">
<refbean="dealSweepCompareService"/>
</property>
<propertyname="commonJobDAO">
<refbean="commonJobDAO"/>
</property>
<propertyname="jobLogDAO">
<refbean="jobLogDAO"/>
</property>
</bean>