问题描述
我已经使用java spring boot task scheduler 创建了一个调度程序作业,该任务一个接一个地执行,也能够从DB获取cron表达式,但是这里的挑战是要指定no尝试在意外响应或失败的情况下尝试。我在下面给出了示例代码。
TaskScheduler taskScheduler;
private ScheduledFuture<?> job1;
private ScheduledFuture<?> job2;
private volatile boolean job1Flag = false;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(10);// Set the pool of threads
threadPoolTaskScheduler.setThreadNamePrefix("scheduler-thread");
threadPoolTaskScheduler.initialize();
scheduleJob1(threadPoolTaskScheduler);// Assign the job1 to the scheduler
scheduleJob2(threadPoolTaskScheduler);// Assign the job2 to the scheduler
this.taskScheduler = threadPoolTaskScheduler;// this will be used in later part of the article during refreshing
// the cron expression dynamically
taskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
private void scheduleJob1(TaskScheduler scheduler) {
job1 = scheduler.schedule(new Runnable() {
@Override
public void run() {
try {
HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:8040/getsqldatastream")
.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setConnectTimeout(20 * // minutes to sleep
60 * // seconds to a minute
1000); // set timeout to 1 second
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String output;
JSONObject outStream = new JSONObject();
while ((output = reader.readLine()) != null) {
JSONObject outPut = new JSONObject(output);
outStream = outPut;
}
int outputStatuscode = outStream.getInt("statusCode");
if ((con.getResponseCode() == HttpURLConnection.HTTP_OK) && outputStatuscode == 200) {
job1Flag = true;// setting the flag true to mark it complete
}
} catch (IOException e1) {
// Todo Auto-generated catch block
e1.printstacktrace();
}
System.out.println(Thread.currentThread().getName() + " The Task1 executed at " + new Date());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// Todo Auto-generated catch block
e.printstacktrace();
}
}
},new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String cronExp = "* 33 22 * * ?";// Can be pulled from a db
return new crontrigger(cronExp).nextExecutionTime(triggerContext);
}
});
}
private void scheduleJob2(TaskScheduler scheduler) {
job2 = scheduler.schedule(new Runnable() {
@Override
public void run() {
synchronized (this) {
while (!job1Flag) {
// System.out.println(Thread.currentThread().getName()
// + " waiting for job1 to complete to execute " + new Date());
try {
wait(10000);// add any number of seconds to wait
} catch (InterruptedException e) {
e.printstacktrace();
}
}
}
System.out.println(Thread.currentThread().getName() + " The Task2 executed at " + new Date());
job1Flag = false;
}
},new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String cronExp = "* 0/5 * * * ?";// Can be pulled from a db . This will run
// every minute
return new crontrigger(cronExp).nextExecutionTime(triggerContext);
}
});
}
在我的情况下,根据上述代码cron表达式设置为在晚上10:33之前运行,并且只有在响应符合预期的情况下,标记值才变为true。如果在给定时间完成之前快速返回响应,它将一次又一次轮询。在这里,我必须告诉我的调度程序,以防接下来发生成功响应,而在失败响应或flagvalue仍然为false的情况下,经过15分钟后再尝试一次,但不要超过此时间。我们应该如何为任务计划程序设置尝试限制。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)