版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
源代码:
package com.twotiger.core.job;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.twotiger.system.Global;
import org.apache.log4j.MDC;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.jfinal.log.Logger;
import com.twotiger.cms.notify.service.INotifyService;
import com.twotiger.cms.notify.service.NotifyService;
import com.twotiger.constant.Notification.NotificationLevel;
import com.twotiger.constant.Notification.NotificationType;
import com.twotiger.core.job.model.JobConfig;
import com.twotiger.system.exception.BusinessException;
import com.twotiger.system.exception.DaoException;
import com.twotiger.system.exception.StackTraceMessage;
import com.twotiger.system.lock.ILockHandler;
import com.twotiger.system.lock.ILockIdentify;
import com.twotiger.system.lock.StringLockIdentify;
import com.twotiger.system.lock.memory.MemoryLockHandler;
import com.twotiger.system.proxy.TxProxyCache;
import com.twotiger.util.ConfigUtil;
import com.twotiger.util.DateUtil;
/**
*
* @Description: 基础定时任务
* @Author hansc
* @Date 2014年11月24日 下午3:14:32
*/
public abstract class BaseJob implements Job {
public final Logger log = Logger.getLogger(getClass());
//执行中服务名称、以名称进行辨别执行(不执行正在执行的服务)
private static final Map<String, Boolean> locks = new HashMap<String, Boolean>();
//执行中的服务分组、以分组进行加锁串行处理(阻塞执行同组的服务)
private static final ILockHandler hander = new MemoryLockHandler();
/**
* 定时器执行入口
* @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
*/
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
if (!Boolean.parseBoolean(ConfigUtil.getInstance().getString("job")))
return;
String name = context.getJobDetail().getKey().getName();
execute(name);
}
private void execute(String name) {
MDC.clear();
MDC.put(Global.THREAD_KEY, UUID.randomUUID().toString().replaceAll("-", ""));
JobConfig config = null;
try {
config = JobConfig.DAO.findByNid(name);
} catch (DaoException e) {
log.error("", e);
return;
}
String group = config.getStr("group");
if (!locks.containsKey(name)) {
locks.put(name, false);
}
if (locks.get(name)) {
log.info(name + "<执行中>");
return;
}
locks.put(name, true);
ILockIdentify identify = new StringLockIdentify(group);
hander.lock(identify);
log.info(name + "<开始执行>");
config.set("startTime", DateUtil.getSystemTimeForLong());
config.set("status", 1);
config.update();
long now = System.currentTimeMillis();
try {
run();
} catch (Exception e) {
String msg = name + "<执行失败>";
log.error(msg, e);
INotifyService notServ = (INotifyService) TxProxyCache.getProxy(NotifyService.service);
try {
notServ.addNotify(NotificationType.SYSTEM, msg, StackTraceMessage.message(e),
NotificationLevel.HIGH);
} catch (BusinessException e1) {
}
} finally {
config.set("endTime", DateUtil.getSystemTimeForLong());
config.set("status", 0);
config.update();
hander.unLock(identify);
locks.put(name, false);
log.info(name + "<结束执行:" + (System.currentTimeMillis() - now) + ">");
MDC.clear();
}
}
/**
* 实现类需要重写此方法
* @throws BusinessException
*/
protected abstract void run() throws Exception;
}