This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
ConfiguredCronScheduleComponent.java
86 lines (73 loc) · 2.12 KB
/
ConfiguredCronScheduleComponent.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
package osgi.enroute.examples.scheduler.examples;
import java.util.Map;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
import osgi.enroute.dto.api.DTOs;
import osgi.enroute.examples.scheduler.application.SchedulerApplication;
import osgi.enroute.scheduler.api.CronJob;
/**
* This component is an example for a component that receives its schedule
* through Configuration Admin. If the PID {@value PID} is set, it will creat
* this component. The {@value CronJob#CRON} service property should be set with
* a Cron schedule.
*/
@Component(configurationPolicy = ConfigurationPolicy.REQUIRE, configurationPid = ConfiguredCronScheduleComponent.PID)
public class ConfiguredCronScheduleComponent implements
CronJob<ConfiguredCronScheduleComponent.CronData> {
public static final String PID = "cron.pid";
/**
* The cron expression can be preceded by properties. These properties can
* be mapped to this interface. An instance of this interface is then passed
* to the {@link CronJob}.
*/
public interface CronData {
/**
* A message. For example:
*
* <pre>
* message=foo
* * * * * * ?
* </pre>
*/
String message();
}
/**
* Define the schema for the configuration data.
*/
public interface Configuration {
/**
* The tracker id
*/
int id();
/**
* The cron expression
*/
String cron();
}
@Reference
private SchedulerApplication app;
@Reference
private DTOs dtos;
private int id;
private Configuration configuration;
/*
* Activate
*/
@Activate
void activate(Map<String, Object> map) throws Exception {
configuration = dtos.convert(map).to(Configuration.class);
this.id = configuration.id();
assert id != 0 : "This component requires an id";
}
/*
* This method is called as indicated by the cron expression.
*/
@Override
public void run(CronData data) throws Exception {
System.out.println("Fired Cron Component " + id + " "
+ configuration.cron());
app.fire(id);
}
}