From 23a090c5d6bb5f4d3a563bb695336a1331b09b6d Mon Sep 17 00:00:00 2001 From: notoon Date: Wed, 10 Aug 2016 16:37:37 +0200 Subject: [PATCH] Add ability to configure Scheduled annotation using props --- .../scheduler/AutomaticScheduleIT.java | 14 +++++++- .../org/seedstack/scheduler/TimedTask4.java | 33 +++++++++++++++++++ .../META-INF/configuration/seed.props | 12 +++++++ .../internal/ScheduledTaskBuilderImpl.java | 11 ++++--- .../internal/ScheduledTasksImpl.java | 6 +++- 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 src/it/java/org/seedstack/scheduler/TimedTask4.java create mode 100644 src/it/resources/META-INF/configuration/seed.props diff --git a/src/it/java/org/seedstack/scheduler/AutomaticScheduleIT.java b/src/it/java/org/seedstack/scheduler/AutomaticScheduleIT.java index 9ab9e39..4ac9b76 100644 --- a/src/it/java/org/seedstack/scheduler/AutomaticScheduleIT.java +++ b/src/it/java/org/seedstack/scheduler/AutomaticScheduleIT.java @@ -12,6 +12,7 @@ import org.junit.Test; import org.junit.runner.RunWith; +import java.util.Date; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -23,12 +24,18 @@ */ @RunWith(SeedITRunner.class) public class AutomaticScheduleIT { - static CountDownLatch countDownLatch = new CountDownLatch(2); + static CountDownLatch countDownLatch = new CountDownLatch(3); static int invocationCount1 = 0; static int invocationCount2 = 0; + static int invocationCount4 = 0; static boolean beforeCalled = false; static boolean afterCalled = false; static boolean onExceptionCalled = false; + static String actualTaskName; + static String actualTriggerName; + private final static String expectedTaskName = "Task4"; + private final static String expectedTriggerName = "Trigger4"; + @Test public void automatically_timed_task() throws Exception { @@ -42,5 +49,10 @@ public void automatically_timed_task() throws Exception { // if the test is slow it will execute TimedTask1 multiple times Assertions.assertThat(invocationCount1).isGreaterThanOrEqualTo(1); Assertions.assertThat(invocationCount2).isGreaterThanOrEqualTo(1); + Assertions.assertThat(invocationCount4).isGreaterThanOrEqualTo(1); + + Assertions.assertThat(actualTaskName).isEqualTo(expectedTaskName); + Assertions.assertThat(actualTriggerName).isEqualTo(expectedTriggerName); } + } diff --git a/src/it/java/org/seedstack/scheduler/TimedTask4.java b/src/it/java/org/seedstack/scheduler/TimedTask4.java new file mode 100644 index 0000000..9d54591 --- /dev/null +++ b/src/it/java/org/seedstack/scheduler/TimedTask4.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2013-2016, The SeedStack authors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.scheduler; + +import org.seedstack.seed.Logging; +import org.slf4j.Logger; + +import static org.seedstack.scheduler.ExceptionPolicy.UNSCHEDULE_ALL_TRIGGERS; + +/** + * @author tuan.docao@ext.mpsa.com + */ +@Scheduled(value = "${org.seedstack.addons.scheduling.cron.expression}", taskName = "${org.seedstack.addons.scheduling.task.name}", triggerName = "${org.seedstack.addons.scheduling.trigger.name}", exceptionPolicy = UNSCHEDULE_ALL_TRIGGERS) +public class TimedTask4 implements Task { + + @Logging + private Logger logger; + + @Override + public void execute(SchedulingContext sc) throws Exception { + logger.info("Executing timed task 4"); + AutomaticScheduleIT.invocationCount4++; + AutomaticScheduleIT.actualTaskName = sc.getTaskName(); + AutomaticScheduleIT.actualTriggerName = sc.getTriggerName(); + AutomaticScheduleIT.countDownLatch.countDown(); + + } +} diff --git a/src/it/resources/META-INF/configuration/seed.props b/src/it/resources/META-INF/configuration/seed.props new file mode 100644 index 0000000..40a0e89 --- /dev/null +++ b/src/it/resources/META-INF/configuration/seed.props @@ -0,0 +1,12 @@ +# +# Copyright (c) 2013-2016, The SeedStack authors +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +[org.seedstack.addons.scheduling] +cron.expression = * * * * * ? +task.name = Task4 +trigger.name = Trigger4 \ No newline at end of file diff --git a/src/main/java/org/seedstack/scheduler/internal/ScheduledTaskBuilderImpl.java b/src/main/java/org/seedstack/scheduler/internal/ScheduledTaskBuilderImpl.java index 3ddb8b6..615278d 100644 --- a/src/main/java/org/seedstack/scheduler/internal/ScheduledTaskBuilderImpl.java +++ b/src/main/java/org/seedstack/scheduler/internal/ScheduledTaskBuilderImpl.java @@ -9,6 +9,7 @@ import org.seedstack.scheduler.ScheduledTaskBuilder; import org.seedstack.scheduler.Task; +import org.seedstack.seed.Application; import org.seedstack.seed.SeedException; import org.seedstack.scheduler.Scheduled; import org.apache.commons.lang.StringUtils; @@ -111,9 +112,11 @@ class ScheduledTaskBuilderImpl implements ScheduledTaskBuilder { private String jobName; + private Application application; + private Class taskClass; - ScheduledTaskBuilderImpl(final Class taskClass, Scheduler scheduler) { + ScheduledTaskBuilderImpl(final Class taskClass, Scheduler scheduler, Application application) { this.jobClass = TaskDelegateJob.class; this.scheduler = scheduler; this.taskClass = taskClass; @@ -129,9 +132,9 @@ class ScheduledTaskBuilderImpl implements ScheduledTaskBuilder { // if present, the name associated to a trigger or a job is retrieved // else, it is generated but can still be provided // with DSL withTriggerName() / withTaskName() methods - this.cronExpression = annotation.value(); - this.jobName = DEFAULT.equals(annotation.taskName()) ? UUID.randomUUID().toString() : annotation.taskName(); - this.triggerName = DEFAULT.equals(annotation.triggerName()) ? UUID.randomUUID().toString() : annotation.triggerName(); + this.cronExpression = application.substituteWithConfiguration(annotation.value()); + this.jobName = DEFAULT.equals(annotation.taskName()) ? UUID.randomUUID().toString() : application.substituteWithConfiguration(annotation.taskName()); + this.triggerName = DEFAULT.equals(annotation.triggerName()) ? UUID.randomUUID().toString() : application.substituteWithConfiguration(annotation.triggerName()); this.timeZone = !DEFAULT.equals(annotation.timeZoneId()) ? TimeZone.getTimeZone(annotation.timeZoneId()) : getDefault(); this.requestRecovery = annotation.requestRecovery(); this.priority = annotation.priority(); diff --git a/src/main/java/org/seedstack/scheduler/internal/ScheduledTasksImpl.java b/src/main/java/org/seedstack/scheduler/internal/ScheduledTasksImpl.java index c12e2ec..1de1649 100644 --- a/src/main/java/org/seedstack/scheduler/internal/ScheduledTasksImpl.java +++ b/src/main/java/org/seedstack/scheduler/internal/ScheduledTasksImpl.java @@ -11,6 +11,7 @@ import org.seedstack.scheduler.ScheduledTasks; import org.seedstack.scheduler.Task; import org.quartz.Scheduler; +import org.seedstack.seed.Application; import javax.inject.Inject; @@ -23,8 +24,11 @@ class ScheduledTasksImpl implements ScheduledTasks { @Inject private Scheduler scheduler; + @Inject + private Application application; + @Override public ScheduledTaskBuilder scheduledTask(Class taskClass) { - return new ScheduledTaskBuilderImpl(taskClass, scheduler); + return new ScheduledTaskBuilderImpl(taskClass, scheduler, application); } }