老虎的定时任务核心类——加锁支持集群部署

源代码:


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;

}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值