From c9fa0c1ca0f2e06892cff8a04073350019a0ceab Mon Sep 17 00:00:00 2001 From: Paldiu Date: Thu, 15 Dec 2022 13:32:28 -0600 Subject: [PATCH] Switch to using Mono.create() Switched from using Mono.just(T) and Mono.empty() for creating Mono objects to Mono.create(sink -> {}); --- .../simplexdevelopment/impl/ServiceImpl.java | 17 ++++++++--------- .../scheduler/ExecutableService.java | 15 +++++++++++++-- .../scheduler/SchedulingSystem.java | 6 +----- .../scheduler/ServicePool.java | 14 ++++++++------ 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/github/simplexdevelopment/impl/ServiceImpl.java b/src/main/java/io/github/simplexdevelopment/impl/ServiceImpl.java index c8880e4..a54ff26 100644 --- a/src/main/java/io/github/simplexdevelopment/impl/ServiceImpl.java +++ b/src/main/java/io/github/simplexdevelopment/impl/ServiceImpl.java @@ -26,7 +26,6 @@ package io.github.simplexdevelopment.impl; import io.github.simplexdevelopment.scheduler.ExecutableService; import io.github.simplexdevelopment.scheduler.ServicePool; -import org.bukkit.plugin.java.JavaPlugin; import reactor.core.publisher.Mono; public class ServiceImpl extends ExecutableService { @@ -39,18 +38,18 @@ public class ServiceImpl extends ExecutableService { @Override public Mono start() { - return Mono.just(plugin) - .map(JavaPlugin::getLogger) - .doOnNext(l -> l.info("The service has executed successfully!")) - .then(); + return Mono.create(sink -> { + plugin.getLogger().info("The service has started successfully!"); + sink.success(); + }); } @Override public Mono stop() { - return Mono.just(plugin) - .map(JavaPlugin::getLogger) - .doOnNext(l -> l.info("The service has stopped")) - .then(); + return Mono.create(sink -> { + plugin.getLogger().info("The service has stopped successfully!"); + sink.success(); + }); } @Override diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/ExecutableService.java b/src/main/java/io/github/simplexdevelopment/scheduler/ExecutableService.java index ef0ab67..d307ffd 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/ExecutableService.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/ExecutableService.java @@ -181,7 +181,15 @@ public abstract class ExecutableService implements IService { this.delay = Objects.requireNonNullElse(delay, 0L); this.period = Objects.requireNonNullElse(period, (20L * 60L) * 20L); this.mayInterruptWhenRunning = mayInterruptWhenRunning; - this.parentPool = parentPool; + + if (parentPool == null) { + this.parentPool = new ServicePool("defaultPool" + SchedulingSystem.denom, false); + SchedulingSystem.denom++; + } else { + this.parentPool = parentPool; + } + + this.parentPool.getAssociatedServices().add(this); } @Override @@ -245,6 +253,9 @@ public abstract class ExecutableService implements IService { @Override public Mono setParentPool(ServicePool servicePool) { - return Mono.just(servicePool).doOnNext(pool -> this.parentPool = pool).then(); + return Mono.create(sink -> { + this.parentPool = servicePool; + sink.success(); + }); } } diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java b/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java index a8d4c9b..64079d0 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/SchedulingSystem.java @@ -102,11 +102,7 @@ public final class SchedulingSystem implements ISchedule { @Override public @NotNull Mono runOnce(IService service) { - return Mono.just(service) - .doOnNext(s -> s.start() - .then(s.stop()) - .subscribe()) - .then(); + return Mono.create(sink -> service.start().then(service.stop()).subscribe(sink::success)); } @Override diff --git a/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java b/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java index e4cfa8f..14d9eab 100644 --- a/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java +++ b/src/main/java/io/github/simplexdevelopment/scheduler/ServicePool.java @@ -186,11 +186,13 @@ public final class ServicePool implements Identifier { * @return A {@link Mono} object which can be used to stop the service. */ public @NotNull Mono stopService(@NotNull String service_name, @Nullable Mono disposable) { - getService(service_name).doOnNext(IService::stop).subscribe(); - if (disposable != null) { - disposable.doOnNext(Disposable::dispose).subscribe(); - } - return Mono.empty(); + return Mono.create(sink -> { + getService(service_name).doOnNext(IService::stop).subscribe(); + if (disposable != null) { + disposable.doOnNext(Disposable::dispose).subscribe(); + } + sink.success(); + }); } /** @@ -223,7 +225,7 @@ public final class ServicePool implements Identifier { */ public @NotNull Mono recycle() { this.getAssociatedServices().clear(); - return Mono.just(this); + return Mono.create(sink -> sink.success(this)); } /**