ZEST-156 - removed Scheduler Library, due to serious bug that is nearly impossible to fix.
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/8915dfaa Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/8915dfaa Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/8915dfaa Branch: refs/heads/ValueSerializationCleaning Commit: 8915dfaab1b703eb868815f212d745d52ed8d1c0 Parents: ab96cf4 Author: Niclas Hedhman <[email protected]> Authored: Thu Jun 16 10:29:58 2016 +0800 Committer: Niclas Hedhman <[email protected]> Committed: Thu Jun 16 10:29:58 2016 +0800 ---------------------------------------------------------------------- libraries/scheduler/build.gradle | 38 --- libraries/scheduler/dev-status.xml | 38 --- libraries/scheduler/src/docs/scheduler.txt | 195 ------------- .../zest/library/scheduler/CronSchedule.java | 121 -------- .../zest/library/scheduler/OnceSchedule.java | 69 ----- .../apache/zest/library/scheduler/Schedule.java | 131 --------- .../zest/library/scheduler/ScheduleFactory.java | 41 --- .../zest/library/scheduler/Scheduler.java | 138 ---------- .../scheduler/SchedulerConfiguration.java | 47 ---- .../library/scheduler/SchedulerService.java | 33 --- .../library/scheduler/SchedulesHandler.java | 88 ------ .../org/apache/zest/library/scheduler/Task.java | 78 ------ .../scheduler/bootstrap/SchedulerAssembler.java | 118 -------- .../library/scheduler/bootstrap/package.html | 24 -- .../defaults/DefaultRejectionHandler.java | 39 --- .../defaults/DefaultScheduleFactoryMixin.java | 91 ------ .../defaults/DefaultThreadFactory.java | 57 ---- .../library/scheduler/defaults/package.html | 24 -- .../library/scheduler/internal/Execution.java | 276 ------------------- .../scheduler/internal/ScheduleTime.java | 62 ----- .../scheduler/internal/SchedulerMixin.java | 194 ------------- .../library/scheduler/internal/Schedules.java | 28 -- .../library/scheduler/internal/TaskRunner.java | 115 -------- .../library/scheduler/internal/package.html | 24 -- .../apache/zest/library/scheduler/package.html | 24 -- .../library/scheduler/schedule/package.html | 24 -- .../library/scheduler/timeline/Timeline.java | 75 ----- .../timeline/TimelineForScheduleConcern.java | 93 ------- .../scheduler/timeline/TimelineRecord.java | 81 ------ .../scheduler/timeline/TimelineRecordStep.java | 28 -- .../timeline/TimelineScheduleMixin.java | 141 ---------- .../timeline/TimelineScheduleState.java | 30 -- .../timeline/TimelineSchedulerServiceMixin.java | 110 -------- .../library/scheduler/timeline/package.html | 24 -- .../scheduler/AbstractSchedulerTest.java | 76 ----- .../zest/library/scheduler/Constants.java | 27 -- .../library/scheduler/CronScheduleTest.java | 78 ------ .../apache/zest/library/scheduler/FooTask.java | 77 ------ .../zest/library/scheduler/SchedulerTest.java | 206 -------------- .../scheduler/docsupport/SchedulerDocs.java | 99 ------- .../src/test/resources/logback-test.xml | 35 --- libraries/scheduler/test-repeatedly.sh | 35 --- manual/src/docs/userguide/libraries.txt | 4 - settings.gradle | 1 - 44 files changed, 3337 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/build.gradle ---------------------------------------------------------------------- diff --git a/libraries/scheduler/build.gradle b/libraries/scheduler/build.gradle deleted file mode 100644 index 520d01f..0000000 --- a/libraries/scheduler/build.gradle +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - * - * - */ - -description = "Apache Zest⢠Schduler Library for long term timing functionalities." - -jar { manifest { name = "Apache Zest⢠Library - Scheduler" }} - - -dependencies { - compile project( ":org.apache.zest.core:org.apache.zest.core.bootstrap" ) - compile project( ':org.apache.zest.libraries:org.apache.zest.library.constraints' ) - compile libraries.sked - compile libraries.slf4j_api - - testCompile project( ":org.apache.zest.core:org.apache.zest.core.testsupport" ) - testCompile project( ":org.apache.zest.extensions:org.apache.zest.extension.indexing-rdf" ) - testCompile libraries.awaitility - - testRuntime project( ":org.apache.zest.core:org.apache.zest.core.runtime" ) - testRuntime libraries.logback -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/dev-status.xml ---------------------------------------------------------------------- diff --git a/libraries/scheduler/dev-status.xml b/libraries/scheduler/dev-status.xml deleted file mode 100644 index 55032e6..0000000 --- a/libraries/scheduler/dev-status.xml +++ /dev/null @@ -1,38 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" ?> -<!-- - ~ Licensed to the Apache Software Foundation (ASF) under one - ~ or more contributor license agreements. See the NOTICE file - ~ distributed with this work for additional information - ~ regarding copyright ownership. The ASF licenses this file - ~ to you 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. - ~ - ~ - --> -<module xmlns="http://zest.apache.org/schemas/2008/dev-status/1" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://zest.apache.org/schemas/2008/dev-status/1 - http://zest.apache.org/schemas/2008/dev-status/1/dev-status.xsd"> - <status> - <!--none,early,beta,stable,mature--> - <codebase>early</codebase> - - <!-- none, brief, good, complete --> - <documentation>good</documentation> - - <!-- none, some, good, complete --> - <unittests>good</unittests> - </status> - <licenses> - <license>ALv2</license> - </licenses> -</module> http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/docs/scheduler.txt ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/docs/scheduler.txt b/libraries/scheduler/src/docs/scheduler.txt deleted file mode 100644 index a5ab18d..0000000 --- a/libraries/scheduler/src/docs/scheduler.txt +++ /dev/null @@ -1,195 +0,0 @@ -/////////////////////////////////////////////////////////////// - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. -/////////////////////////////////////////////////////////////// - -[[library-scheduler,Scheduler Library]] -= Scheduler = - -[devstatus] --------------- -source=libraries/scheduler/dev-status.xml --------------- - -The Scheduler library provides an easy way to schedule tasks either for one time execution, CRON expression intervals or a custom algorithm. - -An optional Timeline allows you to browse past and future task runs. - -include::../../build/docs/buildinfo/artifact.txt[] - -== Logging == - -The SLF4J Logger used by this library is named "org.apache.zest.library.scheduler". - -== Assembly == - -Use SchedulerAssembler to add the Scheduler service to your Application. This -Assembler provide a fluent api to programmatically configure configuration defaults and activate the -Timeline service assembly that allow browsing of past and future Task runs. - -Here is a full example: - -[snippet,java] ----- -source=libraries/scheduler/src/test/java/org/apache/zest/library/scheduler/SchedulerTest.java -tag=assembly ----- - -== Configuration == - -SchedulerConfiguration defines configuration properties details: - -[snippet,java] ----- -source=libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulerConfiguration.java -tag=configuration ----- - -== Writing Tasks == - -To write a schedulable Task, compose an Entity with the Task type to be able to schedule it. - -The Task contract is quite simple: - -[snippet,java] ----- -source=libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Task.java -tag=task ----- - -Tasks have a mandatory name property and an optional tags property. These properties get copied in -each TimelineRecord created when the Timeline feature is activated. - -The run() method of Tasks is wrapped in a UnitOfWork when called by the Scheduler. -Thanks to the UnitOfWork handling in Zest, you can split the work done in your Tasks in -several UnitOfWorks. See UnitOfWork strategy below. - -Here is a simple example: - -[snippet,java] ------------ -source=libraries/scheduler/src/test/java/org/apache/zest/library/scheduler/docsupport/SchedulerDocs.java -tag=1 ------------ - -== Scheduling Tasks == - -Tasks are scheduled using the Scheduler service. This creates a Schedule associated to -the Task that allows you to know if it is running, to change it's cron expression and set it's -durability. - -All Schedules are durable. In other words, it will survive an Application restart, and your application should -not schedule it again, as the Schedules are when the SchedulerService is activated after bootstrap. - -There are three ways to schedule a Task using the Scheduler service: once or with a cron -expression or providing your own Schedule instance. - -=== Scheduling once === - -This is the easiest way to run a background Task once after a given initial delay in seconds. - -[snippet,java] ------------ -source=libraries/scheduler/src/test/java/org/apache/zest/library/scheduler/docsupport/SchedulerDocs.java -tag=2 ------------ - -Since all Schedules are durable, this "once" can be far into the future, and still be executed if the application -has been restarted. - - -=== Scheduling using a cron expression === - -Cron expression parsing is based on the GNU crontab manpage that can be found here: -http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5 . - -The following extensions are used: - -- a mandatory field is added at the begining: seconds. -- a special string is added: @minutely -- a special character is added: ? to choose between dayOfMonth and dayOfWeek - -The ? special char has the same behavior as in the Quartz Scheduler expression. The wikipedia page -http://en.wikipedia.org/wiki/CRON_expression -explains Quartz Scheduler expression, not simple cron expressions. You'll find there about the ? special -char and maybe that some other extensions you would like to use are missing in this project. - -To sum up, cron expressions used here have a precision of one second. The following special strings can be used: - -- @minutely -- @hourly -- @midnight or @daily -- @weekly -- @monthly -- @annualy or @yearly - - -== Overrun == -If the Schedule is running when it is time to be executed, then the execution will be skipped. This means that -the Task must complete within its period, or executions will be skipped. The sideeffect of that is that this -reduces thread exhaustion. - -When the Task execution is skipped, the overrun() property on the Schedule is incremented by 1. - -== Durability == -All Schedules are durable and the Task must be an Entity Composite. It also means that Tasks should be schedule -once and not on each reboot. The SchedulerService will load all Schedules on activation. - -While the Task is running, the Schedule will be held in the UnitOfWork of the TaskRunner. This means that IF the -Schedule is updated, i.e. cancelled or directly manipulating Schedule properties, the UnitOfWork.complete() will fail. -And if the Task is executing within the same UnitOfWork, any changes made will not take place. - -== UnitOfWork strategy == -The TaskRunner creates a UnitOfWork and the Task is excuted within that UnitOfWork. This may be very convenient, but -as noted in Durability above, that UnitOfWork will fail if Schedule properties are updated while the Task is -running. To avoid that the Task's operations suffers from this, OR if the Task wants a Retry/DiscardOn strategy -different from the default one, then the Task can simply declare its own. such as; - -[snippet,java] ------------ -source=libraries/scheduler/src/test/java/org/apache/zest/library/scheduler/docsupport/SchedulerDocs.java -tag=strategy ------------ - -== Custom Schedules == -It is possible to implement Schedule directly. It must be declared as an EntityComposite in the assembly, and be -visible from the SchedulerService. No other considerations should be necessary. - -== Observing the Timeline == - -Timeline allow to browse in past and future Task runs. This feature is available only if you activate -the Timeline assembly in the SchedulerAssembler}, see above. - -Once activated, Task success and failures are recorded. Then, the Timeline -service allow to browse in past (recorded) and in anticipated (future) Task runs. - -Use the following in your code to get a Timeline Service injected: - -[snippet,java] ------------ -source=libraries/scheduler/src/test/java/org/apache/zest/library/scheduler/docsupport/SchedulerDocs.java -tag=timeline ------------ - -Here is the actual Timeline contract: - -[snippet,java] ----- -source=libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/timeline/Timeline.java -tag=timeline ----- - http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/CronSchedule.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/CronSchedule.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/CronSchedule.java deleted file mode 100644 index 5270da0..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/CronSchedule.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler; - -import java.lang.annotation.Retention; -import java.time.Instant; -import org.apache.zest.api.constraint.Constraint; -import org.apache.zest.api.constraint.ConstraintDeclaration; -import org.apache.zest.api.constraint.Constraints; -import org.apache.zest.api.mixin.Mixins; -import org.apache.zest.api.property.Immutable; -import org.apache.zest.api.property.Property; -import org.apache.zest.library.constraints.annotation.InstanceOf; -import org.apache.zest.library.constraints.annotation.NotEmpty; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -@Mixins( CronSchedule.CronScheduleMixin.class ) -public interface CronSchedule - extends Schedule -{ - /** - * The Cron expression indicating when the Schedule is to be run. - * The Schedule can NOT be changed once it is set. If this is needed, delete this Schedule and attach the Task - * to a new Schedule. - * - * @return The cron expression that will be used on {@link org.apache.zest.api.unitofwork.UnitOfWork} completion to compute next run - */ - @CronExpression - @Immutable - Property<String> cronExpression(); - - abstract class CronScheduleMixin - implements CronSchedule - { - private static final Logger LOGGER = LoggerFactory.getLogger( Schedule.class ); - - @Override - public void taskStarting() - { - } - - @Override - public void taskCompletedSuccessfully() - { - } - - @Override - public void taskCompletedWithException( Throwable ex ) - { - } - - @Override - public String presentationString() - { - return cronExpression().get(); - } - - @Override - public Instant nextRun( Instant from ) - { - Instant actualFrom = from; - Instant firstRun = start().get(); - if( firstRun.isAfter(from )) - { - actualFrom = firstRun; - } - // TODO:PM cron "next run" handling mismatch with the underlying cron library - Instant nextRun = Instant.ofEpochMilli( - createCron().firstRunAfter( actualFrom.plusSeconds( 1 ).toEpochMilli()) - ); - LOGGER.info( "CronSchedule::nextRun({}) is {}", from, firstRun ); - return nextRun; - } - - private org.codeartisans.sked.cron.CronSchedule createCron() - { - return new org.codeartisans.sked.cron.CronSchedule( cronExpression().get() ); - } - } - - @ConstraintDeclaration - @Retention( RUNTIME ) - @NotEmpty - @InstanceOf( String.class ) - @Constraints( CronExpressionConstraint.class ) - @interface CronExpression - { - } - - class CronExpressionConstraint - implements Constraint<CronExpression, String> - { - private static final long serialVersionUID = 1L; - - @Override - public boolean isValid( CronExpression annotation, String cronExpression ) - { - return org.codeartisans.sked.cron.CronSchedule.isExpressionValid( cronExpression ); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/OnceSchedule.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/OnceSchedule.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/OnceSchedule.java deleted file mode 100644 index 7421882..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/OnceSchedule.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler; - -import java.time.Instant; -import org.apache.zest.api.mixin.Mixins; - -@Mixins( OnceSchedule.OnceScheduleMixin.class ) -public interface OnceSchedule - extends Schedule -{ - abstract class OnceScheduleMixin - implements OnceSchedule - { - @Override - public void taskStarting() - { - } - - @Override - public void taskCompletedSuccessfully() - { - } - - @Override - public void taskCompletedWithException( Throwable ex ) - { - } - - @Override - public Instant nextRun( Instant from ) - { - if( done().get() ) - { - return Instant.MIN; - } - done().set( true ); - Instant runAt = start().get(); - if( runAt.isAfter( from ) ) - { - return runAt; - } - return from; - } - - @Override - public String presentationString() - { - return start().get().toString(); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Schedule.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Schedule.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Schedule.java deleted file mode 100644 index 6d7191d..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Schedule.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler; - -import java.time.Instant; -import java.time.ZonedDateTime; -import org.apache.zest.api.association.Association; -import org.apache.zest.api.common.UseDefaults; -import org.apache.zest.api.entity.EntityComposite; -import org.apache.zest.api.property.Immutable; -import org.apache.zest.api.property.Property; - -/** - * Represent the scheduling of a {@link Task}. - */ -public interface Schedule extends EntityComposite -{ - /** - * @return The Association to the Task to be executed when it is time. - */ - Association<Task> task(); - - /** The first run of this Schedule. - * - * @return The property containing the first time this Schedule will be run. - */ - @Immutable - Property<Instant> start(); - - /** Returns true if the Schedule has been cancelled. - * - * @return true if the Schedule has been cancelled. - */ - @UseDefaults - Property<Boolean> cancelled(); - - /** Returns true if the Schedule is currently running. - * - * @return true if the Schedule is currently running. - */ - @UseDefaults - Property<Boolean> running(); - - /** Returns the number of times the {@link Task} has been executed. - * <p> - * Each time the {@link Task#run} method completes, with or without an {@link Exception}, this - * counter is incremented by 1. - * </p> - * - * @return true the number of Exception that has occurred when running the {@link Task}. - */ - @UseDefaults - Property<Long> executionCounter(); - - /** Returns the number of Exception that has occurred when running the {@link Task}. - * <p> - * Each time the {@link Task#run} method throws a {@link RuntimeException}, this property - * is incremenented by 1, - * </p> - * - * @return true the number of Exception that has occurred when running the {@link Task}. - */ - @UseDefaults - Property<Long> exceptionCounter(); - - /** Returns true if the Schedule is done and will not be executed any more times. - * - * @return true if the Schedule is done and will not be executed any more times. - */ - @UseDefaults - Property<Boolean> done(); - - /** Returns the number of times the Schedule has been skipped, due to the Task was still running. - * - * @return the number of times the Schedule has been skipped, due to the Task was still running. - */ - @UseDefaults - Property<Long> overrun(); - - /** - * Called just before the {@link org.apache.zest.library.scheduler.Task#run()} method is called. - */ - void taskStarting(); - - /** - * Called directly after the {@link org.apache.zest.library.scheduler.Task#run()} method has been completed and - * returned from the method normally. - */ - void taskCompletedSuccessfully(); - - /** - * Called directly after the {@link org.apache.zest.library.scheduler.Task#run()} method has been completed but - * threw a RuntimeException. - * @param ex The execption that was thrown in the Task. If the thrown Exception was an - * {@link java.lang.reflect.UndeclaredThrowableException} then the underlying exception is passed here. - */ - void taskCompletedWithException( Throwable ex ); - - /** - * Compute the next time this schedule is to be run. - * - * @param from The starting time when to look for the next time it will run. - * - * @return The exact absolute time when this Schedule is to be run next time, or -1 if never - */ - Instant nextRun( Instant from ); - - /** - * Return a representation of the Schedule in a human understandable format. - * - * @return A String representing this schedule. - */ - String presentationString(); -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/ScheduleFactory.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/ScheduleFactory.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/ScheduleFactory.java deleted file mode 100644 index c5fdd81..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/ScheduleFactory.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler; - -import java.time.Instant; -import java.time.ZonedDateTime; -import org.apache.zest.api.concern.Concerns; -import org.apache.zest.api.unitofwork.concern.UnitOfWorkConcern; -import org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation; -import org.apache.zest.library.scheduler.defaults.DefaultScheduleFactoryMixin; -import org.apache.zest.api.mixin.Mixins; - -import static org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation.Propagation.MANDATORY; - -@Mixins( DefaultScheduleFactoryMixin.class ) -@Concerns( UnitOfWorkConcern.class ) -public interface ScheduleFactory -{ - @UnitOfWorkPropagation( MANDATORY) - Schedule newCronSchedule( Task task, String cronExpression, Instant start ); - - @UnitOfWorkPropagation( MANDATORY) - Schedule newOnceSchedule( Task task, Instant runAt ); -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Scheduler.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Scheduler.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Scheduler.java deleted file mode 100644 index ac081d6..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Scheduler.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler; - -import java.time.Instant; -import org.apache.zest.library.scheduler.internal.Schedules; -import org.apache.zest.api.concern.Concerns; -import org.apache.zest.api.structure.Application; -import org.apache.zest.api.unitofwork.concern.UnitOfWorkConcern; -import org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation; -import org.apache.zest.library.scheduler.bootstrap.SchedulerAssembler; -import org.apache.zest.library.scheduler.timeline.Timeline; - -import static org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation.Propagation.MANDATORY; - -/** - * Scheduler. - * <p> - * This is the only interface you should use in your application for scheduling tasks. - * </p> - * <p> - * See {@link SchedulerConfiguration} for configuration properties. - * </p> - * <p> - * See in {@link SchedulerAssembler} how to assemble a {@link Scheduler} and optional {@link Timeline}. - * </p> - * <p> - * By default, a {@link Schedule} is not durable. In other words, it do not survive an {@link Application} restart. - * </p> - * <p> - * All {@link Schedule}s are durable and stored in the visible {@link org.apache.zest.spi.entitystore.EntityStore} like - * any ordinary {@link org.apache.zest.api.entity.EntityComposite}. There is also a {@link Schedules} - * entity composite that has Associations to all active, completed and cancelled schedules. - * </p> - */ -@Concerns( UnitOfWorkConcern.class ) -public interface Scheduler -{ - /** - * Schedule a Task to be run after a given initial delay in seconds. - * - * @param task Task to be scheduled once - * @param initialSecondsDelay Initial delay the Task will be run after, in seconds - * - * @return The newly created Schedule - */ - @UnitOfWorkPropagation( MANDATORY ) - Schedule scheduleOnce( Task task, int initialSecondsDelay ); - - /** - * Schedule a Task to be run after a given initial delay in seconds. - * - * @param task Task to be scheduled once - * @param runAt The future point in time when the Schedule will be run. - * - * @return The newly created Schedule - */ - @UnitOfWorkPropagation( MANDATORY ) - Schedule scheduleOnce( Task task, Instant runAt ); - - /** - * Schedule a Task using a CronExpression. - * - * @param task Task to be scheduled once - * @param cronExpression CronExpression for creating the Schedule for the given Task - * - * @return The newly created Schedule - */ - @UnitOfWorkPropagation( MANDATORY ) - Schedule scheduleCron( Task task, @CronSchedule.CronExpression String cronExpression ); - - /** - * Schedule a Task using a CronExpression with a given initial delay in milliseconds. - * - * @param task Task to be scheduled once - * @param cronExpression CronExpression for creating the Schedule for the given Task - * @param initialDelay Initial delay the Schedule will be active after, in milliseconds - * - * @return The newly created Schedule - */ - @UnitOfWorkPropagation( MANDATORY ) - Schedule scheduleCron( Task task, @CronSchedule.CronExpression String cronExpression, long initialDelay ); - - /** - * Schedule a Task using a CronExpression starting at a given date. - * - * @param task Task to be scheduled once - * @param cronExpression CronExpression for creating the Schedule for the given Task - * @param start Date from which the Schedule will become active - * - * @return The newly created Schedule - */ - @UnitOfWorkPropagation( MANDATORY ) - Schedule scheduleCron( Task task, @CronSchedule.CronExpression String cronExpression, Instant start ); - - /** Schedules a custom Schedule. - * - * - * @param schedule The Schedule instance to be scheduled. - */ - @UnitOfWorkPropagation( MANDATORY ) - void scheduleCron( Schedule schedule ); - - /** Cancels a Schedule. - * Reads the Schedule from the EntityStore and calls {@link #cancelSchedule(Schedule)}. - * - * @param scheduleId The identity of the Schedule to be cancelled. - */ - @UnitOfWorkPropagation( MANDATORY ) - void cancelSchedule( String scheduleId ); - - /** Cancels the provided Schedule. - * - * Cancellation can be done before, while and after execution of the Schedule. If the execution - * is in progress, it will not be interrupted. - * - * @param schedule The schedule to be cancelled. - */ - @UnitOfWorkPropagation( MANDATORY ) - public void cancelSchedule( Schedule schedule ); -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulerConfiguration.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulerConfiguration.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulerConfiguration.java deleted file mode 100644 index 73fbb84..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulerConfiguration.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler; - -import org.apache.zest.api.common.Optional; -import org.apache.zest.api.common.UseDefaults; -import org.apache.zest.api.property.Property; - -/** - * Configuration for the {@link Scheduler}. - * - * Every property has a default value, you can use a {@link Scheduler} without providing any. - */ -public interface SchedulerConfiguration -{ -// START SNIPPET: configuration - /** - * @return Number of worker threads, optional and defaults to the number of available cores. - */ - @Optional @UseDefaults - Property<Integer> workersCount(); - - /** - * @return Size of the queue to use for holding tasks before they are run, optional and defaults to 10. - */ - @Optional @UseDefaults - Property<Integer> workQueueSize(); - -// END SNIPPET: configuration -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulerService.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulerService.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulerService.java deleted file mode 100644 index c71717c..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulerService.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler; - -import org.apache.zest.api.entity.Identity; -import org.apache.zest.api.mixin.Mixins; -import org.apache.zest.api.service.ServiceActivation; -import org.apache.zest.library.scheduler.defaults.DefaultRejectionHandler; -import org.apache.zest.library.scheduler.defaults.DefaultThreadFactory; -import org.apache.zest.library.scheduler.internal.SchedulerMixin; - -@Mixins( { SchedulerMixin.class, DefaultThreadFactory.class, DefaultRejectionHandler.class } ) -public interface SchedulerService - extends Scheduler, ServiceActivation, Identity -{ -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulesHandler.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulesHandler.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulesHandler.java deleted file mode 100644 index a9b4602..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/SchedulesHandler.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler; - -import org.apache.zest.api.entity.Identity; -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.injection.scope.This; -import org.apache.zest.api.mixin.Mixins; -import org.apache.zest.api.unitofwork.NoSuchEntityException; -import org.apache.zest.api.unitofwork.UnitOfWork; -import org.apache.zest.api.unitofwork.UnitOfWorkFactory; -import org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation; -import org.apache.zest.library.scheduler.internal.Schedules; - -@Mixins( SchedulesHandler.SchedulesHandlerMixin.class ) -public interface SchedulesHandler -{ - @UnitOfWorkPropagation( UnitOfWorkPropagation.Propagation.MANDATORY ) - Schedules getActiveSchedules(); - - @UnitOfWorkPropagation( UnitOfWorkPropagation.Propagation.MANDATORY ) - Schedules getCancelledSchedules(); - - class SchedulesHandlerMixin implements SchedulesHandler - { - @This - private Identity me; - - @Structure - private UnitOfWorkFactory module; - - @Override - public Schedules getActiveSchedules() - { - return getOrCreateSchedules( getActiveSchedulesIdentity() ); - } - - @Override - public Schedules getCancelledSchedules() - { - return getOrCreateSchedules( getCancelledSchedulesIdentity() ); - } - - public String getActiveSchedulesIdentity() - { - return "Schedules-Active:" + me.identity().get(); - } - - public String getCancelledSchedulesIdentity() - { - return "Schedules-Cancelled:" + me.identity().get(); - } - - private Schedules getOrCreateSchedules( String identity ) - { - UnitOfWork uow = module.currentUnitOfWork(); - Schedules schedules; - try - { - schedules = uow.get( Schedules.class, identity ); - } - catch( NoSuchEntityException e ) - { - // Create a new Schedules entity for keeping track of them all. - schedules = uow.newEntity( Schedules.class, identity ); - } - return schedules; - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Task.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Task.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Task.java deleted file mode 100644 index edd67ba..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/Task.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler; - -import java.util.List; -import org.apache.zest.api.common.UseDefaults; -import org.apache.zest.api.concern.Concerns; -import org.apache.zest.api.property.Property; -import org.apache.zest.api.unitofwork.UnitOfWork; -import org.apache.zest.api.unitofwork.concern.UnitOfWorkConcern; -import org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation; - -/** - * Compose an Entity using this type to be able to Schedule it. - *<p> - * A Task is associated from a {@link Schedule}, and upon time to execute - * the SchedulerService will dispatch a TaskRunner in a new thread, and establish a UnitOfWork (Usecase name of "Task Runner"). - *</p> - *<p> - * The {@code Task} type declares the {@link UnitOfWorkConcern} and therefor the {@code Task} implementation may - * declare the {@link UnitOfWorkPropagation} annotation with the - * {@link org.apache.zest.api.unitofwork.concern.UnitOfWorkPropagation.Propagation#REQUIRES_NEW} and a different - * {@link UnitOfWork} strategy, such as {@code Retries} and {@code DiscardOn}. - * - *</p> - * - * Here is a simple example: - * <pre><code> - * interface MyTask - * extends Task - * { - * Property<String customState(); - * Association<AnotherEntity> anotherEntity(); - * } - * - * class MyTaskMixin - * implements Runnable - * { - * @This MyTaskEntity me; - * - * public void run() - * { - * me.customState().set( me.anotherEntity().get().doSomeStuff( me.customState().get() ) ); - * } - * } - * </code></pre> - * - * Finaly, {@literal MyTask} must be assembled into an {@literal EntityComposite}. - */ -// START SNIPPET: task -@Concerns( UnitOfWorkConcern.class ) -public interface Task - extends Runnable -{ - Property<String> name(); - - @UseDefaults - Property<List<String>> tags(); - -} -// END SNIPPET: task http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/bootstrap/SchedulerAssembler.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/bootstrap/SchedulerAssembler.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/bootstrap/SchedulerAssembler.java deleted file mode 100644 index 7ff8d93..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/bootstrap/SchedulerAssembler.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler.bootstrap; - -import org.apache.zest.api.unitofwork.concern.UnitOfWorkConcern; -import org.apache.zest.bootstrap.Assemblers; -import org.apache.zest.bootstrap.AssemblyException; -import org.apache.zest.bootstrap.EntityDeclaration; -import org.apache.zest.bootstrap.ModuleAssembly; -import org.apache.zest.bootstrap.ServiceDeclaration; -import org.apache.zest.bootstrap.ValueDeclaration; -import org.apache.zest.library.scheduler.SchedulerConfiguration; -import org.apache.zest.library.scheduler.SchedulerService; -import org.apache.zest.library.scheduler.internal.TaskRunner; -import org.apache.zest.library.scheduler.ScheduleFactory; -import org.apache.zest.library.scheduler.internal.Schedules; -import org.apache.zest.library.scheduler.CronSchedule; -import org.apache.zest.library.scheduler.OnceSchedule; -import org.apache.zest.library.scheduler.timeline.Timeline; -import org.apache.zest.library.scheduler.timeline.TimelineForScheduleConcern; -import org.apache.zest.library.scheduler.timeline.TimelineRecord; -import org.apache.zest.library.scheduler.timeline.TimelineScheduleMixin; -import org.apache.zest.library.scheduler.timeline.TimelineSchedulerServiceMixin; - -/** - * Assembler for Scheduler. - * - * Use this Assembler to add the Scheduler service to your application. - * This Assembler provide a fluent api to programmatically configure configuration defaults and - * activate the Timeline service assembly that allow to browse in past and future Task runs. - * - * Here is a full example: - * <pre> - * new SchedulerAssembler(). - * visibleIn( Visibility.layer ). - * withConfig( configModuleAssembly, Visibility.application ). - * withTimeline(). - * assemble( module ); - * </pre> - */ -public class SchedulerAssembler - extends Assemblers.VisibilityConfig<SchedulerAssembler> -{ - - private static final int DEFAULT_WORKERS_COUNT = Runtime.getRuntime().availableProcessors() + 1; - private static final int DEFAULT_WORKQUEUE_SIZE = 10; - - private boolean timeline; - - /** - * Activate the assembly of Timeline related services. - * - * @return SchedulerAssembler - */ - public SchedulerAssembler withTimeline() - { - timeline = true; - return this; - } - - @Override - public void assemble( ModuleAssembly assembly ) - throws AssemblyException - { - assembly.services( ScheduleFactory.class ); - assembly.entities( Schedules.class ); - EntityDeclaration scheduleEntities = assembly.entities( CronSchedule.class, OnceSchedule.class ); - - ValueDeclaration scheduleValues = assembly.values( CronSchedule.class, OnceSchedule.class ); - - ServiceDeclaration schedulerDeclaration = assembly.services( SchedulerService.class ) - .visibleIn( visibility() ) - .instantiateOnStartup(); - - assembly.transients( Runnable.class ).withMixins( TaskRunner.class ).withConcerns( UnitOfWorkConcern.class ); - - if( timeline ) - { - scheduleEntities.withTypes( Timeline.class ) - .withMixins( TimelineScheduleMixin.class ) - .withConcerns( TimelineForScheduleConcern.class ); - - scheduleValues.withTypes( Timeline.class ) - .withMixins( TimelineScheduleMixin.class ) - .withConcerns( TimelineForScheduleConcern.class ); - - // Internal - assembly.values( TimelineRecord.class ); - schedulerDeclaration.withTypes( Timeline.class ).withMixins( TimelineSchedulerServiceMixin.class ); - } - - if( hasConfig() ) - { - configModule().entities( SchedulerConfiguration.class ) - .visibleIn( configVisibility() ); - SchedulerConfiguration defaults = assembly.forMixin( SchedulerConfiguration.class ).declareDefaults(); - defaults.workersCount().set( DEFAULT_WORKERS_COUNT ); - defaults.workQueueSize().set( DEFAULT_WORKQUEUE_SIZE ); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/bootstrap/package.html ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/bootstrap/package.html b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/bootstrap/package.html deleted file mode 100644 index 6466780..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/bootstrap/package.html +++ /dev/null @@ -1,24 +0,0 @@ -<!-- - ~ Licensed to the Apache Software Foundation (ASF) under one - ~ or more contributor license agreements. See the NOTICE file - ~ distributed with this work for additional information - ~ regarding copyright ownership. The ASF licenses this file - ~ to you 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. - ~ - ~ - --> -<html> - <body> - <h2>Scheduler Assembly.</h2> - </body> -</html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultRejectionHandler.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultRejectionHandler.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultRejectionHandler.java deleted file mode 100644 index 2b5b2e3..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultRejectionHandler.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler.defaults; - -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ThreadPoolExecutor; -import org.apache.zest.library.scheduler.SchedulerService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DefaultRejectionHandler - implements RejectedExecutionHandler -{ - private static final Logger LOGGER = LoggerFactory.getLogger( SchedulerService.class ); - - @Override - public void rejectedExecution( Runnable r, ThreadPoolExecutor executor ) - { - LOGGER.error( "Runnable [" + r + "] was rejected by executor [" + executor + "]" ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultScheduleFactoryMixin.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultScheduleFactoryMixin.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultScheduleFactoryMixin.java deleted file mode 100644 index 2d21e27..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultScheduleFactoryMixin.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler.defaults; - -import java.time.Instant; -import org.apache.zest.api.entity.EntityBuilder; -import org.apache.zest.api.injection.scope.Service; -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.unitofwork.UnitOfWork; -import org.apache.zest.api.unitofwork.UnitOfWorkFactory; -import org.apache.zest.library.scheduler.CronSchedule; -import org.apache.zest.library.scheduler.OnceSchedule; -import org.apache.zest.library.scheduler.Schedule; -import org.apache.zest.library.scheduler.ScheduleFactory; -import org.apache.zest.library.scheduler.SchedulerService; -import org.apache.zest.library.scheduler.Task; -import org.apache.zest.spi.uuid.UuidIdentityGeneratorService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DefaultScheduleFactoryMixin - implements ScheduleFactory -{ - private static final Logger logger = LoggerFactory.getLogger( ScheduleFactory.class ); - - @Structure - private UnitOfWorkFactory uowf; - - @Service - private SchedulerService scheduler; - - @Service - private UuidIdentityGeneratorService uuid; - - @Override - public CronSchedule newCronSchedule( Task task, String cronExpression, Instant start ) - { - return newPersistentCronSchedule( task, cronExpression, start ); - } - - @Override - public Schedule newOnceSchedule( Task task, Instant runAt ) - { - return newPersistentOnceSchedule( task, runAt ); - } - - private CronSchedule newPersistentCronSchedule( Task task, String cronExpression, Instant start ) - { - UnitOfWork uow = uowf.currentUnitOfWork(); - EntityBuilder<CronSchedule> builder = uow.newEntityBuilder( CronSchedule.class ); - CronSchedule instance = builder.instance(); - instance.task().set( task ); - instance.start().set( start ); - instance.identity().set( uuid.generate( CronSchedule.class ) ); - instance.cronExpression().set( cronExpression ); - CronSchedule schedule = builder.newInstance(); - logger.info( "Schedule {} created: {}", schedule.presentationString(), schedule.identity().get() ); - return schedule; - } - - private Schedule newPersistentOnceSchedule( Task task, Instant runAt ) - { - UnitOfWork uow = uowf.currentUnitOfWork(); - EntityBuilder<OnceSchedule> builder = uow.newEntityBuilder( OnceSchedule.class ); - OnceSchedule builderInstance = builder.instance(); - builderInstance.task().set( task ); - builderInstance.start().set( runAt ); - builderInstance.identity().set( uuid.generate( OnceSchedule.class ) ); - OnceSchedule schedule = builder.newInstance(); - logger.info( "Schedule {} created: {}", schedule.presentationString(), schedule.identity().get() ); - return schedule; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultThreadFactory.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultThreadFactory.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultThreadFactory.java deleted file mode 100644 index 85d137c..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/DefaultThreadFactory.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler.defaults; - -import java.util.concurrent.atomic.AtomicInteger; -import org.apache.zest.api.injection.scope.This; -import org.apache.zest.library.scheduler.internal.Execution; -import org.apache.zest.library.scheduler.SchedulerService; - -public class DefaultThreadFactory - implements java.util.concurrent.ThreadFactory -{ - private static final AtomicInteger POOL_NUMBER = new AtomicInteger( 1 ); - private final ThreadGroup group; - private final AtomicInteger threadNumber = new AtomicInteger( 1 ); - private final String namePrefix; - - protected DefaultThreadFactory( @This SchedulerService me ) - { - SecurityManager sm = System.getSecurityManager(); - group = ( sm != null ) ? sm.getThreadGroup() : Execution.ExecutionMixin.TG; - namePrefix = me.identity().get() + "-P" + POOL_NUMBER.getAndIncrement() + "W"; - } - - @Override - public Thread newThread( Runnable runnable ) - { - Thread thread = new Thread( group, runnable, namePrefix + threadNumber.getAndIncrement(), 0 ); - if( thread.isDaemon() ) - { - thread.setDaemon( false ); - } - if( thread.getPriority() != Thread.NORM_PRIORITY ) - { - thread.setPriority( Thread.NORM_PRIORITY ); - } - return thread; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/package.html ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/package.html b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/package.html deleted file mode 100644 index 86177ff..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/defaults/package.html +++ /dev/null @@ -1,24 +0,0 @@ -<!-- - ~ Licensed to the Apache Software Foundation (ASF) under one - ~ or more contributor license agreements. See the NOTICE file - ~ distributed with this work for additional information - ~ regarding copyright ownership. The ASF licenses this file - ~ to you 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. - ~ - ~ - --> -<html> - <body> - <h2>Scheduler Defaults.</h2> - </body> -</html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/Execution.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/Execution.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/Execution.java deleted file mode 100644 index 6ca01af..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/Execution.java +++ /dev/null @@ -1,276 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler.internal; - -import java.time.Duration; -import java.time.Instant; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; -import org.apache.zest.api.composite.TransientBuilderFactory; -import org.apache.zest.api.configuration.Configuration; -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.injection.scope.This; -import org.apache.zest.api.mixin.Mixins; -import org.apache.zest.api.unitofwork.NoSuchEntityException; -import org.apache.zest.api.unitofwork.UnitOfWork; -import org.apache.zest.api.unitofwork.UnitOfWorkFactory; -import org.apache.zest.library.scheduler.Schedule; -import org.apache.zest.library.scheduler.Scheduler; -import org.apache.zest.library.scheduler.SchedulerConfiguration; - -/** - * This composite handles the Execution of Schedules. - * - * The composite is internal and should never be used by clients. - */ -@Mixins( Execution.ExecutionMixin.class ) -public interface Execution -{ - void dispatchForExecution( Schedule schedule ); - - void start() - throws Exception; - - void stop() - throws Exception; - - void updateNextTime( ScheduleTime schedule ); // This method is public, only because the UnitOfWorkConcern is wanted. - - class ExecutionMixin - implements Execution, Runnable - { - public static final ThreadGroup TG = new ThreadGroup( "Zest Scheduling" ); - - private final Object lock = new Object(); - - @Structure - private UnitOfWorkFactory uowf; - - @Structure - private TransientBuilderFactory tbf; - - @This - private Scheduler scheduler; - - @This - private Configuration<SchedulerConfiguration> config; - - @This - private ThreadFactory threadFactory; - - @This - private RejectedExecutionHandler rejectionHandler; - - private final SortedSet<ScheduleTime> timingQueue = new TreeSet<>(); - private volatile boolean running; - private ThreadPoolExecutor taskExecutor; - private volatile Thread scheduleThread; - - @Override - public void run() - { - running = true; - while( running ) - { - try - { - ScheduleTime scheduleTime = timing(); - if( scheduleTime != null ) - { - waitFor( scheduleTime ); - - if( isTime( scheduleTime ) ) // We might have been awakened to reschedule - { - updateNextTime( scheduleTime ); - } - } - else - { - waitFor( 100 ); - } - } - catch( Throwable e ) - { - e.printStackTrace(); - } - } - } - - private ScheduleTime timing() - { - synchronized( lock ) - { - if( timingQueue.size() == 0 ) - { - return null; - } - return timingQueue.first(); - } - } - - private boolean isTime( ScheduleTime scheduleTime ) - { - return scheduleTime.nextTime().isBefore( Instant.now() ); - } - - private void waitFor( ScheduleTime scheduleTime ) - throws InterruptedException - { - Duration waitingTime = Duration.between( Instant.now(), scheduleTime.nextTime() ); - waitFor( waitingTime.toMillis() ); - } - - private void waitFor( long waitingTime ) - { - if( waitingTime > 0 ) - { - synchronized( lock ) - { - try - { - lock.wait( waitingTime ); - } - catch( InterruptedException e ) - { - // should be ignored. - } - } - } - } - - @Override - public void updateNextTime( ScheduleTime oldScheduleTime ) - { - try (UnitOfWork uow = uowf.newUnitOfWork()) // This will discard() the UoW when block is exited. We are only doing reads, so fine. - { - submitTaskForExecution( oldScheduleTime ); - Schedule schedule = uow.get( Schedule.class, oldScheduleTime.scheduleIdentity() ); - Instant nextTime = schedule.nextRun( Instant.now() ); - if( nextTime.isAfter( Instant.MIN ) ) - { - ScheduleTime newScheduleTime = new ScheduleTime( oldScheduleTime.scheduleIdentity(), nextTime ); - synchronized( lock ) - { - // Re-add to the Timing Queue, to re-position the sorting. - timingQueue.remove( oldScheduleTime ); - timingQueue.add( newScheduleTime ); - } - } - else - { - synchronized( lock ) - { - timingQueue.remove( oldScheduleTime ); - } - } - } - catch( NoSuchEntityException e ) - { - e.printStackTrace(); -// scheduler.cancelSchedule( oldScheduleTime.scheduleIdentity() ); - } - } - - private void submitTaskForExecution( ScheduleTime scheduleTime ) - { - Runnable taskRunner = tbf.newTransient( Runnable.class, scheduleTime ); - this.taskExecutor.submit( taskRunner ); - } - - @Override - public void dispatchForExecution( Schedule schedule ) - { - Instant nextRun = schedule.nextRun( Instant.now() ); - if( nextRun.equals( Instant.MIN ) ) - { - return; - } - synchronized( lock ) - { - timingQueue.add( new ScheduleTime( schedule.identity().get(), nextRun ) ); - lock.notifyAll(); - } - } - - @Override - public void start() - throws Exception - { - SchedulerConfiguration configuration = config.get(); - Integer workersCount = configuration.workersCount().get(); - Integer workQueueSize = configuration.workQueueSize().get(); - createThreadPoolExecutor( workersCount, workQueueSize ); - taskExecutor.prestartAllCoreThreads(); - - SecurityManager sm = System.getSecurityManager(); - ThreadGroup threadGroup = sm != null ? sm.getThreadGroup() : TG; - scheduleThread = new Thread( threadGroup, this, "Scheduler" ); - scheduleThread.start(); - } - - private void createThreadPoolExecutor( Integer workersCount, Integer workQueueSize ) - { - int corePoolSize = 2; - if( workersCount > 4 ) - { - corePoolSize = workersCount / 4 + 1; - } - if( corePoolSize > 50 ) - { - corePoolSize = 20; - } - if( workersCount > 200 ) - { - workersCount = 200; - } - taskExecutor = new ThreadPoolExecutor( corePoolSize, workersCount, - 0, TimeUnit.MILLISECONDS, - new LinkedBlockingQueue<>( workQueueSize ), - threadFactory, rejectionHandler ); - } - - @Override - public void stop() - throws Exception - { - running = false; - synchronized( this ) - { - scheduleThread.interrupt(); - } - taskExecutor.shutdown(); - try - { - taskExecutor.awaitTermination( 5, TimeUnit.SECONDS ); - } - catch( InterruptedException e ) - { - e.printStackTrace(); - } - taskExecutor.shutdownNow(); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/ScheduleTime.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/ScheduleTime.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/ScheduleTime.java deleted file mode 100644 index caf9a75..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/ScheduleTime.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler.internal; - -import java.time.Instant; -import org.apache.zest.api.util.NullArgumentException; - -public final class ScheduleTime - implements Comparable<ScheduleTime> -{ - private final String scheduleIdentity; - private final Instant nextTime; - - public ScheduleTime( String scheduleIdentity, Instant nextTime ) - { - NullArgumentException.validateNotEmpty( "scheduleIdentity", scheduleIdentity ); - this.scheduleIdentity = scheduleIdentity; - this.nextTime = nextTime; - } - - public Instant nextTime() - { - return nextTime; - } - - public String scheduleIdentity() - { - return scheduleIdentity; - } - - @Override - public int compareTo( ScheduleTime another ) - { - if( this.scheduleIdentity.equals( another.scheduleIdentity ) ) - { - return 0; - } - - if( this.nextTime.isBefore(another.nextTime) ) - { - return -1; - } - return 1; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/SchedulerMixin.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/SchedulerMixin.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/SchedulerMixin.java deleted file mode 100644 index 43dc826..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/SchedulerMixin.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler.internal; - -import java.time.Instant; -import org.apache.zest.api.configuration.Configuration; -import org.apache.zest.api.injection.scope.Service; -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.injection.scope.This; -import org.apache.zest.api.service.ServiceActivation; -import org.apache.zest.api.unitofwork.NoSuchEntityException; -import org.apache.zest.api.unitofwork.UnitOfWork; -import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException; -import org.apache.zest.api.unitofwork.UnitOfWorkFactory; -import org.apache.zest.api.usecase.UsecaseBuilder; -import org.apache.zest.library.scheduler.CronSchedule; -import org.apache.zest.library.scheduler.Schedule; -import org.apache.zest.library.scheduler.ScheduleFactory; -import org.apache.zest.library.scheduler.Scheduler; -import org.apache.zest.library.scheduler.SchedulerConfiguration; -import org.apache.zest.library.scheduler.SchedulerService; -import org.apache.zest.library.scheduler.SchedulesHandler; -import org.apache.zest.library.scheduler.Task; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SchedulerMixin - implements Scheduler, ServiceActivation -{ - private static final Logger LOGGER = LoggerFactory.getLogger( Scheduler.class ); - - @Service - private ScheduleFactory scheduleFactory; - - @Structure - private UnitOfWorkFactory uowf; - - @This - private SchedulerService me; - - @This - private SchedulesHandler schedulesHandler; - - @This - private Execution execution; - - @This - private Configuration<SchedulerConfiguration> config; - - @Override - public Schedule scheduleOnce( Task task, int initialSecondsDelay ) - { - Schedule schedule = scheduleFactory.newOnceSchedule( task, Instant.now().plusSeconds( initialSecondsDelay ) ); - saveAndDispatch( schedule ); - return schedule; - } - - @Override - public Schedule scheduleOnce( Task task, Instant runAt ) - { - Schedule schedule = scheduleFactory.newOnceSchedule( task, runAt ); - saveAndDispatch( schedule ); - return schedule; - } - - @Override - public Schedule scheduleCron( Task task, String cronExpression ) - { - Schedule schedule = scheduleFactory.newCronSchedule( task, cronExpression, Instant.now() ); - saveAndDispatch( schedule ); - return schedule; - } - - @Override - public Schedule scheduleCron( Task task, @CronSchedule.CronExpression String cronExpression, Instant start ) - { - Schedule schedule = scheduleFactory.newCronSchedule( task, cronExpression, start ); - saveAndDispatch( schedule ); - return schedule; - } - - @Override - public void scheduleCron( Schedule schedule ) - { - saveAndDispatch( schedule ); - } - - @Override - public Schedule scheduleCron( Task task, String cronExpression, long initialDelay ) - { - Instant start = Instant.now().plusMillis( initialDelay ); - Schedule schedule = scheduleFactory.newCronSchedule( task, cronExpression, start ); - saveAndDispatch( schedule ); - return schedule; - } - - @Override - public void cancelSchedule( String scheduleId ) - { - UnitOfWork uow = uowf.currentUnitOfWork(); - Schedule schedule; - try - { - schedule = uow.get( Schedule.class, scheduleId ); - } - catch( NoSuchEntityException e ) - { - return; - } - cancelSchedule( schedule ); - } - - @Override - public void cancelSchedule( Schedule schedule ) - { - Schedules active = schedulesHandler.getActiveSchedules(); - if( active.schedules().remove( schedule ) ) - { - schedule.cancelled().set( true ); - } - } - - private void saveAndDispatch( Schedule schedule ) - { - Schedules schedules = schedulesHandler.getActiveSchedules(); - schedules.schedules().add( schedule ); - execution.dispatchForExecution( schedule ); - } - - private void loadSchedules() - throws UnitOfWorkCompletionException - { - try (UnitOfWork ignored = uowf.newUnitOfWork( UsecaseBuilder.newUsecase( "Initialize Schedules" ) )) - { - Schedules schedules = schedulesHandler.getActiveSchedules(); - for( Schedule schedule : schedules.schedules() ) - { - dispatch( schedule ); - } - } - } - - private void dispatch( Schedule schedule ) - { - try - { - if( !schedule.cancelled().get() && !schedule.done().get() ) - { - execution.dispatchForExecution( schedule ); - } - } - catch( Exception e ) - { - e.printStackTrace(); - } - } - - @Override - public void activateService() - throws Exception - { - // Throws IllegalArgument if corePoolSize or keepAliveTime less than zero, - // or if workersCount less than or equal to zero, - // or if corePoolSize greater than workersCount. - execution.start(); - loadSchedules(); - LOGGER.debug( "Activated" ); - } - - @Override - public void passivateService() - throws Exception - { - execution.stop(); - LOGGER.debug( "Passivated" ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8915dfaa/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/Schedules.java ---------------------------------------------------------------------- diff --git a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/Schedules.java b/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/Schedules.java deleted file mode 100644 index 8aa567a..0000000 --- a/libraries/scheduler/src/main/java/org/apache/zest/library/scheduler/internal/Schedules.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.zest.library.scheduler.internal; - -import org.apache.zest.api.association.ManyAssociation; -import org.apache.zest.library.scheduler.Schedule; - -public interface Schedules -{ - ManyAssociation<Schedule> schedules(); -}
