All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kedauis.config.InvokingJobDetailDetailFactory Maven / Gradle / Ivy

package com.kedauis.config;

import com.kedauis.common.service.QuartzService;
import com.kedauis.system.model.SysQuartzLogs;
import com.kedauis.util.DateUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.lang.reflect.Method;

public class InvokingJobDetailDetailFactory extends QuartzJobBean {
 
    // 计划任务所在类 
    private String targetObject;
   
    // 具体需要执行的计划任务 
    private String targetMethod;

    private String targetTask;
     
    private ApplicationContext ctx;
     
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        SysQuartzLogs sysQuartzLogs = new SysQuartzLogs();
        sysQuartzLogs.setTask(targetTask);
        sysQuartzLogs.setTriggerTime(DateUtil.getNowTimeStr());
        QuartzService quartzService = null;
        try {
            Object otargetObject = ctx.getBean(targetObject);
            quartzService = (QuartzService)ctx.getBean("quartzService");
            Method m = otargetObject.getClass().getMethod(targetMethod);
            m.invoke(otargetObject);
            sysQuartzLogs.setFinishTime(DateUtil.getNowTimeStr());
            sysQuartzLogs.setState(SysQuartzLogs.SUCCESS);
            sysQuartzLogs.countTime();
        } catch (Exception e) {
            sysQuartzLogs.setState(SysQuartzLogs.FAILED);
            sysQuartzLogs.setLogInfos(e.getMessage());
            throw new JobExecutionException(e); 
        }finally {
            if(quartzService != null){
                quartzService.insQuartzLog(sysQuartzLogs);
            }
        }
    }
 
    public void setApplicationContext(ApplicationContext applicationContext) { 
        this.ctx = applicationContext; 
    } 
   
    public void setTargetObject(String targetObject) { 
        this.targetObject = targetObject; 
    } 
   
    public void setTargetMethod(String targetMethod) { 
        this.targetMethod = targetMethod; 
    }

    public void setTargetTask(String targetTask) {
        this.targetTask = targetTask;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy