28
28
29
29
public class JobsPlugin extends PlayPlugin {
30
30
31
- public static ScheduledThreadPoolExecutor executor = null ;
32
- public static List <Job > scheduledJobs = null ;
33
- private static ThreadLocal <List <Callable <? extends Object >>> afterInvocationActions = new ThreadLocal <List <Callable <? extends Object >>>();
31
+ public static ScheduledThreadPoolExecutor executor ;
32
+ public static List <Job > scheduledJobs ;
33
+ private static ThreadLocal <List <Callable <?>>> afterInvocationActions = new ThreadLocal <List <Callable <?>>>();
34
34
35
35
@ Override
36
36
public String getStatus () {
@@ -54,7 +54,7 @@ public String getStatus() {
54
54
out .println ("Scheduled jobs (" + scheduledJobs .size () + "):" );
55
55
out .println ("~~~~~~~~~~~~~~~~~~~~~~~~~~" );
56
56
for (Job job : scheduledJobs ) {
57
- out .print (job . getClass (). getName () );
57
+ out .print (job );
58
58
if (job .getClass ().isAnnotationPresent (OnApplicationStart .class )
59
59
&& !(job .getClass ().isAnnotationPresent (On .class ) || job .getClass ().isAnnotationPresent (Every .class ))) {
60
60
OnApplicationStart appStartAnnotation = job .getClass ().getAnnotation (OnApplicationStart .class );
@@ -89,10 +89,9 @@ public String getStatus() {
89
89
out .println ();
90
90
out .println ("Waiting jobs:" );
91
91
out .println ("~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
92
- ScheduledFuture [] q = executor .getQueue ().toArray (new ScheduledFuture [0 ]);
92
+ ScheduledFuture [] q = executor .getQueue ().toArray (new ScheduledFuture [executor . getQueue (). size () ]);
93
93
94
- for (int i = 0 ; i < q .length ; i ++) {
95
- ScheduledFuture task = q [i ];
94
+ for (ScheduledFuture task : q ) {
96
95
out .println (Java .extractUnderlyingCallable ((FutureTask <?>) task ) + " will run in " + task .getDelay (TimeUnit .SECONDS )
97
96
+ " seconds" );
98
97
}
@@ -108,7 +107,6 @@ public void afterApplicationStart() {
108
107
jobs .add (clazz );
109
108
}
110
109
}
111
- scheduledJobs = new ArrayList <Job >();
112
110
for (final Class <?> clazz : jobs ) {
113
111
// @OnApplicationStart
114
112
if (clazz .isAnnotationPresent (OnApplicationStart .class )) {
@@ -117,8 +115,7 @@ public void afterApplicationStart() {
117
115
if (!appStartAnnotation .async ()) {
118
116
// run job sync
119
117
try {
120
- Job <?> job = ((Job <?>) clazz .newInstance ());
121
- scheduledJobs .add (job );
118
+ Job <?> job = createJob (clazz );
122
119
job .run ();
123
120
if (job .wasError ) {
124
121
if (job .lastException != null ) {
@@ -139,8 +136,7 @@ public void afterApplicationStart() {
139
136
} else {
140
137
// run job async
141
138
try {
142
- Job <?> job = ((Job <?>) clazz .newInstance ());
143
- scheduledJobs .add (job );
139
+ Job <?> job = createJob (clazz );
144
140
// start running job now in the background
145
141
@ SuppressWarnings ("unchecked" )
146
142
Callable <Job > callable = (Callable <Job >) job ;
@@ -156,8 +152,7 @@ public void afterApplicationStart() {
156
152
// @On
157
153
if (clazz .isAnnotationPresent (On .class )) {
158
154
try {
159
- Job <?> job = ((Job <?>) clazz .newInstance ());
160
- scheduledJobs .add (job );
155
+ Job <?> job = createJob (clazz );
161
156
scheduleForCRON (job );
162
157
} catch (InstantiationException ex ) {
163
158
throw new UnexpectedException ("Cannot instanciate Job " + clazz .getName ());
@@ -168,8 +163,7 @@ public void afterApplicationStart() {
168
163
// @Every
169
164
if (clazz .isAnnotationPresent (Every .class )) {
170
165
try {
171
- Job job = (Job ) clazz .newInstance ();
172
- scheduledJobs .add (job );
166
+ Job job = createJob (clazz );
173
167
String value = job .getClass ().getAnnotation (Every .class ).value ();
174
168
if (value .startsWith ("cron." )) {
175
169
value = Play .configuration .getProperty (value );
@@ -187,10 +181,17 @@ public void afterApplicationStart() {
187
181
}
188
182
}
189
183
184
+ private Job <?> createJob (Class <?> clazz ) throws InstantiationException , IllegalAccessException {
185
+ Job <?> job = (Job <?>) clazz .newInstance ();
186
+ scheduledJobs .add (job );
187
+ return job ;
188
+ }
189
+
190
190
@ Override
191
191
public void onApplicationStart () {
192
192
int core = Integer .parseInt (Play .configuration .getProperty ("play.jobs.pool" , "10" ));
193
193
executor = new ScheduledThreadPoolExecutor (core , new PThreadFactory ("jobs" ), new ThreadPoolExecutor .AbortPolicy ());
194
+ scheduledJobs = new ArrayList <Job >();
194
195
}
195
196
196
197
public static <V > void scheduleForCRON (Job <V > job ) {
@@ -202,7 +203,7 @@ public static <V> void scheduleForCRON(Job<V> job) {
202
203
cron = Play .configuration .getProperty (cron );
203
204
}
204
205
cron = Expression .evaluate (cron , cron ).toString ();
205
- if (cron == null || "" . equals ( cron ) || "never" .equalsIgnoreCase (cron )) {
206
+ if (cron == null || cron . isEmpty ( ) || "never" .equalsIgnoreCase (cron )) {
206
207
Logger .info ("Skipping job %s, cron expression is not defined" , job .getClass ().getName ());
207
208
return ;
208
209
}
@@ -212,8 +213,8 @@ public static <V> void scheduleForCRON(Job<V> job) {
212
213
CronExpression cronExp = new CronExpression (cron );
213
214
Date nextDate = cronExp .getNextValidTimeAfter (now );
214
215
if (nextDate == null ) {
215
- Logger .warn ("The cron expression for job %s doesn't have any match in the future, will never be executed" , job . getClass ()
216
- .getName ());
216
+ Logger .warn ("The cron expression for job %s doesn't have any match in the future, will never be executed" ,
217
+ job . getClass () .getName ());
217
218
return ;
218
219
}
219
220
if (nextDate .equals (job .nextPlannedExecution )) {
@@ -240,8 +241,7 @@ public void onApplicationStop() {
240
241
// @OnApplicationStop
241
242
if (clazz .isAnnotationPresent (OnApplicationStop .class )) {
242
243
try {
243
- Job <?> job = ((Job <?>) clazz .newInstance ());
244
- scheduledJobs .add (job );
244
+ Job <?> job = createJob (clazz );
245
245
job .run ();
246
246
if (job .wasError ) {
247
247
if (job .lastException != null ) {
@@ -268,20 +268,20 @@ public void onApplicationStop() {
268
268
269
269
@ Override
270
270
public void beforeInvocation () {
271
- afterInvocationActions .set (new LinkedList <Callable <? extends Object >>());
271
+ afterInvocationActions .set (new LinkedList <Callable <?>>());
272
272
}
273
273
274
274
@ Override
275
275
public void afterInvocation () {
276
- List <Callable <? extends Object >> currentActions = afterInvocationActions .get ();
276
+ List <Callable <?>> currentActions = afterInvocationActions .get ();
277
277
afterInvocationActions .set (null );
278
- for (Callable <? extends Object > callable : currentActions ) {
279
- JobsPlugin . executor .submit (callable );
278
+ for (Callable <?> callable : currentActions ) {
279
+ executor .submit (callable );
280
280
}
281
281
}
282
282
283
283
// default visibility, because we want to use this only from Job.java
284
- static void addAfterRequestAction (Callable <? extends Object > c ) {
284
+ static void addAfterRequestAction (Callable <?> c ) {
285
285
if (Request .current () == null ) {
286
286
throw new IllegalStateException ("After request actions can be added only from threads that serve requests!" );
287
287
}
0 commit comments