Skip to content

Commit ce541f8

Browse files
committed
* [#1758] doc(time): add some comment in the class description
* [#1758] refactor(time): split CronExpression in its own file
1 parent 479fc14 commit ce541f8

File tree

4 files changed

+1571
-1546
lines changed

4 files changed

+1571
-1546
lines changed

framework/src/play/jobs/JobsPlugin.java

+40-32
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
import java.text.SimpleDateFormat;
66
import java.util.ArrayList;
77
import java.util.Date;
8-
import java.util.List;
98
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;
1116

1217
import play.Logger;
1318
import play.Play;
1419
import play.PlayPlugin;
1520
import play.exceptions.PlayException;
1621
import play.exceptions.UnexpectedException;
22+
import play.libs.CronExpression;
1723
import play.libs.Expression;
1824
import play.libs.Time;
19-
import play.libs.Time.CronExpression;
2025
import play.mvc.Http.Request;
2126
import play.utils.Java;
2227
import play.utils.PThreadFactory;
@@ -46,16 +51,17 @@ public String getStatus() {
4651
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
4752
if (!scheduledJobs.isEmpty()) {
4853
out.println();
49-
out.println("Scheduled jobs ("+scheduledJobs.size()+"):");
54+
out.println("Scheduled jobs (" + scheduledJobs.size() + "):");
5055
out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
5156
for (Job job : scheduledJobs) {
5257
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))) {
5460
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)" : "") + ".");
5662
}
5763

58-
if( job.getClass().isAnnotationPresent(On.class)) {
64+
if (job.getClass().isAnnotationPresent(On.class)) {
5965

6066
String cron = job.getClass().getAnnotation(On.class).value();
6167
if (cron != null && cron.startsWith("cron.")) {
@@ -68,7 +74,7 @@ public String getStatus() {
6874
}
6975
if (job.lastRun > 0) {
7076
out.print(" (last run at " + df.format(new Date(job.lastRun)));
71-
if(job.wasError) {
77+
if (job.wasError) {
7278
out.print(" with error)");
7379
} else {
7480
out.print(")");
@@ -87,13 +93,13 @@ public String getStatus() {
8793

8894
for (int i = 0; i < q.length; i++) {
8995
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");
9198
}
9299
}
93100
return sw.toString();
94101
}
95102

96-
97103
@Override
98104
public void afterApplicationStart() {
99105
List<Class<?>> jobs = new ArrayList<Class<?>>();
@@ -106,16 +112,16 @@ public void afterApplicationStart() {
106112
for (final Class<?> clazz : jobs) {
107113
// @OnApplicationStart
108114
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
110116
OnApplicationStart appStartAnnotation = clazz.getAnnotation(OnApplicationStart.class);
111-
if( !appStartAnnotation.async()) {
112-
//run job sync
117+
if (!appStartAnnotation.async()) {
118+
// run job sync
113119
try {
114120
Job<?> job = ((Job<?>) clazz.newInstance());
115121
scheduledJobs.add(job);
116122
job.run();
117-
if(job.wasError) {
118-
if(job.lastException != null) {
123+
if (job.wasError) {
124+
if (job.lastException != null) {
119125
throw job.lastException;
120126
}
121127
throw new RuntimeException("@OnApplicationStart Job has failed");
@@ -131,13 +137,13 @@ public void afterApplicationStart() {
131137
throw new UnexpectedException(ex);
132138
}
133139
} else {
134-
//run job async
140+
// run job async
135141
try {
136142
Job<?> job = ((Job<?>) clazz.newInstance());
137143
scheduledJobs.add(job);
138-
//start running job now in the background
144+
// start running job now in the background
139145
@SuppressWarnings("unchecked")
140-
Callable<Job> callable = (Callable<Job>)job;
146+
Callable<Job> callable = (Callable<Job>) job;
141147
executor.submit(callable);
142148
} catch (InstantiationException ex) {
143149
throw new UnexpectedException("Cannot instanciate Job " + clazz.getName());
@@ -169,7 +175,7 @@ public void afterApplicationStart() {
169175
value = Play.configuration.getProperty(value);
170176
}
171177
value = Expression.evaluate(value, value).toString();
172-
if(!"never".equalsIgnoreCase(value)){
178+
if (!"never".equalsIgnoreCase(value)) {
173179
executor.scheduleWithFixedDelay(job, Time.parseDuration(value), Time.parseDuration(value), TimeUnit.SECONDS);
174180
}
175181
} catch (InstantiationException ex) {
@@ -206,17 +212,19 @@ public static <V> void scheduleForCRON(Job<V> job) {
206212
CronExpression cronExp = new CronExpression(cron);
207213
Date nextDate = cronExp.getNextValidTimeAfter(now);
208214
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());
210217
return;
211218
}
212219
if (nextDate.equals(job.nextPlannedExecution)) {
213220
// 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)
215223
Date nextInvalid = cronExp.getNextInvalidTimeAfter(nextDate);
216224
nextDate = cronExp.getNextValidTimeAfter(nextInvalid);
217225
}
218226
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);
220228
job.executor = executor;
221229
} catch (Exception ex) {
222230
throw new UnexpectedException(ex);
@@ -260,23 +268,23 @@ public void onApplicationStop() {
260268

261269
@Override
262270
public void beforeInvocation() {
263-
afterInvocationActions.set(new LinkedList<Callable<? extends Object>>());
271+
afterInvocationActions.set(new LinkedList<Callable<? extends Object>>());
264272
}
265273

266274
@Override
267275
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+
}
273281
}
274282

275283
// default visibility, because we want to use this only from Job.java
276284
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);
281289
}
282290
}

0 commit comments

Comments
 (0)