展开全部
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);
}
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起