-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
MultiInstanceRecurringConfiguration.java
117 lines (100 loc) · 4.1 KB
/
MultiInstanceRecurringConfiguration.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* 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.boot.config;
import com.github.kagkarlsson.examples.boot.ExampleContext;
import com.github.kagkarlsson.scheduler.task.ExecutionContext;
import com.github.kagkarlsson.scheduler.task.Task;
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import com.github.kagkarlsson.scheduler.task.TaskWithDataDescriptor;
import com.github.kagkarlsson.scheduler.task.helper.ScheduleAndData;
import com.github.kagkarlsson.scheduler.task.helper.Tasks;
import com.github.kagkarlsson.scheduler.task.schedule.CronSchedule;
import java.io.Serializable;
import java.time.Instant;
import java.util.Random;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import utils.EventLogger;
@Configuration
public class MultiInstanceRecurringConfiguration {
public static final TaskWithDataDescriptor<ScheduleAndCustomer> MULTI_INSTANCE_RECURRING_TASK =
new TaskWithDataDescriptor<>("multi-instance-recurring-task", ScheduleAndCustomer.class);
/** Start the example */
public static void start(ExampleContext ctx) {
CronSchedule cron = new CronSchedule(String.format("%s * * * * *", new Random().nextInt(59)));
Customer customer = new Customer(String.valueOf(new Random().nextInt(10000)));
ScheduleAndCustomer data = new ScheduleAndCustomer(cron, customer);
ctx.log(
"Scheduling instance of recurring task "
+ MULTI_INSTANCE_RECURRING_TASK.getTaskName()
+ " with data: "
+ data);
ctx.schedulerClient.schedule(
MULTI_INSTANCE_RECURRING_TASK.instance(customer.id, data),
cron.getInitialExecutionTime(Instant.now()));
}
/** Bean definition */
@Bean
public Task<ScheduleAndCustomer> multiInstanceRecurring() {
// This task will only start running when at least one instance of the task has been scheduled
return Tasks.recurringWithPersistentSchedule(MULTI_INSTANCE_RECURRING_TASK)
.execute(
(TaskInstance<ScheduleAndCustomer> taskInstance, ExecutionContext executionContext) -> {
ScheduleAndCustomer data = taskInstance.getData();
EventLogger.logTask(
MULTI_INSTANCE_RECURRING_TASK,
String.format(
"Ran according to schedule '%s' for customer %s",
data.getSchedule(), data.getData()));
});
}
public static class ScheduleAndCustomer implements ScheduleAndData {
private static final long serialVersionUID = 1L; // recommended when using Java serialization
private final CronSchedule schedule;
private final Customer customer;
private ScheduleAndCustomer() {
this(null, null);
} //
public ScheduleAndCustomer(CronSchedule schedule, Customer customer) {
this.schedule = schedule;
this.customer = customer;
}
@Override
public CronSchedule getSchedule() {
return schedule;
}
@Override
public Customer getData() {
return customer;
}
@Override
public String toString() {
return "ScheduleAndCustomer{" + "schedule=" + schedule + ", customer=" + customer + '}';
}
}
public static class Customer implements Serializable {
private static final long serialVersionUID = 1L; // recommended when using Java serialization
public final String id;
private Customer() {
this(null);
}
public Customer(String id) {
this.id = id;
}
@Override
public String toString() {
return "Customer{" + "id='" + id + '\'' + '}';
}
}
}