[jboss-svn-commits] JBL Code SVN: r20220 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/time and 5 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu May 29 15:51:34 EDT 2008
Author: mark.proctor at jboss.com
Date: 2008-05-29 15:51:34 -0400 (Thu, 29 May 2008)
New Revision: 20220
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKTimerService.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/jdk/
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/jdk/JDKSchedulerTest.java
Removed:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKScheduler.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/scheduler/
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/jdk/JDKSchedulerTest.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/process/instance/timer/TimerManager.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/SchedulerFactory.java
Log:
JBRULES-1625 Create Scheduler API with JDK implementation
-Basic refactoring for for TimerService, instead of Scheduler
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/process/instance/timer/TimerManager.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/process/instance/timer/TimerManager.java 2008-05-29 17:25:37 UTC (rev 20219)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/process/instance/timer/TimerManager.java 2008-05-29 19:51:34 UTC (rev 20220)
@@ -81,14 +81,14 @@
public Date getNextFireTime() {
Date date = null;
- if ( period == 0 ) {
+ if ( delay == 0 ) {
if ( count == 0 ) {
- date = new Date( System.currentTimeMillis() + this.delay );
+ date = new Date( System.currentTimeMillis() + this.period );
}
} else if ( count == 0 ) {
- date = new Date( System.currentTimeMillis() + this.period + this.delay );
+ date = new Date( System.currentTimeMillis() + this.delay + this.period );
} else {
- date = new Date( System.currentTimeMillis() + this.delay );
+ date = new Date( System.currentTimeMillis() + this.period );
}
count++;
return date;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/SchedulerFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/SchedulerFactory.java 2008-05-29 17:25:37 UTC (rev 20219)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/SchedulerFactory.java 2008-05-29 19:51:34 UTC (rev 20220)
@@ -1,11 +1,11 @@
package org.drools.time;
-import org.drools.time.impl.JDKScheduler;
+import org.drools.time.impl.JDKTimerService;
public class SchedulerFactory {
- private static TimerService scheduler = new JDKScheduler();
+ private static TimerService timerService = new JDKTimerService();
public static TimerService getScheduler() {
- return scheduler;
+ return timerService;
}
}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKScheduler.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKScheduler.java 2008-05-29 17:25:37 UTC (rev 20219)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKScheduler.java 2008-05-29 19:51:34 UTC (rev 20220)
@@ -1,159 +0,0 @@
-/*
- * Copyright 2008 JBoss Inc
- *
- * 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.drools.time.impl;
-
-import java.util.Date;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
-
-import org.drools.time.Job;
-import org.drools.time.JobContext;
-import org.drools.time.JobHandle;
-import org.drools.time.TimerService;
-import org.drools.time.Trigger;
-
-/**
- * A default Scheduler implementation that uses the
- * JDK built-in ScheduledThreadPoolExecutor as the
- * scheduler and the system clock as the clock.
- *
- */
-public class JDKScheduler
- implements
- TimerService {
- private ScheduledThreadPoolExecutor scheduler;
-
- public JDKScheduler() {
- this( 3 );
- }
-
- public JDKScheduler(int size) {
- this.scheduler = new ScheduledThreadPoolExecutor( size );
- }
-
- /**
- * @inheritDoc
- */
- public long getCurrentTime() {
- return System.currentTimeMillis();
- }
-
- public JobHandle scheduleJob(Job job,
- JobContext ctx,
- Trigger trigger) {
- JDKJobHandle jobHandle = new JDKJobHandle();
-
- Date date = trigger.getNextFireTime();
-
- if ( date != null ) {
- JDKCallableJob callableJob = new JDKCallableJob( job,
- ctx,
- trigger,
- jobHandle,
- this.scheduler );
- ScheduledFuture future = schedule( date,
- callableJob,
- this.scheduler );
- jobHandle.setFuture( future );
-
- return jobHandle;
- } else {
- return null;
- }
- }
-
- public boolean removeJob(JobHandle jobHandle) {
- return this.scheduler.remove( (Runnable) ((JDKJobHandle) jobHandle).getFuture() );
- }
-
- private static ScheduledFuture schedule(Date date,
- JDKCallableJob callableJob,
- ScheduledThreadPoolExecutor scheduler) {
- long then = date.getTime();
- long now = System.currentTimeMillis();
- ScheduledFuture future = null;
- if ( then >= now ) {
- future = scheduler.schedule( callableJob,
- then - now,
- TimeUnit.MILLISECONDS );
- } else {
- future = scheduler.schedule( callableJob,
- 0,
- TimeUnit.MILLISECONDS );
- }
- return future;
- }
-
- public static class JDKCallableJob
- implements
- Callable {
- private Job job;
- private Trigger trigger;
- private JobContext ctx;
- private ScheduledThreadPoolExecutor scheduler;
- private JDKJobHandle handle;
-
- public JDKCallableJob(Job job,
- JobContext ctx,
- Trigger trigger,
- JDKJobHandle handle,
- ScheduledThreadPoolExecutor scheduler) {
- this.job = job;
- this.ctx = ctx;
- this.trigger = trigger;
- this.handle = handle;
- this.scheduler = scheduler;
- }
-
- public Object call() throws Exception {
- this.job.execute( this.ctx );
-
- // our triggers allow for flexible rescheduling
- Date date = this.trigger.getNextFireTime();
- if ( date != null ) {
- ScheduledFuture future = schedule( date,
- this,
- this.scheduler );
- this.handle.setFuture( future );
- }
-
- return null;
- }
- }
-
- public static class JDKJobHandle
- implements
- JobHandle {
- private ScheduledFuture future;
-
- public JDKJobHandle() {
-
- }
-
- public ScheduledFuture getFuture() {
- return future;
- }
-
- public void setFuture(ScheduledFuture future) {
- this.future = future;
- }
-
- }
-
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKTimerService.java (from rev 20217, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKScheduler.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKTimerService.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/time/impl/JDKTimerService.java 2008-05-29 19:51:34 UTC (rev 20220)
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2008 JBoss Inc
+ *
+ * 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.drools.time.impl;
+
+import java.util.Date;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.drools.time.Job;
+import org.drools.time.JobContext;
+import org.drools.time.JobHandle;
+import org.drools.time.TimerService;
+import org.drools.time.Trigger;
+
+/**
+ * A default Scheduler implementation that uses the
+ * JDK built-in ScheduledThreadPoolExecutor as the
+ * scheduler and the system clock as the clock.
+ *
+ */
+public class JDKTimerService
+ implements
+ TimerService {
+ private ScheduledThreadPoolExecutor scheduler;
+
+ public JDKTimerService() {
+ this( 3 );
+ }
+
+ public JDKTimerService(int size) {
+ this.scheduler = new ScheduledThreadPoolExecutor( size );
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public long getCurrentTime() {
+ return System.currentTimeMillis();
+ }
+
+ public JobHandle scheduleJob(Job job,
+ JobContext ctx,
+ Trigger trigger) {
+ JDKJobHandle jobHandle = new JDKJobHandle();
+
+ Date date = trigger.getNextFireTime();
+
+ if ( date != null ) {
+ JDKCallableJob callableJob = new JDKCallableJob( job,
+ ctx,
+ trigger,
+ jobHandle,
+ this.scheduler );
+ ScheduledFuture future = schedule( date,
+ callableJob,
+ this.scheduler );
+ jobHandle.setFuture( future );
+
+ return jobHandle;
+ } else {
+ return null;
+ }
+ }
+
+ public boolean removeJob(JobHandle jobHandle) {
+ return this.scheduler.remove( (Runnable) ((JDKJobHandle) jobHandle).getFuture() );
+ }
+
+ private static ScheduledFuture schedule(Date date,
+ JDKCallableJob callableJob,
+ ScheduledThreadPoolExecutor scheduler) {
+ long then = date.getTime();
+ long now = System.currentTimeMillis();
+ ScheduledFuture future = null;
+ if ( then >= now ) {
+ future = scheduler.schedule( callableJob,
+ then - now,
+ TimeUnit.MILLISECONDS );
+ } else {
+ future = scheduler.schedule( callableJob,
+ 0,
+ TimeUnit.MILLISECONDS );
+ }
+ return future;
+ }
+
+ public static class JDKCallableJob
+ implements
+ Callable {
+ private Job job;
+ private Trigger trigger;
+ private JobContext ctx;
+ private ScheduledThreadPoolExecutor scheduler;
+ private JDKJobHandle handle;
+
+ public JDKCallableJob(Job job,
+ JobContext ctx,
+ Trigger trigger,
+ JDKJobHandle handle,
+ ScheduledThreadPoolExecutor scheduler) {
+ this.job = job;
+ this.ctx = ctx;
+ this.trigger = trigger;
+ this.handle = handle;
+ this.scheduler = scheduler;
+ }
+
+ public Object call() throws Exception {
+ this.job.execute( this.ctx );
+
+ // our triggers allow for flexible rescheduling
+ Date date = this.trigger.getNextFireTime();
+ if ( date != null ) {
+ ScheduledFuture future = schedule( date,
+ this,
+ this.scheduler );
+ this.handle.setFuture( future );
+ }
+
+ return null;
+ }
+ }
+
+ public static class JDKJobHandle
+ implements
+ JobHandle {
+ private ScheduledFuture future;
+
+ public JDKJobHandle() {
+
+ }
+
+ public ScheduledFuture getFuture() {
+ return future;
+ }
+
+ public void setFuture(ScheduledFuture future) {
+ this.future = future;
+ }
+
+ }
+
+}
Copied: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/jdk (from rev 20217, labs/jbossrules/trunk/drools-core/src/test/java/org/drools/scheduler/impl/jdk)
Deleted: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/jdk/JDKSchedulerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/scheduler/impl/jdk/JDKSchedulerTest.java 2008-05-29 15:39:11 UTC (rev 20217)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/jdk/JDKSchedulerTest.java 2008-05-29 19:51:34 UTC (rev 20220)
@@ -1,131 +0,0 @@
-package org.drools.scheduler.impl.jdk;
-
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Stack;
-
-import junit.framework.TestCase;
-
-import org.drools.time.Job;
-import org.drools.time.JobContext;
-import org.drools.time.JobHandle;
-import org.drools.time.TimeServices;
-import org.drools.time.SchedulerFactory;
-import org.drools.time.Trigger;
-
-public class JDKSchedulerTest extends TestCase {
-
- public void test1() throws Exception {
- TimeServices scheduler = SchedulerFactory.getScheduler();
- Trigger trigger = new DelayedTrigger( 100 );
- HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", scheduler);
- scheduler.scheduleJob( new HelloWorldJob(), ctx, trigger);
- Thread.sleep( 500 );
- assertEquals( 1, ctx.getList().size() );
- }
-
- public void test2() throws Exception {
- TimeServices scheduler = SchedulerFactory.getScheduler();
- Trigger trigger = new DelayedTrigger( new long[] { 100, 100, 100} );
- HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", scheduler);
- scheduler.scheduleJob( new HelloWorldJob(), ctx, trigger);
- Thread.sleep( 500 );
-
- assertEquals( 3, ctx.getList().size() );
- }
-
-
- public void test3() throws Exception {
- TimeServices scheduler = SchedulerFactory.getScheduler();
- Trigger trigger = new DelayedTrigger( new long[] { 100, 100, 100, 100, 100 } );
- HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", scheduler);
- ctx.setLimit( 3 );
- scheduler.scheduleJob( new HelloWorldJob(), ctx, trigger);
- Thread.sleep( 1000 );
-
- assertEquals( 4, ctx.getList().size() );
- }
-
- public static class HelloWorldJob implements Job {
- public void execute(JobContext c) {
- HelloWorldJobContext ctx = (HelloWorldJobContext) c;
- int counter = ctx.increaseCounter();
- if ( counter > 3 ) {
- ctx.scheduler.removeJob( ctx.getJobHandle() );
- }
- ctx.getList().add( ((HelloWorldJobContext)ctx).getMessage() + " : " + counter);
- }
- }
-
- public static class HelloWorldJobContext implements JobContext {
- private String message;
- private TimeServices scheduler;
- private JobHandle jobHandle;
-
- private List list;
-
- private int counter;
- private int limit;
-
- public HelloWorldJobContext(String message, TimeServices scheduler) {
- this.message = message;
- this.scheduler = scheduler;
- this.list = new ArrayList();
- }
-
- public String getMessage() {
- return this.message;
- }
-
- public int increaseCounter() {
- return this.counter++;
- }
-
- public JobHandle getJobHandle() {
- return this.jobHandle;
- }
-
- public void setJobHandle(JobHandle jobHandle) {
- this.jobHandle = jobHandle;
- }
-
- public int getLimit() {
- return limit;
- }
-
- public void setLimit(int limit) {
- this.limit = limit;
- }
-
- public List getList() {
- return list;
- }
-
-
- }
-
- public static class DelayedTrigger implements Trigger {
- public Stack<Long> stack;
-
- public DelayedTrigger(long delay) {
- this( new long[] { delay } );
- }
-
- public DelayedTrigger(long[] delay) {
- this.stack = new Stack<Long>();
- for( int i = delay.length-1; i >= 0; i-- ) {
- this.stack.push( delay[i] );
- }
- }
-
- public Date getNextFireTime() {
- if ( !this.stack.isEmpty() ) {
- return new Date( this.stack.pop() );
- } else {
- return null;
- }
- }
-
- }
-}
Copied: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/jdk/JDKSchedulerTest.java (from rev 20219, labs/jbossrules/trunk/drools-core/src/test/java/org/drools/scheduler/impl/jdk/JDKSchedulerTest.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/jdk/JDKSchedulerTest.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/time/impl/jdk/JDKSchedulerTest.java 2008-05-29 19:51:34 UTC (rev 20220)
@@ -0,0 +1,131 @@
+package org.drools.time.impl.jdk;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Stack;
+
+import junit.framework.TestCase;
+
+import org.drools.time.Job;
+import org.drools.time.JobContext;
+import org.drools.time.JobHandle;
+import org.drools.time.TimerService;
+import org.drools.time.SchedulerFactory;
+import org.drools.time.Trigger;
+
+public class JDKSchedulerTest extends TestCase {
+
+ public void test1() throws Exception {
+ TimerService scheduler = SchedulerFactory.getScheduler();
+ Trigger trigger = new DelayedTrigger( 100 );
+ HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", scheduler);
+ scheduler.scheduleJob( new HelloWorldJob(), ctx, trigger);
+ Thread.sleep( 500 );
+ assertEquals( 1, ctx.getList().size() );
+ }
+
+ public void test2() throws Exception {
+ TimerService scheduler = SchedulerFactory.getScheduler();
+ Trigger trigger = new DelayedTrigger( new long[] { 100, 100, 100} );
+ HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", scheduler);
+ scheduler.scheduleJob( new HelloWorldJob(), ctx, trigger);
+ Thread.sleep( 500 );
+
+ assertEquals( 3, ctx.getList().size() );
+ }
+
+
+ public void test3() throws Exception {
+ TimerService scheduler = SchedulerFactory.getScheduler();
+ Trigger trigger = new DelayedTrigger( new long[] { 100, 100, 100, 100, 100 } );
+ HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", scheduler);
+ ctx.setLimit( 3 );
+ scheduler.scheduleJob( new HelloWorldJob(), ctx, trigger);
+ Thread.sleep( 1000 );
+
+ assertEquals( 4, ctx.getList().size() );
+ }
+
+ public static class HelloWorldJob implements Job {
+ public void execute(JobContext c) {
+ HelloWorldJobContext ctx = (HelloWorldJobContext) c;
+ int counter = ctx.increaseCounter();
+ if ( counter > 3 ) {
+ ctx.scheduler.removeJob( ctx.getJobHandle() );
+ }
+ ctx.getList().add( ((HelloWorldJobContext)ctx).getMessage() + " : " + counter);
+ }
+ }
+
+ public static class HelloWorldJobContext implements JobContext {
+ private String message;
+ private TimerService scheduler;
+ private JobHandle jobHandle;
+
+ private List list;
+
+ private int counter;
+ private int limit;
+
+ public HelloWorldJobContext(String message, TimerService scheduler) {
+ this.message = message;
+ this.scheduler = scheduler;
+ this.list = new ArrayList();
+ }
+
+ public String getMessage() {
+ return this.message;
+ }
+
+ public int increaseCounter() {
+ return this.counter++;
+ }
+
+ public JobHandle getJobHandle() {
+ return this.jobHandle;
+ }
+
+ public void setJobHandle(JobHandle jobHandle) {
+ this.jobHandle = jobHandle;
+ }
+
+ public int getLimit() {
+ return limit;
+ }
+
+ public void setLimit(int limit) {
+ this.limit = limit;
+ }
+
+ public List getList() {
+ return list;
+ }
+
+
+ }
+
+ public static class DelayedTrigger implements Trigger {
+ public Stack<Long> stack;
+
+ public DelayedTrigger(long delay) {
+ this( new long[] { delay } );
+ }
+
+ public DelayedTrigger(long[] delay) {
+ this.stack = new Stack<Long>();
+ for( int i = delay.length-1; i >= 0; i-- ) {
+ this.stack.push( delay[i] );
+ }
+ }
+
+ public Date getNextFireTime() {
+ if ( !this.stack.isEmpty() ) {
+ return new Date( this.stack.pop() );
+ } else {
+ return null;
+ }
+ }
+
+ }
+}
More information about the jboss-svn-commits
mailing list