Title: [1055] trunk/servicemix-core/src/main/java/org/servicemix/components: Added simple scheduler using java.util.Timer.
- Revision
- 1055
- Author
- gastaldi
- Date
- 2005-12-09 10:45:58 -0500 (Fri, 09 Dec 2005)
Log Message
Added simple scheduler using java.util.Timer.
org.servicemix.components.util.PollingComponentSupport must be updated to use this scheduler, so task execution times may be customized
Added Paths
Diff
Added: trunk/servicemix-core/src/main/java/org/servicemix/components/varscheduler/ScheduleIterator.java (1054 => 1055)
--- trunk/servicemix-core/src/main/java/org/servicemix/components/varscheduler/ScheduleIterator.java 2005-12-09 06:55:28 UTC (rev 1054)
+++ trunk/servicemix-core/src/main/java/org/servicemix/components/varscheduler/ScheduleIterator.java 2005-12-09 15:45:58 UTC (rev 1055)
@@ -0,0 +1,35 @@
+/**
+ *
+ * Copyright 2005 Datasul B2B, Inc. http://www.neogrid.com.br
+ *
+ * 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.servicemix.components.varscheduler;
+
+import java.util.Date;
+
+/**
+ * Iterator for scheduling.
+ * @author george
+ *
+ */
+public interface ScheduleIterator {
+ /**
+ * Next execution date of associated task.
+ * Implementations should return null to cancel running task.
+ *
+ * @return next date of Execution
+ */
+ Date nextExecution();
+}
Added: trunk/servicemix-core/src/main/java/org/servicemix/components/varscheduler/Scheduler.java (1054 => 1055)
--- trunk/servicemix-core/src/main/java/org/servicemix/components/varscheduler/Scheduler.java 2005-12-09 06:55:28 UTC (rev 1054)
+++ trunk/servicemix-core/src/main/java/org/servicemix/components/varscheduler/Scheduler.java 2005-12-09 15:45:58 UTC (rev 1055)
@@ -0,0 +1,115 @@
+/**
+ *
+ * Copyright 2005 Datasul B2B, Inc. http://www.neogrid.com.br
+ *
+ * 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.servicemix.components.varscheduler;
+
+import java.util.Date;
+import java.util.Timer;
+import java.util.TimerTask;
+
+/**
+ * Class to handle scheduling tasks.
+ * <p>
+ * This class is thread-safe
+ * <p>
+ *
+ * @author George Gastaldi (gastaldi)
+ */
+public class Scheduler {
+
+ private Timer timer;
+
+ /**
+ * Creates a new Scheduler.
+ */
+ public Scheduler() {
+ this.timer = new Timer();
+ }
+
+ /**
+ * Creates a new Daemon Scheduler
+ * @param daemon Thread must be executed as "daemon".
+ */
+ public Scheduler(boolean daemon) {
+ this.timer = new Timer(daemon);
+ }
+
+ /**
+ * Cancels the scheduler task
+ */
+ public void cancel() {
+ timer.cancel();
+ }
+
+ /**
+ * Schedules a task
+ *
+ * @param task scheduled tasl
+ * @param iterator iterator for schedulingque descreve o agendamento
+ * @throws IllegalStateException if task scheduled or canceled
+ */
+ public void schedule(SchedulerTask task, ScheduleIterator iterator) {
+ Date time = iterator.nextExecution();
+ if (time == null) {
+ task.cancel();
+ } else {
+ synchronized (task.lock) {
+ if (task.state != SchedulerTask.VIRGIN) {
+ throw new IllegalStateException(
+ "Task already scheduled or cancelled");
+ }
+ task.state = SchedulerTask.SCHEDULED;
+ task.timerTask = new SchedulerTimerTask(task, iterator);
+ timer.schedule(task.timerTask, time);
+ }
+ }
+ }
+
+ private void reschedule(SchedulerTask task, ScheduleIterator iterator) {
+ Date time = iterator.nextExecution();
+ if (time == null) {
+ task.cancel();
+ } else {
+ synchronized (task.lock) {
+ if (task.state != SchedulerTask.CANCELLED) {
+ task.timerTask = new SchedulerTimerTask(task, iterator);
+ timer.schedule(task.timerTask, time);
+ }
+ }
+ }
+ }
+
+ /**
+ * Internal TimerTask instance
+ */
+ class SchedulerTimerTask extends TimerTask {
+ private SchedulerTask task;
+ private ScheduleIterator iterator;
+
+ public SchedulerTimerTask(SchedulerTask task,ScheduleIterator iterator){
+ this.task = task;
+ this.iterator = iterator;
+ }
+
+ public void run() {
+ task.run();
+ reschedule(task, iterator);
+ }
+ }
+
+}
+
Added: trunk/servicemix-core/src/main/java/org/servicemix/components/varscheduler/SchedulerTask.java (1054 => 1055)
--- trunk/servicemix-core/src/main/java/org/servicemix/components/varscheduler/SchedulerTask.java 2005-12-09 06:55:28 UTC (rev 1054)
+++ trunk/servicemix-core/src/main/java/org/servicemix/components/varscheduler/SchedulerTask.java 2005-12-09 15:45:58 UTC (rev 1055)
@@ -0,0 +1,64 @@
+/**
+ *
+ * Copyright 2005 Datasul B2B, Inc. http://www.neogrid.com.br
+ *
+ * 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.servicemix.components.varscheduler;
+
+import java.util.*;
+
+/**
+ * A task run by a [EMAIL PROTECTED] Scheduler}.
+ *
+ * @author George Gastaldi (gastaldi)
+ */
+public abstract class SchedulerTask implements Runnable {
+
+ static final int VIRGIN = 0;
+ static final int SCHEDULED = 1;
+ static final int CANCELLED = 2;
+
+ final Object lock = new Object();
+ int state = VIRGIN;
+ TimerTask timerTask;
+
+ protected SchedulerTask() {
+ }
+
+ public abstract void run();
+
+ /**
+ * Cancels task.
+ * @return true if task already scheduled
+ */
+ public boolean cancel() {
+ synchronized (lock) {
+ if (timerTask != null) {
+ timerTask.cancel();
+ }
+ boolean result = (state == SCHEDULED);
+ state = CANCELLED;
+ return result;
+ }
+ }
+
+ public long scheduledExecutionTime() {
+ synchronized (lock) {
+ return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
+ }
+ }
+
+}
+