java 修改cron表达式_如何实现cron表达式的统一管理,不用实现修改代码就能修改定时任务执行时间?...

展开全部

ThreadPoolTaskScheduler

你可能参数这个类,看能不能满足你的62616964757a686964616fe4b893e5b19e31333433643033需求 。下面是我以前实现的文件。

package cn.lyq.service;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.concurrent.ScheduledFuture;

import javax.annotation.PostConstruct;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import org.springframework.scheduling.support.CronTrigger;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.lyq.pojo.TimeJob;

import cn.lyq.utils.FileUtils;

import cn.lyq.utils.HttpUtils;

import cn.lyq.utils.LogUtils;

import cn.lyq.utils.SystemUtils;

import lombok.extern.log4j.Log4j;

@Service

@Log4j

public class JobServer implements IJobServer {

private Map jobs = new HashMap();

private ThreadPoolTaskScheduler taskScheduler = null;

private final String fileName = "jobs.data";

public ThreadPoolTaskScheduler createThreadPoolTaskScheduler() {

ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();

executor.setPoolSize(20);

executor.setThreadNamePrefix("task-");

// 用来设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean

executor.setWaitForTasksToCompleteOnShutdown(true);

// 该方法用来设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住。

executor.setAwaitTerminationSeconds(60);

executor.initialize();

return executor;

}

public void readFromFile() {

this.addJobs(FileUtils.readFileToString(this.fileName));

}

public void saveToFile() {

FileUtils.writeToFile(this.fileName, this.getJos());

}

@PostConstruct

public void init() {

this.taskScheduler = this.createThreadPoolTaskScheduler();

this.readFromFile();

}

@Override

public void addJobs(String jobJSONStr) {

if (jobJSONStr.equals("")) {

return;

}

List jobs = JSON.parseArray(jobJSONStr, TimeJob.class);

for (int i = 0; i < jobs.size(); i++) {

this.addJob(i, jobs.get(i));

}

}

@SuppressWarnings("rawtypes")

@Override

public void delJob(String jobsId) {

// TODO Auto-generated method stub

ScheduledFuture future = null;

if (this.jobs.containsKey(jobsId)) {

future = this.jobs.get(jobsId).getFuture();

this.jobs.remove(jobsId);

}

if (future == null) {

log.info(String.format("there is no job[%s]", jobsId));

} else {

log.info(String.format("cancel job[%s] finish", jobsId));

future.cancel(true);

}

this.saveToFile();

}

@Override

public String getJos() {

return JSON.toJSONString(this.jobs.values());

}

private void addJob(int index, TimeJob timeJob) {

try {

timeJob.setJobId(

String.format("%d%s", System.currentTimeMillis(), index));

timeJob.setFuture(this.taskScheduler.schedule(new runTask(timeJob),

new CronTrigger(timeJob.getCron())));

this.jobs.put(timeJob.getJobId(), timeJob);

} catch (Exception e) {

LogUtils.traceError(log, e);

}

this.saveToFile();

}

public class runTask implements Runnable {

public TimeJob timeJob = null;

public int times = 0;

public runTask(TimeJob timeJob) {

this.timeJob = timeJob;

}

private void taskByType(long startTime, TimeJob timeJob, int times) {

try {

switch (timeJob.getTaskType()) {

case 1:

SystemUtils.runCmd(timeJob.getCommand());

break;

case 2:

HttpUtils.doGet(timeJob.getCommand(), null);

break;

case 3:

System.out.println(timeJob.getCommand());

break;

default:

break;

}

} catch (Exception e) {

LogUtils.traceError(log, e);

} finally {

LogUtils.printTask(log, times, startTime, timeJob);

}

}

@Override

public void run() {

this.taskByType(System.currentTimeMillis(), this.timeJob,

++this.times);

}

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值