5
5
import java .text .SimpleDateFormat ;
6
6
import java .util .ArrayList ;
7
7
import java .util .Date ;
8
- import java .util .List ;
9
8
import java .util .LinkedList ;
10
- import java .util .concurrent .*;
9
+ import java .util .List ;
10
+ import java .util .concurrent .Callable ;
11
+ import java .util .concurrent .FutureTask ;
12
+ import java .util .concurrent .ScheduledFuture ;
13
+ import java .util .concurrent .ScheduledThreadPoolExecutor ;
14
+ import java .util .concurrent .ThreadPoolExecutor ;
15
+ import java .util .concurrent .TimeUnit ;
11
16
12
17
import play .Logger ;
13
18
import play .Play ;
14
19
import play .PlayPlugin ;
15
20
import play .exceptions .PlayException ;
16
21
import play .exceptions .UnexpectedException ;
22
+ import play .libs .CronExpression ;
17
23
import play .libs .Expression ;
18
24
import play .libs .Time ;
19
- import play .libs .Time .CronExpression ;
20
25
import play .mvc .Http .Request ;
21
26
import play .utils .Java ;
22
27
import play .utils .PThreadFactory ;
@@ -46,16 +51,17 @@ public String getStatus() {
46
51
SimpleDateFormat df = new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss" );
47
52
if (!scheduledJobs .isEmpty ()) {
48
53
out .println ();
49
- out .println ("Scheduled jobs (" + scheduledJobs .size ()+ "):" );
54
+ out .println ("Scheduled jobs (" + scheduledJobs .size () + "):" );
50
55
out .println ("~~~~~~~~~~~~~~~~~~~~~~~~~~" );
51
56
for (Job job : scheduledJobs ) {
52
57
out .print (job .getClass ().getName ());
53
- if (job .getClass ().isAnnotationPresent (OnApplicationStart .class ) && !(job .getClass ().isAnnotationPresent (On .class ) || job .getClass ().isAnnotationPresent (Every .class ))) {
58
+ if (job .getClass ().isAnnotationPresent (OnApplicationStart .class )
59
+ && !(job .getClass ().isAnnotationPresent (On .class ) || job .getClass ().isAnnotationPresent (Every .class ))) {
54
60
OnApplicationStart appStartAnnotation = job .getClass ().getAnnotation (OnApplicationStart .class );
55
- out .print (" run at application start" + (appStartAnnotation .async ()? " (async)" : "" ) + "." );
61
+ out .print (" run at application start" + (appStartAnnotation .async () ? " (async)" : "" ) + "." );
56
62
}
57
63
58
- if ( job .getClass ().isAnnotationPresent (On .class )) {
64
+ if ( job .getClass ().isAnnotationPresent (On .class )) {
59
65
60
66
String cron = job .getClass ().getAnnotation (On .class ).value ();
61
67
if (cron != null && cron .startsWith ("cron." )) {
@@ -68,7 +74,7 @@ public String getStatus() {
68
74
}
69
75
if (job .lastRun > 0 ) {
70
76
out .print (" (last run at " + df .format (new Date (job .lastRun )));
71
- if (job .wasError ) {
77
+ if (job .wasError ) {
72
78
out .print (" with error)" );
73
79
} else {
74
80
out .print (")" );
@@ -87,13 +93,13 @@ public String getStatus() {
87
93
88
94
for (int i = 0 ; i < q .length ; i ++) {
89
95
ScheduledFuture task = q [i ];
90
- out .println (Java .extractUnderlyingCallable ((FutureTask <?>) task ) + " will run in " + task .getDelay (TimeUnit .SECONDS ) + " seconds" );
96
+ out .println (Java .extractUnderlyingCallable ((FutureTask <?>) task ) + " will run in " + task .getDelay (TimeUnit .SECONDS )
97
+ + " seconds" );
91
98
}
92
99
}
93
100
return sw .toString ();
94
101
}
95
102
96
-
97
103
@ Override
98
104
public void afterApplicationStart () {
99
105
List <Class <?>> jobs = new ArrayList <Class <?>>();
@@ -106,16 +112,16 @@ public void afterApplicationStart() {
106
112
for (final Class <?> clazz : jobs ) {
107
113
// @OnApplicationStart
108
114
if (clazz .isAnnotationPresent (OnApplicationStart .class )) {
109
- //check if we're going to run the job sync or async
115
+ // check if we're going to run the job sync or async
110
116
OnApplicationStart appStartAnnotation = clazz .getAnnotation (OnApplicationStart .class );
111
- if ( !appStartAnnotation .async ()) {
112
- //run job sync
117
+ if ( !appStartAnnotation .async ()) {
118
+ // run job sync
113
119
try {
114
120
Job <?> job = ((Job <?>) clazz .newInstance ());
115
121
scheduledJobs .add (job );
116
122
job .run ();
117
- if (job .wasError ) {
118
- if (job .lastException != null ) {
123
+ if (job .wasError ) {
124
+ if (job .lastException != null ) {
119
125
throw job .lastException ;
120
126
}
121
127
throw new RuntimeException ("@OnApplicationStart Job has failed" );
@@ -131,13 +137,13 @@ public void afterApplicationStart() {
131
137
throw new UnexpectedException (ex );
132
138
}
133
139
} else {
134
- //run job async
140
+ // run job async
135
141
try {
136
142
Job <?> job = ((Job <?>) clazz .newInstance ());
137
143
scheduledJobs .add (job );
138
- //start running job now in the background
144
+ // start running job now in the background
139
145
@ SuppressWarnings ("unchecked" )
140
- Callable <Job > callable = (Callable <Job >)job ;
146
+ Callable <Job > callable = (Callable <Job >) job ;
141
147
executor .submit (callable );
142
148
} catch (InstantiationException ex ) {
143
149
throw new UnexpectedException ("Cannot instanciate Job " + clazz .getName ());
@@ -169,7 +175,7 @@ public void afterApplicationStart() {
169
175
value = Play .configuration .getProperty (value );
170
176
}
171
177
value = Expression .evaluate (value , value ).toString ();
172
- if (!"never" .equalsIgnoreCase (value )){
178
+ if (!"never" .equalsIgnoreCase (value )) {
173
179
executor .scheduleWithFixedDelay (job , Time .parseDuration (value ), Time .parseDuration (value ), TimeUnit .SECONDS );
174
180
}
175
181
} catch (InstantiationException ex ) {
@@ -206,17 +212,19 @@ public static <V> void scheduleForCRON(Job<V> job) {
206
212
CronExpression cronExp = new CronExpression (cron );
207
213
Date nextDate = cronExp .getNextValidTimeAfter (now );
208
214
if (nextDate == null ) {
209
- Logger .warn ("The cron expression for job %s doesn't have any match in the future, will never be executed" , job .getClass ().getName ());
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 ());
210
217
return ;
211
218
}
212
219
if (nextDate .equals (job .nextPlannedExecution )) {
213
220
// Bug #13: avoid running the job twice for the same time
214
- // (happens when we end up running the job a few minutes before the planned time)
221
+ // (happens when we end up running the job a few minutes before
222
+ // the planned time)
215
223
Date nextInvalid = cronExp .getNextInvalidTimeAfter (nextDate );
216
224
nextDate = cronExp .getNextValidTimeAfter (nextInvalid );
217
225
}
218
226
job .nextPlannedExecution = nextDate ;
219
- executor .schedule ((Callable <V >)job , nextDate .getTime () - now .getTime (), TimeUnit .MILLISECONDS );
227
+ executor .schedule ((Callable <V >) job , nextDate .getTime () - now .getTime (), TimeUnit .MILLISECONDS );
220
228
job .executor = executor ;
221
229
} catch (Exception ex ) {
222
230
throw new UnexpectedException (ex );
@@ -260,23 +268,23 @@ public void onApplicationStop() {
260
268
261
269
@ Override
262
270
public void beforeInvocation () {
263
- afterInvocationActions .set (new LinkedList <Callable <? extends Object >>());
271
+ afterInvocationActions .set (new LinkedList <Callable <? extends Object >>());
264
272
}
265
273
266
274
@ Override
267
275
public void afterInvocation () {
268
- List <Callable <? extends Object >> currentActions = afterInvocationActions .get ();
269
- afterInvocationActions .set (null );
270
- for (Callable <? extends Object > callable : currentActions ) {
271
- JobsPlugin .executor .submit (callable );
272
- }
276
+ List <Callable <? extends Object >> currentActions = afterInvocationActions .get ();
277
+ afterInvocationActions .set (null );
278
+ for (Callable <? extends Object > callable : currentActions ) {
279
+ JobsPlugin .executor .submit (callable );
280
+ }
273
281
}
274
282
275
283
// default visibility, because we want to use this only from Job.java
276
284
static void addAfterRequestAction (Callable <? extends Object > c ) {
277
- if (Request .current () == null ) {
278
- throw new IllegalStateException ("After request actions can be added only from threads that serve requests!" );
279
- }
280
- afterInvocationActions .get ().add (c );
285
+ if (Request .current () == null ) {
286
+ throw new IllegalStateException ("After request actions can be added only from threads that serve requests!" );
287
+ }
288
+ afterInvocationActions .get ().add (c );
281
289
}
282
290
}
0 commit comments