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 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 checkState( jobListeners == null, "Re-entry is not allowed." );
61 checkState( triggerListeners == null, "Re-entry is not allowed." );
62 checkState( schedulerListeners == null, "Re-entry is not allowed." );
63 checkState( schedulerConfiguration == null, "Re-entry is not allowed." );
64
65 jobListeners = newSetBinder( binder(), JobListener.class );
66 triggerListeners = newSetBinder( binder(), TriggerListener.class );
67 schedulerListeners = newSetBinder( binder(), SchedulerListener.class );
68 schedulerConfiguration = new SchedulerConfiguration();
69
70 try
71 {
72 schedule();
73 bind( JobFactory.class ).to( InjectorJobFactory.class ).in( SINGLETON );
74 bind( Scheduler.class ).toProvider( SchedulerProvider.class ).asEagerSingleton();
75 bind( SchedulerConfiguration.class ).toInstance( schedulerConfiguration );
76 }
77 finally
78 {
79 jobListeners = null;
80 triggerListeners = null;
81 schedulerListeners = null;
82 schedulerConfiguration = null;
83 }
84 }
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 return schedulerConfiguration;
124 }
125
126
127
128
129
130
131 protected final void addJobListener( Class<? extends JobListener> jobListenerType )
132 {
133 doBind( jobListeners, jobListenerType );
134 }
135
136
137
138
139
140
141 protected final void addTriggerListener( Class<? extends TriggerListener> triggerListenerType )
142 {
143 doBind( triggerListeners, triggerListenerType );
144 }
145
146
147
148
149
150
151 protected final void addSchedulerListener( Class<? extends SchedulerListener> schedulerListenerType )
152 {
153 doBind( schedulerListeners, schedulerListenerType );
154 }
155
156
157
158
159
160
161
162
163
164
165
166 protected final JobSchedulerBuilder scheduleJob( Class<? extends Job> jobClass )
167 {
168 checkNotNull( jobClass, "Argument 'jobClass' must be not null." );
169
170 JobSchedulerBuilder builder = new JobSchedulerBuilder( jobClass );
171
172 if ( jobClass.isAnnotationPresent( Scheduled.class ) )
173 {
174 Scheduled scheduled = jobClass.getAnnotation( Scheduled.class );
175
176 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 if ( !DEFAULT.equals( scheduled.timeZoneId() ) )
187 {
188 TimeZone timeZone = getTimeZone( scheduled.timeZoneId() );
189 if ( timeZone != null )
190 {
191 builder.withTimeZone( timeZone );
192 }
193 }
194 }
195
196 requestInjection( builder );
197 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 checkNotNull( type, "Impossible to bind a null type" );
210 binder.addBinding().to( type );
211 }
212
213 private static void checkState( boolean state, String message )
214 {
215 if ( !state )
216 {
217 throw new IllegalStateException( message );
218 }
219 }
220
221 private static void checkNotNull( Object object, String message )
222 {
223 if ( object == null )
224 {
225 throw new IllegalArgumentException( message );
226 }
227 }
228
229 }