diff --git a/src/main/java/io/github/simplex/api/ExecutableService.java b/src/main/java/io/github/simplex/api/ExecutableService.java index e20bcf9..16d9a2a 100644 --- a/src/main/java/io/github/simplex/api/ExecutableService.java +++ b/src/main/java/io/github/simplex/api/ExecutableService.java @@ -7,16 +7,14 @@ public abstract class ExecutableService implements IService { private final Plugin plugin; private final long delay; private final long period; - private final boolean delayed; private final boolean repeating; - public ExecutableService(Plugin plugin, int serviceID, long delay, long period, boolean delayed, boolean repeating) { + public ExecutableService(Plugin plugin, int serviceID, long delay, long period, boolean repeating) { this.plugin = plugin; this.serviceID = serviceID; this.repeating = repeating; this.delay = delay; this.period = period; - this.delayed = delayed; } @Override @@ -39,11 +37,6 @@ public abstract class ExecutableService implements IService { return period; } - @Override - public boolean isDelayed() { - return delayed; - } - @Override public boolean isRepeating() { return repeating; diff --git a/src/main/java/io/github/simplex/api/IService.java b/src/main/java/io/github/simplex/api/IService.java index 7b8014d..e79392d 100644 --- a/src/main/java/io/github/simplex/api/IService.java +++ b/src/main/java/io/github/simplex/api/IService.java @@ -8,8 +8,6 @@ import java.util.concurrent.RunnableScheduledFuture; public interface IService extends RunnableScheduledFuture { int getServiceID(); - boolean isDelayed(); - boolean isRepeating(); long getPeriod(); diff --git a/src/main/java/io/github/simplex/simplexss/ServiceManager.java b/src/main/java/io/github/simplex/simplexss/ServiceManager.java index c973351..29b7835 100644 --- a/src/main/java/io/github/simplex/simplexss/ServiceManager.java +++ b/src/main/java/io/github/simplex/simplexss/ServiceManager.java @@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.stream.Stream; @@ -22,24 +23,36 @@ public final class ServiceManager { @Contract(pure = true, value = "_ -> new") public @NotNull Mono createServicePool(IService... services) { - ServicePool pool = new ServicePool(); + ServicePool pool = new ServicePool(false); Stream.of(services).forEach(pool::addService); return Mono.just(pool); } - @Contract("_, _ -> param1") - public Mono addToExistingPool(@NotNull ServicePool pool, IService... services) { - Stream.of(services).forEach(pool::addService); + public @NotNull Mono multithreadedServicePool(IService... services) { + ServicePool pool = new ServicePool(true); + Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> { + pool.addService(s.get()); + }); return Mono.just(pool); } @Contract("_, _ -> param1") - public Mono takeFromExistingPool(@NotNull ServicePool pool, IService... services) { - Stream.of(services).forEach(pool::removeService); + public @NotNull Mono addToExistingPool(@NotNull ServicePool pool, IService... services) { + Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> { + pool.addService(s.get()); + }); return Mono.just(pool); } - public Flux getServicePools() { + @Contract("_, _ -> param1") + public @NotNull Mono takeFromExistingPool(@NotNull ServicePool pool, IService... services) { + Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> { + pool.removeService(s.get()); + }); + return Mono.just(pool); + } + + public @NotNull Flux getServicePools() { return Flux.fromIterable(servicePools); } diff --git a/src/main/java/io/github/simplex/simplexss/ServicePool.java b/src/main/java/io/github/simplex/simplexss/ServicePool.java index fb15260..782f07d 100644 --- a/src/main/java/io/github/simplex/simplexss/ServicePool.java +++ b/src/main/java/io/github/simplex/simplexss/ServicePool.java @@ -17,12 +17,14 @@ import java.util.concurrent.TimeUnit; public final class ServicePool { private final Set associatedServices; private final Scheduler scheduler; - private final ExecutorService executor; - public ServicePool() { + public ServicePool(boolean multithreaded) { this.associatedServices = new HashSet<>(); - this.executor = Executors.newSingleThreadExecutor(); - this.scheduler = Schedulers.fromExecutorService(executor); + if (multithreaded) { + this.scheduler = Schedulers.fromExecutorService(Executors.newFixedThreadPool(4)); + } else { + this.scheduler = Schedulers.fromExecutorService(Executors.newSingleThreadExecutor()); + } } void addService(IService service) { @@ -38,6 +40,22 @@ public final class ServicePool { return associatedServices; } + public Mono startService(int serviceID) { + Mono service = getService(serviceID); + return service.map(s -> { + if (s.isRepeating()) { + return scheduler.schedulePeriodically(s, + s.getDelay() * 5, + s.getPeriod() * 5, + TimeUnit.MILLISECONDS); + } + return scheduler.schedule(s, + s.getDelay() * 5, + TimeUnit.MILLISECONDS); + + }); + } + public Flux startServices() { return Mono.just(getAssociatedServices()).flatMapMany(services -> { Set disposables = new HashSet<>(); @@ -47,10 +65,6 @@ public final class ServicePool { service.getDelay() * 5, service.getPeriod() * 5, TimeUnit.MILLISECONDS)); - } else if (service.isDelayed()) { - disposables.add(scheduler.schedule(service, - service.getDelay() * 5, - TimeUnit.MILLISECONDS)); } else { disposables.add(scheduler.schedule(service)); } @@ -61,11 +75,11 @@ public final class ServicePool { } public Mono stopServices(Flux disposableThread) { - return Mono.just(getAssociatedServices()).doOnNext(services -> { - for (IService service : services) { - disposableThread.doOnNext(Disposable::dispose); - } - }).then(); + return disposableThread.doOnNext(Disposable::dispose).then(); + } + + public Mono stopService(int serviceID) { + return getService(serviceID).doOnNext(IService::stop).then(); } public Mono getService(int serviceID) { @@ -78,8 +92,8 @@ public final class ServicePool { getAssociatedServices().remove(service); } - public ServicePool recycle() { + public Mono recycle() { this.getAssociatedServices().clear(); - return this; + return Mono.just(this); } }