public class QuickStart
{ public static void main(String[] args) throws Exception { Scheduler scheduler = new Scheduler(); (1) String cronExpression = "0/1 * * * * ?"; (2) Runnable task = new Runnable() (3) { @Override public void run() { System.out.println("" + new Date() + " hello world! "); } }; scheduler.schedule(task, cronExpression); (4) } } |
|
SchedulerOptions schedulerOptions = new SchedulerOptions(); (1) schedulerOptions.setMaxThreads(1); (2) .... Scheduler scheduler = new Scheduler(schedulerOptions); (3) |
| Property | Default Value | Description |
| threadFactory | Executors.defaultThreadFactory() | The thread factory used to create new threads |
| tickDuration | 100 ms | see netty documentation |
| ticksPerWheel | 512 | see netty documentation |
| maxThreads | Integer.MAX_VALUE | size of the thread pool for executing scheduled tasks. if the number of triggered tasks is higher then the number of threads the tasks are added to a queue and will be executed as soon as a thread is freed. setting this property to 1 will result in all tasks executed in sequence. configure this property to control the memory requirements of your application. |
| maxQueueSize | Integer.MAX_VALUE | maximal size of the tasks which have been triggered and are awaiting execution in the thread pool. note: configure this property to control the memory requirements of your application. However setting it too low may cause a RejectedExecutionException. |
| logger | new DefaultLogger("yacron4j-scheduler") | The logger used by the Scheduler. Similar to netty's logger concept it must implement a InternalLogger. The netty project includes adatpers to standard logging frameworks and slf4j. |
| debug | false | if true prints debug information using logger.info() |
| persistManager | DefaultPersistManager (no persistance) | see Persistence |
| TaskOptions taskOptions = new TaskOptions();
(1) taskOptions.setAllowOverlap(true); (2) ... ScheduledFuture task = scheduler.schedule(task, cronExpression, taskOptions) (3) |
| Property | Default Value | Description |
| allowOverlap | false | if true, allows multiple concurrent executions of the same task |
| ignoreOnOverlap | true | an overlap conflict resolution property if allowOverlap is false, will cause the execution of the task to be ignored if it is triggered while the task is executing if this property is false the trigger will execute immediatly on termination of the execution of the current task. note: if the trigger frequency is higher then the execution duration this may cause an overflow of the thread pool queue. |
| maxConcurrent | 1 | if allowOverlap is true, the maximal number of concurrent executions of the same task. Futher triggers will be ignored |
| dstConflictIgnore | false | if true, the scheduler will use the standard quartz method to determine the trigger times, which may result in NPE exeptions. if false the DST conflict resolution is activated |
| dstConflictResolution | DST_CONFLICT_IMMEDIATE | Example: within the time zone America/Vancouver the cron expression "0 15 2 8 3 ? 2015" will cause a conflict because it does not exist. At 2:00 the clocks are moved to 3:00. The property controls how the conflict is resolved: DST_CONFLICT_BACKWARD: will trigger at 01:15 DST_CONFLICT_IMMEDIATE: 03:00 DST_CONFLICT_FORWARD 03:15 |
| timeZone | TimeZone.getDefault() | sets the time zone for which the trigger calculations are done |
| exceptionResolution | EXCEPTION_IGNORE | in case the task throws an exception this property controls how this is handeled EXCEPTION_CANCEL: the task execution is cancelled EXCEPTION_RETRY: the task is immediatly reexecuted EXCEPTION_IGNORE: the exception is ignored and the the task is queued for execution on the next cron trigger Note: InterruptedException may cause the task to be cancelled |
| logger | new DefaultLogger("scheduler") | The logger used by the CronTask. Similar to netty's logger concept it must implement a InternalLogger. The netty project includes adatpers to standard logging frameworks and slf4j. |
| debug | false | if true prints debug information using logger.info() |
| persist | false | see Persistence |
SchedulerOptions schedulerOptions = new SchedulerOptions(); schedulerOptions.setPersistManager(new PrevaylerPersistentManager("data")); (1) Scheduler scheduler = new Scheduler(schedulerOptions); String cronExpression = "0/1 * * * * ?"; Runnable task = new Runnable() { public void run() { System.out.println("" + new Date() + " hello world! "); } }; TaskOptions taskOptions = new TaskOptions(); taskOptions.setPersist(true); (2) scheduler.schedule("testid", task, cronExpression, taskOptions); (3) |
final ScheduledFuture future = scheduler.schedule(callableTask, cronExpression); (1) new Thread(new Runnable() { @Override public void run() { Object result = null; try { while ((result = future.get()) != null) (2) { System.out.println("got: " + result); } } catch (Exception e) { e.printStackTrace(); } } }).start(); |
future = scheduler.schedule(task, cronExpression);
future.cancel() |
scheduler.shutdown(mayInterruptIfRunning)
|