Skip to content

Commit

Permalink
Merge pull request #3 from notoon/feat_props_config_sub
Browse files Browse the repository at this point in the history
Add ability to configure Scheduled annotation using props
  • Loading branch information
adrienlauer committed Aug 10, 2016
2 parents 98fd197 + 23a090c commit 8ca5044
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/it/java/org/seedstack/scheduler/AutomaticScheduleIT.java
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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);
}

}
33 changes: 33 additions & 0 deletions src/it/java/org/seedstack/scheduler/TimedTask4.java
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
*
* 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();

}
}
12 changes: 12 additions & 0 deletions src/it/resources/META-INF/configuration/seed.props
@@ -0,0 +1,12 @@
#
# Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
#
# 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
Expand Up @@ -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;
Expand Down Expand Up @@ -111,9 +112,11 @@ class ScheduledTaskBuilderImpl implements ScheduledTaskBuilder {

private String jobName;

private Application application;

private Class<? extends Task> taskClass;

ScheduledTaskBuilderImpl(final Class<? extends Task> taskClass, Scheduler scheduler) {
ScheduledTaskBuilderImpl(final Class<? extends Task> taskClass, Scheduler scheduler, Application application) {
this.jobClass = TaskDelegateJob.class;
this.scheduler = scheduler;
this.taskClass = taskClass;
Expand All @@ -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();
Expand Down
Expand Up @@ -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;

Expand All @@ -23,8 +24,11 @@ class ScheduledTasksImpl implements ScheduledTasks {
@Inject
private Scheduler scheduler;

@Inject
private Application application;

@Override
public ScheduledTaskBuilder scheduledTask(Class<? extends Task> taskClass) {
return new ScheduledTaskBuilderImpl(taskClass, scheduler);
return new ScheduledTaskBuilderImpl(taskClass, scheduler, application);
}
}

0 comments on commit 8ca5044

Please sign in to comment.