Skip to content
This repository
tree: 7492206fca
Fetching contributors…

Cannot retrieve contributors at this time

executable file 70 lines (59 sloc) 2.015 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/**
* http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html
*/
component extends="ExecutorService" accessors="true" output="false"{

property name="scheduledExecutor";
property name="storedTasks" type="struct";

public function init( String serviceName, maxConcurrent=0, objectFactory="#createObject('component', 'ObjectFactory').init()#" ){

super.init( serviceName, maxConcurrent, -1, objectFactory );

storedTasks = {};
return this;
}

public function start(){
variables.scheduledExecutor = objectFactory.createScheduledThreadPoolExecutor( maxConcurrent );

//store the executor for sane destructability
storeExecutor( "scheduledExecutor", variables.scheduledExecutor );
status = "started";
return this;
}

public function scheduleAtFixedRate( id, task, initialDelay, period, timeUnit="#objectFactory.SECONDS#" ){
cancelTask( id );
var future = scheduledExecutor.scheduleAtFixedRate(
objectFactory.createRunnableProxy( task ),
initialDelay,
period,
timeUnit
);
storeTask( id, task, future );
return future;
}

public function scheduleWithFixedDelay( id, task, initialDelay, delay, timeUnit="#objectFactory.SECONDS#" ){
cancelTask( id );
var future = scheduledExecutor.scheduleWithFixedDelay(
objectFactory.createRunnableProxy( task ),
initialDelay,
delay,
timeUnit
);
storeTask( id, task, future );
return future;
}

package function storeTask( id, task, future ){
storedTasks[ id ] = { task = task, future = future };
return this;
}

/**
* Returns a struct with keys 'task' and 'future'. The 'task' is the original object submitted to the executor.
The 'future' is the <ScheduledFuture> object returned when submitting the task
*/
public function cancelTask( id ){
if( structKeyExists( storedTasks, id ) ){
var task = storedTasks[ id ];
var future = task.future;
future.cancel( true );
scheduledExecutor.purge();
structDelete( storedTasks, id );
return task;
}
}

}
Something went wrong with that request. Please try again.