1 | |
package org.apache.onami.scheduler; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import static com.google.inject.Scopes.SINGLETON; |
23 | |
import static com.google.inject.multibindings.Multibinder.newSetBinder; |
24 | |
import static java.util.TimeZone.getTimeZone; |
25 | |
import static org.apache.onami.scheduler.Scheduled.DEFAULT; |
26 | |
|
27 | |
import java.util.TimeZone; |
28 | |
|
29 | |
import org.quartz.Job; |
30 | |
import org.quartz.JobListener; |
31 | |
import org.quartz.Scheduler; |
32 | |
import org.quartz.SchedulerListener; |
33 | |
import org.quartz.TriggerListener; |
34 | |
import org.quartz.spi.JobFactory; |
35 | |
|
36 | |
import com.google.inject.AbstractModule; |
37 | |
import com.google.inject.multibindings.Multibinder; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 5 | public abstract class QuartzModule |
43 | |
extends AbstractModule |
44 | |
{ |
45 | |
|
46 | |
private Multibinder<JobListener> jobListeners; |
47 | |
|
48 | |
private Multibinder<TriggerListener> triggerListeners; |
49 | |
|
50 | |
private Multibinder<SchedulerListener> schedulerListeners; |
51 | |
|
52 | |
private SchedulerConfiguration schedulerConfiguration; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
@Override |
58 | |
protected final void configure() |
59 | |
{ |
60 | 5 | checkState( jobListeners == null, "Re-entry is not allowed." ); |
61 | 5 | checkState( triggerListeners == null, "Re-entry is not allowed." ); |
62 | 5 | checkState( schedulerListeners == null, "Re-entry is not allowed." ); |
63 | 5 | checkState( schedulerConfiguration == null, "Re-entry is not allowed." ); |
64 | |
|
65 | 5 | jobListeners = newSetBinder( binder(), JobListener.class ); |
66 | 5 | triggerListeners = newSetBinder( binder(), TriggerListener.class ); |
67 | 5 | schedulerListeners = newSetBinder( binder(), SchedulerListener.class ); |
68 | 5 | schedulerConfiguration = new SchedulerConfiguration(); |
69 | |
|
70 | |
try |
71 | |
{ |
72 | 5 | schedule(); |
73 | 5 | bind( JobFactory.class ).to( InjectorJobFactory.class ).in( SINGLETON ); |
74 | 5 | bind( Scheduler.class ).toProvider( SchedulerProvider.class ).asEagerSingleton(); |
75 | 5 | bind( SchedulerConfiguration.class ).toInstance( schedulerConfiguration ); |
76 | |
} |
77 | |
finally |
78 | |
{ |
79 | 5 | jobListeners = null; |
80 | 5 | triggerListeners = null; |
81 | 5 | schedulerListeners = null; |
82 | 5 | schedulerConfiguration = null; |
83 | 5 | } |
84 | 5 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
protected abstract void schedule(); |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
protected final SchedulerConfigurationBuilder configureScheduler() |
122 | |
{ |
123 | 2 | return schedulerConfiguration; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
protected final void addJobListener( Class<? extends JobListener> jobListenerType ) |
132 | |
{ |
133 | 0 | doBind( jobListeners, jobListenerType ); |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
protected final void addTriggerListener( Class<? extends TriggerListener> triggerListenerType ) |
142 | |
{ |
143 | 0 | doBind( triggerListeners, triggerListenerType ); |
144 | 0 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
protected final void addSchedulerListener( Class<? extends SchedulerListener> schedulerListenerType ) |
152 | |
{ |
153 | 0 | doBind( schedulerListeners, schedulerListenerType ); |
154 | 0 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
protected final JobSchedulerBuilder scheduleJob( Class<? extends Job> jobClass ) |
167 | |
{ |
168 | 5 | checkNotNull( jobClass, "Argument 'jobClass' must be not null." ); |
169 | |
|
170 | 5 | JobSchedulerBuilder builder = new JobSchedulerBuilder( jobClass ); |
171 | |
|
172 | 5 | if ( jobClass.isAnnotationPresent( Scheduled.class ) ) |
173 | |
{ |
174 | 4 | Scheduled scheduled = jobClass.getAnnotation( Scheduled.class ); |
175 | |
|
176 | 4 | builder |
177 | |
|
178 | |
.withJobName( scheduled.jobName() ) |
179 | |
.withJobGroup( scheduled.jobGroup() ) |
180 | |
.withRequestRecovery( scheduled.requestRecovery() ) |
181 | |
.withStoreDurably( scheduled.storeDurably() ) |
182 | |
|
183 | |
.withCronExpression( scheduled.cronExpression() ) |
184 | |
.withTriggerName( scheduled.triggerName() ); |
185 | |
|
186 | 4 | if ( !DEFAULT.equals( scheduled.timeZoneId() ) ) |
187 | |
{ |
188 | 0 | TimeZone timeZone = getTimeZone( scheduled.timeZoneId() ); |
189 | 0 | if ( timeZone != null ) |
190 | |
{ |
191 | 0 | builder.withTimeZone( timeZone ); |
192 | |
} |
193 | |
} |
194 | |
} |
195 | |
|
196 | 5 | requestInjection( builder ); |
197 | 5 | return builder; |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
protected final <T> void doBind( Multibinder<T> binder, Class<? extends T> type ) |
208 | |
{ |
209 | 0 | checkNotNull( type, "Impossible to bind a null type" ); |
210 | 0 | binder.addBinding().to( type ); |
211 | 0 | } |
212 | |
|
213 | |
private static void checkState( boolean state, String message ) |
214 | |
{ |
215 | 20 | if ( !state ) |
216 | |
{ |
217 | 0 | throw new IllegalStateException( message ); |
218 | |
} |
219 | 20 | } |
220 | |
|
221 | |
private static void checkNotNull( Object object, String message ) |
222 | |
{ |
223 | 5 | if ( object == null ) |
224 | |
{ |
225 | 0 | throw new IllegalArgumentException( message ); |
226 | |
} |
227 | 5 | } |
228 | |
|
229 | |
} |