-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#103): add SchedulingMonitorInternal
- Loading branch information
Showing
5 changed files
with
130 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
core/src/main/java/io/github/zero88/schedulerx/impl/SchedulingMonitorAbstract.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.zero88.schedulerx.impl; | ||
|
||
import static io.github.zero88.schedulerx.impl.Utils.brackets; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
import io.github.zero88.schedulerx.ExecutionResult; | ||
import io.github.zero88.schedulerx.SchedulingMonitor; | ||
import io.vertx.core.impl.logging.Logger; | ||
|
||
abstract class SchedulingMonitorAbstract<OUT> implements SchedulingMonitor<OUT> { | ||
|
||
protected abstract Logger logger(); | ||
|
||
protected final void dispatch(Class<? extends SchedulingMonitor> monitorCls, | ||
Consumer<ExecutionResult<OUT>> dispatcher, ExecutionResult<OUT> result) { | ||
try { | ||
if (Objects.nonNull(dispatcher)) { | ||
dispatcher.accept(result); | ||
} | ||
} catch (Throwable ex) { | ||
logger().warn( | ||
"Unexpected error in " + brackets(monitorCls.getName()) + "when dispatching the execution result", ex); | ||
} | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
core/src/main/java/io/github/zero88/schedulerx/impl/SchedulingMonitorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.github.zero88.schedulerx.impl; | ||
|
||
import java.util.Optional; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import io.github.zero88.schedulerx.ExecutionResult; | ||
import io.github.zero88.schedulerx.SchedulingLogMonitor; | ||
import io.github.zero88.schedulerx.SchedulingMonitor; | ||
import io.github.zero88.schedulerx.WorkerExecutorFactory; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.WorkerExecutor; | ||
import io.vertx.core.impl.logging.Logger; | ||
import io.vertx.core.impl.logging.LoggerFactory; | ||
|
||
final class SchedulingMonitorImpl<OUT> extends SchedulingMonitorAbstract<OUT> | ||
implements SchedulingMonitorInternal<OUT> { | ||
|
||
private final WorkerExecutor executor; | ||
private final SchedulingMonitor<OUT> monitor; | ||
|
||
public SchedulingMonitorImpl(@NotNull Vertx vertx, @Nullable SchedulingMonitor<OUT> monitor) { | ||
this.executor = WorkerExecutorFactory.createMonitorWorker(vertx); | ||
this.monitor = Optional.ofNullable(monitor).orElseGet(SchedulingLogMonitor::create); | ||
} | ||
|
||
@Override | ||
protected Logger logger() { | ||
return LoggerFactory.getLogger(SchedulingMonitorInternal.class); | ||
} | ||
|
||
@Override | ||
public void onUnableSchedule(@NotNull ExecutionResult<OUT> result) { | ||
executor.executeBlocking(event -> dispatch(monitor.getClass(), monitor::onUnableSchedule, result)); | ||
} | ||
|
||
@Override | ||
public void onSchedule(@NotNull ExecutionResult<OUT> result) { | ||
executor.executeBlocking(event -> dispatch(monitor.getClass(), monitor::onSchedule, result)); | ||
} | ||
|
||
@Override | ||
public void onMisfire(@NotNull ExecutionResult<OUT> result) { | ||
executor.executeBlocking(event -> dispatch(monitor.getClass(), monitor::onMisfire, result)); | ||
} | ||
|
||
@Override | ||
public void onEach(@NotNull ExecutionResult<OUT> result) { | ||
executor.executeBlocking(event -> dispatch(monitor.getClass(), monitor::onEach, result)); | ||
} | ||
|
||
@Override | ||
public void onCompleted(@NotNull ExecutionResult<OUT> result) { | ||
executor.executeBlocking(event -> dispatch(monitor.getClass(), monitor::onCompleted, result)); | ||
} | ||
|
||
public SchedulingMonitor<OUT> unwrap() { | ||
return this.monitor; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
core/src/main/java/io/github/zero88/schedulerx/impl/SchedulingMonitorInternal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.github.zero88.schedulerx.impl; | ||
|
||
import io.github.zero88.schedulerx.SchedulingMonitor; | ||
|
||
/** | ||
* An internal scheduling monitor to ensure the monitor operation is run on the dedicated thread | ||
* | ||
* @param <OUT> Type of job result data | ||
* @since 2.0.0 | ||
*/ | ||
interface SchedulingMonitorInternal<OUT> extends SchedulingMonitor<OUT> { | ||
|
||
SchedulingMonitor<OUT> unwrap(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters