Skip to content

Commit 1b5a433

Browse files
committed
added ScheduledTaskRegistrar etc
1 parent 47fc8be commit 1b5a433

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

org.springframework.context/src/main/java/org/springframework/scheduling/support/DelegatingExceptionProofRunnable.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,4 +82,10 @@ public void run() {
8282
}
8383
}
8484

85+
86+
@Override
87+
public String toString() {
88+
return "DelegatingExceptionProofRunnable for " + this.delegate;
89+
}
90+
8591
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scheduling.support;
18+
19+
import java.util.LinkedHashSet;
20+
import java.util.Map;
21+
import java.util.Set;
22+
import java.util.concurrent.Executors;
23+
import java.util.concurrent.ScheduledExecutorService;
24+
import java.util.concurrent.ScheduledFuture;
25+
26+
import org.springframework.beans.factory.DisposableBean;
27+
import org.springframework.beans.factory.InitializingBean;
28+
import org.springframework.scheduling.TaskScheduler;
29+
import org.springframework.scheduling.Trigger;
30+
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
31+
import org.springframework.util.Assert;
32+
33+
/**
34+
* Helper bean for registering tasks with a {@link TaskScheduler},
35+
* typically using cron expressions.
36+
*
37+
* @author Juergen Hoeller
38+
* @since 3.0
39+
*/
40+
public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean {
41+
42+
private TaskScheduler taskScheduler;
43+
44+
private Map<Runnable, Trigger> triggerTasks;
45+
46+
private Map<Runnable, String> cronTasks;
47+
48+
private Map<Runnable, Long> fixedRateTasks;
49+
50+
private Map<Runnable, Long> fixedDelayTasks;
51+
52+
private final Set<ScheduledFuture> scheduledFutures = new LinkedHashSet<ScheduledFuture>();
53+
54+
55+
public void setTaskScheduler(TaskScheduler taskScheduler) {
56+
Assert.notNull(taskScheduler, "TaskScheduler must not be null");
57+
this.taskScheduler = taskScheduler;
58+
}
59+
60+
public void setScheduler(Object scheduler) {
61+
Assert.notNull(scheduler, "Scheduler object must not be null");
62+
if (scheduler instanceof TaskScheduler) {
63+
this.taskScheduler = (TaskScheduler) scheduler;
64+
}
65+
else if (scheduler instanceof ScheduledExecutorService) {
66+
this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler));
67+
}
68+
else {
69+
throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass());
70+
}
71+
}
72+
73+
public void setTriggerTasks(Map<Runnable, Trigger> triggerTasks) {
74+
this.triggerTasks = triggerTasks;
75+
}
76+
77+
public void setCronTasks(Map<Runnable, String> cronTasks) {
78+
this.cronTasks = cronTasks;
79+
}
80+
81+
public void setFixedRateTasks(Map<Runnable, Long> fixedRateTasks) {
82+
this.fixedRateTasks = fixedRateTasks;
83+
}
84+
85+
public void setFixedDelayTasks(Map<Runnable, Long> fixedDelayTasks) {
86+
this.fixedDelayTasks = fixedDelayTasks;
87+
}
88+
89+
90+
public void afterPropertiesSet() {
91+
if (this.taskScheduler == null) {
92+
this.taskScheduler = new ConcurrentTaskScheduler(Executors.newSingleThreadScheduledExecutor());
93+
}
94+
if (this.triggerTasks != null) {
95+
for (Map.Entry<Runnable, Trigger> entry : this.triggerTasks.entrySet()) {
96+
this.scheduledFutures.add(this.taskScheduler.schedule(entry.getKey(), entry.getValue()));
97+
}
98+
}
99+
if (this.cronTasks != null) {
100+
for (Map.Entry<Runnable, String> entry : cronTasks.entrySet()) {
101+
this.scheduledFutures.add(this.taskScheduler.schedule(entry.getKey(), new CronTrigger(entry.getValue())));
102+
}
103+
}
104+
if (this.fixedRateTasks != null) {
105+
for (Map.Entry<Runnable, Long> entry : this.fixedRateTasks.entrySet()) {
106+
this.scheduledFutures.add(this.taskScheduler.scheduleAtFixedRate(entry.getKey(), entry.getValue()));
107+
}
108+
}
109+
if (this.fixedDelayTasks != null) {
110+
for (Map.Entry<Runnable, Long> entry : this.fixedDelayTasks.entrySet()) {
111+
this.scheduledFutures.add(this.taskScheduler.scheduleWithFixedDelay(entry.getKey(), entry.getValue()));
112+
}
113+
}
114+
}
115+
116+
117+
public void destroy() {
118+
for (ScheduledFuture future : this.scheduledFutures) {
119+
future.cancel(true);
120+
}
121+
}
122+
123+
}

org.springframework.context/src/test/java/org/springframework/core/task/NoOpRunnable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ public class NoOpRunnable implements Runnable {
2525

2626
public void run() {
2727
// explicit no-op
28+
System.out.println("Running");
2829
}
2930

30-
}
31+
}

0 commit comments

Comments
 (0)