记录一下quartz的配置。
一、QuartzConfigration配置文件
import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.quartz.SchedulerFactoryBean; @Configuration public class QuartzConfigration { /** * attention: * Details:定义quartz调度工厂 */ @Bean(name = "schedulerSQL") public SchedulerFactoryBean schedulerFactory() { SchedulerFactoryBean bean = new SchedulerFactoryBean(); //quartz参数 Properties prop = new Properties(); prop.put("org.quartz.scheduler.instanceName", "CraftsoftScheduler"); prop.put("org.quartz.scheduler.instanceId", "AUTO"); //线程池配置 prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool"); prop.put("org.quartz.threadPool.threadCount", "20"); prop.put("org.quartz.threadPool.threadPriority", "5"); //JobStore配置 prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX"); //集群配置 prop.put("org.quartz.jobStore.isClustered", "true"); prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000"); prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1"); prop.put("org.quartz.jobStore.misfireThreshold", "12000"); prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_"); bean.setSchedulerName("CraftScheduler"); //延时启动 bean.setStartupDelay(5); bean.setApplicationContextSchedulerContextKey("applicationContextKey"); //可选,QuartzScheduler 启动时更新己存在的Job bean.setOverwriteExistingJobs(true); //设置自动启动,默认为true bean.setAutoStartup(true); return bean; } }
二、QuartzManager
import com.craftsman.modules.job.entity.ScheduleJobEntity; import org.quartz.*; public class QuartzManager { /** * 创建任务调度 */ public static void create(Scheduler scheduler, ScheduleJobEntity jobEntity) { try { //生成KEY JobKey jobkey = new JobKey(jobEntity.getJobKey()); TriggerKey triggerKey = new TriggerKey(jobEntity.getJobKey()); JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(jobkey).build(); //构建一个表达式,失效后再恢复并立即执行 CronScheduleBuilder schedBuilder = CronScheduleBuilder.cronSchedule(jobEntity.getCronExpression()).withMisfireHandlingInstructionDoNothing(); //构建一个trigger CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(schedBuilder).build(); //放入对象 jobDetail.getJobDataMap().put(ScheduleJobEntity.JOB_PARAMS_KEY, jobEntity); //注册触发器 scheduler.scheduleJob(jobDetail, trigger); //暂停任务 if (jobEntity.getStatus() == 1) { pause(scheduler, jobEntity); } } catch (SchedulerException e) { e.printStackTrace(); } } /** * 修改调度任务 */ public static void modify(Scheduler scheduler, ScheduleJobEntity jobEntity) { TriggerKey triggerKey = new TriggerKey(jobEntity.getJobKey()); try { CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); CronScheduleBuilder schedBuilder = CronScheduleBuilder.cronSchedule(jobEntity.getCronExpression()).withMisfireHandlingInstructionDoNothing(); //重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(schedBuilder).build(); //放入对象 trigger.getJobDataMap().put(ScheduleJobEntity.JOB_PARAMS_KEY, jobEntity); scheduler.rescheduleJob(triggerKey, trigger); //暂停任务 if (jobEntity.getStatus() == 1) { pause(scheduler, jobEntity); } } catch (SchedulerException e) { e.printStackTrace(); } } /** * 删除调度任务 */ public static void delete(Scheduler scheduler, ScheduleJobEntity jobEntity) { try { scheduler.deleteJob(JobKey.jobKey(jobEntity.getJobKey())); System.out.println("删除调度任务"); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 运行调度任务 */ public static void run(Scheduler scheduler, ScheduleJobEntity jobEntity) { try { //参数 JobDataMap dataMap = new JobDataMap(); dataMap.put(ScheduleJobEntity.JOB_PARAMS_KEY, jobEntity); scheduler.triggerJob(new JobKey(jobEntity.getJobKey()), dataMap); System.out.println("运行调度任务"); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 暂停调度任务 */ public static void pause(Scheduler scheduler, ScheduleJobEntity jobEntity) { try { scheduler.pauseJob(new JobKey(jobEntity.getJobKey())); System.out.println("暂停调度任务"); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 恢复调度任务 */ public static void resume(Scheduler scheduler, ScheduleJobEntity jobEntity) { try { scheduler.resumeJob(new JobKey(jobEntity.getJobKey())); System.err.println("恢复调度任务"); } catch (SchedulerException e) { e.printStackTrace(); } } }
三、ScheduleJob
import com.craftsman.modules.job.entity.ScheduleJobEntity; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.quartz.QuartzJobBean; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class ScheduleJob extends QuartzJobBean { private final Logger log = LoggerFactory.getLogger(ScheduleJob.class); private ExecutorService service = Executors.newSingleThreadExecutor(); @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { //根据主键获取job对象 ScheduleJobEntity jobEntity = (ScheduleJobEntity)context.getMergedJobDataMap().get(ScheduleJobEntity.JOB_PARAMS_KEY); //任务开始时间 long startTime = System.currentTimeMillis(); ScheduleRunnable schedule = new ScheduleRunnable(jobEntity); Future<?> future = service.submit(schedule); try { //通过获取线程结果来捕获异常 future.get(); //任务总计时间 long times = System.currentTimeMillis() - startTime; log.debug("执行完成,耗时:" + times / 1000f + "s"); } catch (InterruptedException e) { log.error("任务执行失败,失败原因:" + e.getMessage()); } catch (ExecutionException e) { log.error("任务执行失败,失败原因:" + e.getMessage()); } } }
四、ScheduleRunnable
import com.craftsman.common.utils.SpringContextUtils; import com.craftsman.modules.job.entity.ScheduleJobEntity; import org.springframework.util.ReflectionUtils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class ScheduleRunnable implements Runnable { private ScheduleJobEntity jobEntity; public ScheduleRunnable(ScheduleJobEntity jobEntity) { this.jobEntity = jobEntity; } @Override public void run() { //利用反射执行bean中的方法 try { Object target = SpringContextUtils.getBean(jobEntity.getBeanName()); Object params = jobEntity.getParams(); Method method = null; if (null != params && !params.equals("")) { method = target.getClass().getDeclaredMethod(jobEntity.getMethodName(), params.getClass()); ReflectionUtils.makeAccessible(method); method.invoke(target, params); } else { method = target.getClass().getDeclaredMethod(jobEntity.getMethodName()); ReflectionUtils.makeAccessible(method); method.invoke(target); } } catch (NoSuchMethodException e) { throw new RuntimeException("执行任务调度异常,找不到执行方,详细:" + e.getMessage()); } catch (SecurityException e) { throw new RuntimeException("执行任务调度异常,详细:" + e.getMessage()); } catch (IllegalAccessException e) { throw new RuntimeException("执行任务调度异常,详细:" + e.getMessage()); } catch (IllegalArgumentException e) { throw new RuntimeException("执行任务调度异常,详细:" + e.getMessage()); } catch (InvocationTargetException e) { throw new RuntimeException("执行任务调度异常,详细:" + e.getMessage()); } } }
五、ScheduleJobEntity
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.util.Date; @TableName(value = "SCHEDULE_JOB") public class ScheduleJobEntity { /** * 任务运行 */ public static final int SCHEDULE_JOB_RUN = 0; /** * 任务停止 */ public static final int SCHEDULE_JOB_STOP = 1; /** * 传递参数的KEY */ public static String JOB_PARAMS_KEY = "JOB_PARAMS_KEY"; @TableId private String id; /** * 名称 */ private String name; /** * 时间表达式 */ private String cronExpression; /** * bean名称 */ private String beanName; /** * 方法名称 */ private String methodName; /** * 参数(可以使用json格式) */ private Object params; /** * 状态 0 运行 1暂停 */ private int status; /** * 创建时间 */ private Date createTime; /** * 备注 */ private String remark; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCronExpression() { return cronExpression; } public void setCronExpression(String cronExpression) { this.cronExpression = cronExpression; } public String getBeanName() { return beanName; } public void setBeanName(String beanName) { this.beanName = beanName; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public Object getParams() { return params; } public void setParams(Object params) { this.params = params; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } /** * 获取job key * @return */ public String getJobKey() { return this.beanName + "_" + this.id; } }
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。