forked from kagkarlsson/db-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobChainingUsingSeparateTasksMain.java
95 lines (77 loc) · 3.46 KB
/
JobChainingUsingSeparateTasksMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Copyright (C) Gustav Karlsson
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.kagkarlsson.examples;
import com.github.kagkarlsson.examples.helpers.Example;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.task.*;
import com.github.kagkarlsson.scheduler.task.helper.CustomTask;
import com.github.kagkarlsson.scheduler.task.helper.Tasks;
import javax.sql.DataSource;
import java.io.Serializable;
import java.time.Duration;
import java.time.Instant;
public class JobChainingUsingSeparateTasksMain extends Example {
public static void main(String[] args) {
new JobChainingUsingSeparateTasksMain().runWithDatasource();
}
@Override
public void run(DataSource dataSource) {
final CustomTask<JobId> jobStep1 = Tasks.custom("job-step-1", JobId.class)
.execute((taskInstance, executionContext) -> {
System.out.println("Step1 ran. Job: " + taskInstance.getData());
return new OnCompleteRemoveAndCreateNextStep("job-step-2");
});
final CustomTask<JobId> jobStep2 = Tasks.custom("job-step-2", JobId.class)
.execute((taskInstance, executionContext) -> {
System.out.println("Step2 ran. Removing multistep-job. Job: " + taskInstance.getData());
return new CompletionHandler.OnCompleteRemove<>();
});
final Scheduler scheduler = Scheduler
.create(dataSource, jobStep1, jobStep2)
.enableImmediateExecution() // will cause job scheduled to now() to run directly
.pollingInterval(Duration.ofSeconds(10))
.build();
scheduler.start();
sleep(1_000);
// Schedule a multistep job. Simulate some instance-specific data, id=507
scheduler.schedule(jobStep1.instance("job-507", new JobId(507)), Instant.now()); // both steps will run directly
}
public static class JobId implements Serializable {
private static long serialVersionUID = 1L;
public int id;
public JobId(int id) {
this.id = id;
}
@Override
public String toString() {
return "JobId{" +
"id=" + id +
'}';
}
}
class OnCompleteRemoveAndCreateNextStep implements CompletionHandler<JobId> {
private final String newTaskName;
public OnCompleteRemoveAndCreateNextStep(String newTaskName) {
this.newTaskName = newTaskName;
}
@Override
public void complete(ExecutionComplete executionComplete, ExecutionOperations<JobId> executionOperations) {
TaskInstance taskInstance = executionComplete.getExecution().taskInstance;
TaskInstance<JobId> nextInstance = new TaskInstance<>(newTaskName, taskInstance.getId(), (JobId) taskInstance.getData());
executionOperations.removeAndScheduleNew(SchedulableInstance.of(nextInstance, Instant.now()));
}
}
}