Skip to content

Commit

Permalink
feat(#52): Makes contract between Task and JobData
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Jul 28, 2023
1 parent 53891ce commit 12e0126
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 107 deletions.
24 changes: 20 additions & 4 deletions core/src/main/java/io/github/zero88/schedulerx/JobData.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package io.github.zero88.schedulerx;

import org.jetbrains.annotations.Nullable;

/**
* Represents for input task data
* Represents for a provider that supply an input task data
*
* @param <T> Type of data
* @since 1.0.0
*/
@FunctionalInterface
public interface JobData {
public interface JobData<T> {

Object get();
/**
* Get input data
*
* @return input data
*/
@Nullable T get();

JobData EMPTY = () -> null;
/**
* Create emtpy data
*
* @param <D> Type of data
* @return JobData contains null data
*/
static <D> JobData<D> empty() {
return () -> null;
}

}
17 changes: 11 additions & 6 deletions core/src/main/java/io/github/zero88/schedulerx/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import org.jetbrains.annotations.NotNull;

import io.github.zero88.schedulerx.trigger.Trigger;

/**
* Represents for Task to run on each trigger time
*
* @see Trigger
* @param <T> Type of input data
* @since 1.0.0
*/
public interface Task {
public interface Task<T> {

/**
* Identify task is async or not
Expand All @@ -25,12 +23,19 @@ default boolean isAsync() {
}

/**
* Execute task
* Execute task.
* <p/>
* After executed task, please remember to:
* <ul>
* <li>set value in case of task is success via {@link TaskExecutionContext#complete(Object)}</li>
* <li>set error in case of task is failed via {@link TaskExecutionContext#fail(Throwable)}</li>
* </ul>
*
* @param jobData job data
* @param executionContext task execution context
* @see JobData
* @see TaskExecutionContext
*/
void execute(@NotNull JobData jobData, @NotNull TaskExecutionContext executionContext);
void execute(@NotNull JobData<T> jobData, @NotNull TaskExecutionContext executionContext);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
/**
* Represents for an executor run {@code task} in conditional loop
*
* @param <INPUT> Type of input data
* @since 1.0.0
*/
@VertxGen(concrete = false)
public interface TaskExecutor extends TaskExecutorProperties {
public interface TaskExecutor<INPUT> extends TaskExecutorProperties<INPUT> {

/**
* Task executor state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
/**
* Shared immutable fields between TaskExecutor and its builder
*
* @param <INPUT> Type of input data
* @since 2.0.0
*/
@Internal
@VertxGen(concrete = false)
public interface TaskExecutorProperties {
public interface TaskExecutorProperties<INPUT> {

/**
* Vertx
Expand All @@ -39,7 +40,7 @@ public interface TaskExecutorProperties {
* @see Task
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull Task task();
@NotNull Task<INPUT> task();

/**
* Defines job data as input task data
Expand All @@ -48,6 +49,6 @@ public interface TaskExecutorProperties {
* @see JobData
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull JobData jobData();
@NotNull JobData<INPUT> jobData();

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
/**
* Represents for an executor run task based on particular trigger
*
* @param <T> Type of Trigger
* @param <INPUT> Type of Input Job data
* @param <TRIGGER> Type of Trigger
* @see Trigger
* @see TaskExecutor
* @since 1.0.0
*/
public interface TriggerTaskExecutor<T extends Trigger> extends TaskExecutor {
public interface TriggerTaskExecutor<INPUT, TRIGGER extends Trigger> extends TaskExecutor<INPUT> {

/**
* Trigger type
*
* @return trigger
*/
@NotNull T trigger();
@NotNull TRIGGER trigger();

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@
/**
* Represents for the high level of a builder that construct {@code TriggerTaskExecutor}
*
* @param <INPUT> Type of job input data
* @param <TRIGGER> Type of Trigger
* @param <EXECUTOR> Type of Trigger Task Executor
* @param <SELF> Type of Executor Builder
* @see Trigger
* @see TriggerTaskExecutor
* @since 2.0.0
*/
public interface TriggerTaskExecutorBuilder<TRIGGER extends Trigger, EXECUTOR extends TriggerTaskExecutor<TRIGGER>,
SELF extends TriggerTaskExecutorBuilder<TRIGGER, EXECUTOR, SELF>>
extends TaskExecutorProperties {
public interface TriggerTaskExecutorBuilder<INPUT, TRIGGER extends Trigger,
EXECUTOR extends TriggerTaskExecutor<INPUT, TRIGGER>,
SELF extends TriggerTaskExecutorBuilder<INPUT, TRIGGER, EXECUTOR, SELF>>
extends TaskExecutorProperties<INPUT> {

@NotNull TRIGGER trigger();

@NotNull SELF setVertx(@NotNull Vertx vertx);

@NotNull SELF setTask(@NotNull Task task);
@NotNull SELF setTask(@NotNull Task<INPUT> task);

@NotNull SELF setTrigger(@NotNull TRIGGER trigger);

@NotNull SELF setJobData(@NotNull JobData jobData);
@NotNull SELF setJobData(@NotNull JobData<INPUT> jobData);

@NotNull SELF setMonitor(@NotNull TaskExecutorMonitor monitor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
/**
* The base task executor
*
* @param <I> Type of input data
* @param <T> Type of trigger
*/
public abstract class AbstractTaskExecutor<T extends Trigger> implements TriggerTaskExecutor<T> {
public abstract class AbstractTaskExecutor<I, T extends Trigger> implements TriggerTaskExecutor<I, T> {

@SuppressWarnings("java:S3416")
protected static final Logger LOGGER = LoggerFactory.getLogger(TaskExecutor.class);
Expand All @@ -39,17 +40,17 @@ public abstract class AbstractTaskExecutor<T extends Trigger> implements Trigger
@NotNull
private final TaskExecutorMonitor monitor;
@NotNull
private final JobData jobData;
private final JobData<I> jobData;
@NotNull
private final Task task;
private final Task<I> task;
@NotNull
private final T trigger;
private final Lock lock = new ReentrantLock();
private boolean didTriggerValidation = false;
private IllegalArgumentException invalidTrigger;

protected AbstractTaskExecutor(@NotNull Vertx vertx, @NotNull TaskExecutorMonitor monitor, @NotNull JobData jobData,
@NotNull Task task, @NotNull T trigger) {
protected AbstractTaskExecutor(@NotNull Vertx vertx, @NotNull TaskExecutorMonitor monitor,
@NotNull JobData<I> jobData, @NotNull Task<I> task, @NotNull T trigger) {
this.vertx = vertx;
this.monitor = monitor;
this.jobData = jobData;
Expand All @@ -68,10 +69,10 @@ protected AbstractTaskExecutor(@NotNull Vertx vertx, @NotNull TaskExecutorMonito
public final @NotNull TaskExecutorMonitor monitor() { return this.monitor; }

@Override
public final @NotNull JobData jobData() { return this.jobData; }
public final @NotNull JobData<I> jobData() { return this.jobData; }

@Override
public final @NotNull Task task() { return this.task; }
public final @NotNull Task<I> task() { return this.task; }

@Override
public final @NotNull T trigger() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.zero88.schedulerx.impl;

import java.util.Objects;
import java.util.Optional;

import org.jetbrains.annotations.NotNull;

Expand All @@ -17,14 +18,14 @@
* The base task executor builder
*/
@SuppressWarnings("unchecked")
public abstract class AbstractTaskExecutorBuilder<T extends Trigger, E extends TriggerTaskExecutor<T>,
B extends TriggerTaskExecutorBuilder<T, E, B>>
implements TriggerTaskExecutorBuilder<T, E, B> {
public abstract class AbstractTaskExecutorBuilder<I, T extends Trigger, E extends TriggerTaskExecutor<I, T>,
B extends TriggerTaskExecutorBuilder<I, T, E, B>>
implements TriggerTaskExecutorBuilder<I, T, E, B> {

private Vertx vertx;
private TaskExecutorMonitor monitor = TaskExecutorLogMonitor.LOG_MONITOR;
private JobData jobData = JobData.EMPTY;
private Task task;
private JobData<I> jobData;
private Task<I> task;
private T trigger;

@Override
Expand All @@ -41,17 +42,19 @@ public abstract class AbstractTaskExecutorBuilder<T extends Trigger, E extends T
public @NotNull T trigger() { return Objects.requireNonNull(trigger, "Trigger is required"); }

@Override
public @NotNull Task task() { return Objects.requireNonNull(task, "Task is required"); }
public @NotNull Task<I> task() { return Objects.requireNonNull(task, "Task is required"); }

@Override
public @NotNull JobData jobData() { return Objects.requireNonNull(jobData, "JobData is required"); }
public @NotNull JobData<I> jobData() {
return Optional.ofNullable(jobData).orElseGet(JobData::empty);
}

public @NotNull B setVertx(@NotNull Vertx vertx) {
this.vertx = vertx;
return (B) this;
}

public @NotNull B setTask(@NotNull Task task) {
public @NotNull B setTask(@NotNull Task<I> task) {
this.task = task;
return (B) this;
}
Expand All @@ -66,7 +69,7 @@ public abstract class AbstractTaskExecutorBuilder<T extends Trigger, E extends T
return (B) this;
}

public @NotNull B setJobData(@NotNull JobData jobData) {
public @NotNull B setJobData(@NotNull JobData<I> jobData) {
this.jobData = jobData;
return (B) this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
/**
* Represents for the task executor has an execution loop based on the timer of cron expressions.
*
* @param <INPUT> Type of job input data
* @see CronTrigger
* @since 2.0.0
*/
@VertxGen
public interface CronTriggerExecutor extends TriggerTaskExecutor<CronTrigger> {
public interface CronTriggerExecutor<INPUT> extends TriggerTaskExecutor<INPUT, CronTrigger> {

static CronTriggerExecutorBuilder builder() { return new CronTriggerExecutorImpl.CronTriggerExecutorBuilderImpl(); }
static <T> CronTriggerExecutorBuilder<T> builder() {
return new CronTriggerExecutorImpl.CronTriggerExecutorBuilderImpl<>();
}

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,37 @@
/**
* Represents a builder that constructs {@link CronTriggerExecutor}
*
* @param <INPUT> Type of Input data
* @since 2.0.0
*/
@VertxGen
public interface CronTriggerExecutorBuilder
extends TriggerTaskExecutorBuilder<CronTrigger, CronTriggerExecutor, CronTriggerExecutorBuilder> {
public interface CronTriggerExecutorBuilder<INPUT> extends
TriggerTaskExecutorBuilder<INPUT, CronTrigger,
CronTriggerExecutor<INPUT>,
CronTriggerExecutorBuilder<INPUT>> {

@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull CronTrigger trigger();

@Fluent
@NotNull CronTriggerExecutorBuilder setVertx(@NotNull Vertx vertx);
@NotNull CronTriggerExecutorBuilder<INPUT> setVertx(@NotNull Vertx vertx);

@Fluent
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull CronTriggerExecutorBuilder setTask(@NotNull Task task);
@NotNull CronTriggerExecutorBuilder<INPUT> setTask(@NotNull Task<INPUT> task);

@Fluent
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull CronTriggerExecutorBuilder setTrigger(@NotNull CronTrigger trigger);
@NotNull CronTriggerExecutorBuilder<INPUT> setTrigger(@NotNull CronTrigger trigger);

@Fluent
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull CronTriggerExecutorBuilder setJobData(@NotNull JobData jobData);
@NotNull CronTriggerExecutorBuilder<INPUT> setJobData(@NotNull JobData<INPUT> jobData);

@Fluent
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull CronTriggerExecutorBuilder setMonitor(@NotNull TaskExecutorMonitor monitor);
@NotNull CronTriggerExecutorBuilder<INPUT> setMonitor(@NotNull TaskExecutorMonitor monitor);

@NotNull CronTriggerExecutor build();
@NotNull CronTriggerExecutor<INPUT> build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import io.vertx.core.Vertx;
import io.vertx.core.WorkerExecutor;

final class CronTriggerExecutorImpl extends AbstractTaskExecutor<CronTrigger> implements CronTriggerExecutor {
final class CronTriggerExecutorImpl<I> extends AbstractTaskExecutor<I, CronTrigger> implements CronTriggerExecutor<I> {

CronTriggerExecutorImpl(@NotNull Vertx vertx, @NotNull TaskExecutorMonitor monitor, @NotNull JobData jobData,
@NotNull Task task, @NotNull CronTrigger trigger) {
CronTriggerExecutorImpl(@NotNull Vertx vertx, @NotNull TaskExecutorMonitor monitor, @NotNull JobData<I> jobData,
@NotNull Task<I> task, @NotNull CronTrigger trigger) {
super(vertx, monitor, jobData, task, trigger);
}

Expand All @@ -40,12 +40,12 @@ protected boolean shouldCancel(long round) {
return false;
}

static final class CronTriggerExecutorBuilderImpl
extends AbstractTaskExecutorBuilder<CronTrigger, CronTriggerExecutor, CronTriggerExecutorBuilder>
implements CronTriggerExecutorBuilder {
static final class CronTriggerExecutorBuilderImpl<D>
extends AbstractTaskExecutorBuilder<D, CronTrigger, CronTriggerExecutor<D>, CronTriggerExecutorBuilder<D>>
implements CronTriggerExecutorBuilder<D> {

public @NotNull CronTriggerExecutor build() {
return new CronTriggerExecutorImpl(vertx(), monitor(), jobData(), task(), trigger());
public @NotNull CronTriggerExecutor<D> build() {
return new CronTriggerExecutorImpl<>(vertx(), monitor(), jobData(), task(), trigger());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
/**
* Represents for the task executor has an execution loop based on interval.
*
* @param <INPUT> Type of job input data
* @see IntervalTrigger
* @since 2.0.0
*/
@VertxGen
public interface IntervalTriggerExecutor extends TriggerTaskExecutor<IntervalTrigger> {
public interface IntervalTriggerExecutor<INPUT> extends TriggerTaskExecutor<INPUT, IntervalTrigger> {

static IntervalTriggerExecutorBuilder builder() { return new IntervalTriggerExecutorImpl.IntervalTriggerExecutorBuilderImpl(); }
static <T> IntervalTriggerExecutorBuilder<T> builder() {
return new IntervalTriggerExecutorImpl.IntervalTriggerExecutorBuilderImpl<>();
}

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
Expand Down
Loading

0 comments on commit 12e0126

Please sign in to comment.