From 151cf74a10358503c789548a01face56c6ebcb4b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 5 May 2009 18:40:43 +0000 Subject: [PATCH] added ScheduledTaskRegistrar etc git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1105 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../DelegatingExceptionProofRunnable.java | 8 +- .../support/ScheduledTaskRegistrar.java | 123 ++++++++++++++++++ .../core/task/NoOpRunnable.java | 5 +- 3 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 org.springframework.context/src/main/java/org/springframework/scheduling/support/ScheduledTaskRegistrar.java diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/support/DelegatingExceptionProofRunnable.java b/org.springframework.context/src/main/java/org/springframework/scheduling/support/DelegatingExceptionProofRunnable.java index 7117890bfc1..79ad1734748 100644 --- a/org.springframework.context/src/main/java/org/springframework/scheduling/support/DelegatingExceptionProofRunnable.java +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/support/DelegatingExceptionProofRunnable.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -82,4 +82,10 @@ public class DelegatingExceptionProofRunnable implements Runnable { } } + + @Override + public String toString() { + return "DelegatingExceptionProofRunnable for " + this.delegate; + } + } diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/support/ScheduledTaskRegistrar.java b/org.springframework.context/src/main/java/org/springframework/scheduling/support/ScheduledTaskRegistrar.java new file mode 100644 index 00000000000..7ee89026532 --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/support/ScheduledTaskRegistrar.java @@ -0,0 +1,123 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.scheduling.support; + +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.scheduling.Trigger; +import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler; +import org.springframework.util.Assert; + +/** + * Helper bean for registering tasks with a {@link TaskScheduler}, + * typically using cron expressions. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean { + + private TaskScheduler taskScheduler; + + private Map triggerTasks; + + private Map cronTasks; + + private Map fixedRateTasks; + + private Map fixedDelayTasks; + + private final Set scheduledFutures = new LinkedHashSet(); + + + public void setTaskScheduler(TaskScheduler taskScheduler) { + Assert.notNull(taskScheduler, "TaskScheduler must not be null"); + this.taskScheduler = taskScheduler; + } + + public void setScheduler(Object scheduler) { + Assert.notNull(scheduler, "Scheduler object must not be null"); + if (scheduler instanceof TaskScheduler) { + this.taskScheduler = (TaskScheduler) scheduler; + } + else if (scheduler instanceof ScheduledExecutorService) { + this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler)); + } + else { + throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass()); + } + } + + public void setTriggerTasks(Map triggerTasks) { + this.triggerTasks = triggerTasks; + } + + public void setCronTasks(Map cronTasks) { + this.cronTasks = cronTasks; + } + + public void setFixedRateTasks(Map fixedRateTasks) { + this.fixedRateTasks = fixedRateTasks; + } + + public void setFixedDelayTasks(Map fixedDelayTasks) { + this.fixedDelayTasks = fixedDelayTasks; + } + + + public void afterPropertiesSet() { + if (this.taskScheduler == null) { + this.taskScheduler = new ConcurrentTaskScheduler(Executors.newSingleThreadScheduledExecutor()); + } + if (this.triggerTasks != null) { + for (Map.Entry entry : this.triggerTasks.entrySet()) { + this.scheduledFutures.add(this.taskScheduler.schedule(entry.getKey(), entry.getValue())); + } + } + if (this.cronTasks != null) { + for (Map.Entry entry : cronTasks.entrySet()) { + this.scheduledFutures.add(this.taskScheduler.schedule(entry.getKey(), new CronTrigger(entry.getValue()))); + } + } + if (this.fixedRateTasks != null) { + for (Map.Entry entry : this.fixedRateTasks.entrySet()) { + this.scheduledFutures.add(this.taskScheduler.scheduleAtFixedRate(entry.getKey(), entry.getValue())); + } + } + if (this.fixedDelayTasks != null) { + for (Map.Entry entry : this.fixedDelayTasks.entrySet()) { + this.scheduledFutures.add(this.taskScheduler.scheduleWithFixedDelay(entry.getKey(), entry.getValue())); + } + } + } + + + public void destroy() { + for (ScheduledFuture future : this.scheduledFutures) { + future.cancel(true); + } + } + +} diff --git a/org.springframework.context/src/test/java/org/springframework/core/task/NoOpRunnable.java b/org.springframework.context/src/test/java/org/springframework/core/task/NoOpRunnable.java index 56f4923c371..acec31ff7c1 100644 --- a/org.springframework.context/src/test/java/org/springframework/core/task/NoOpRunnable.java +++ b/org.springframework.context/src/test/java/org/springframework/core/task/NoOpRunnable.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ public class NoOpRunnable implements Runnable { public void run() { // explicit no-op + System.out.println("Running"); } -} \ No newline at end of file +}