定时任务实战

记录一次简单的定时任务:

注意的点:

思路:实现IQuartzJobService,重写execute()方法;

步骤:1、判断ip是否与配置的ip一致;

           2、打印日志

           3、调用业务接口

以下是源码:

import cn.sh.ideal.elereport.service.IElereportService;
import cn.sh.ideal.framework.quartz.support.IQuartzJobService;
import cn.sh.ideal.framework.quartz.support.QuartzCfg;
import cn.sh.ideal.framework.util.Empty;
import cn.sh.ideal.kpi.service.IKpiService;
import cn.sh.ideal.system.entity.SystemConfig;
import cn.sh.ideal.system.service.ISystemConfigService;
import cn.sh.ideal.task.schdule.StatusChangeType;
import cn.sh.ideal.web.util.WebIpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.net.SocketException;
import java.net.UnknownHostException;


@QuartzCfg(cron = "30 1 0 25 * ?", desc = "电子月报每月25号定时发送")
public class OpenEleReport implements IQuartzJobService {
    private static final Logger LOGGER = LoggerFactory.getLogger(StatusChangeType.class);
    @Resource
    private ISystemConfigService systemConfigService;

    @Resource
    private WebIpUtil webIpUtil;

    @Resource
    private IElereportService elereportService;

    @Resource
    private IKpiService kpiService;

    @Override
    public void execute() {
        SystemConfig systemConfig = systemConfigService.findByKey("AUTHORITY_TASK_EXECUTE_IP");
        try {
            if (!Empty.isEmpty(systemConfig.getCfgValue()) && systemConfig.getCfgValue().equals(webIpUtil.configCheckLocal())) {
                LOGGER.info("Start sending electronic monthly reports.");
                //电子月报-重点工作定时发送
                elereportService.haveInterfaceAndMonth();
                //电子月报-战略KPI定时发送
                kpiService.haveInterfaceAndMonth();
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/supershuyun/article/details/86287608