mirror of
https://github.com/SimplexDevelopment/SimplexSS.git
synced 2024-12-22 13:07:37 +00:00
Oops
Fixed up some things I noticed were incorrect
This commit is contained in:
parent
bbd09910f3
commit
c8b5afe1dd
@ -7,16 +7,14 @@ public abstract class ExecutableService implements IService {
|
|||||||
private final Plugin plugin;
|
private final Plugin plugin;
|
||||||
private final long delay;
|
private final long delay;
|
||||||
private final long period;
|
private final long period;
|
||||||
private final boolean delayed;
|
|
||||||
private final boolean repeating;
|
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.plugin = plugin;
|
||||||
this.serviceID = serviceID;
|
this.serviceID = serviceID;
|
||||||
this.repeating = repeating;
|
this.repeating = repeating;
|
||||||
this.delay = delay;
|
this.delay = delay;
|
||||||
this.period = period;
|
this.period = period;
|
||||||
this.delayed = delayed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -39,11 +37,6 @@ public abstract class ExecutableService implements IService {
|
|||||||
return period;
|
return period;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isDelayed() {
|
|
||||||
return delayed;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isRepeating() {
|
public boolean isRepeating() {
|
||||||
return repeating;
|
return repeating;
|
||||||
|
@ -8,8 +8,6 @@ import java.util.concurrent.RunnableScheduledFuture;
|
|||||||
public interface IService extends RunnableScheduledFuture<IService> {
|
public interface IService extends RunnableScheduledFuture<IService> {
|
||||||
int getServiceID();
|
int getServiceID();
|
||||||
|
|
||||||
boolean isDelayed();
|
|
||||||
|
|
||||||
boolean isRepeating();
|
boolean isRepeating();
|
||||||
|
|
||||||
long getPeriod();
|
long getPeriod();
|
||||||
|
@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@ -22,24 +23,36 @@ public final class ServiceManager {
|
|||||||
|
|
||||||
@Contract(pure = true, value = "_ -> new")
|
@Contract(pure = true, value = "_ -> new")
|
||||||
public @NotNull Mono<ServicePool> createServicePool(IService... services) {
|
public @NotNull Mono<ServicePool> createServicePool(IService... services) {
|
||||||
ServicePool pool = new ServicePool();
|
ServicePool pool = new ServicePool(false);
|
||||||
Stream.of(services).forEach(pool::addService);
|
Stream.of(services).forEach(pool::addService);
|
||||||
return Mono.just(pool);
|
return Mono.just(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract("_, _ -> param1")
|
public @NotNull Mono<ServicePool> multithreadedServicePool(IService... services) {
|
||||||
public Mono<ServicePool> addToExistingPool(@NotNull ServicePool pool, IService... services) {
|
ServicePool pool = new ServicePool(true);
|
||||||
Stream.of(services).forEach(pool::addService);
|
Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> {
|
||||||
|
pool.addService(s.get());
|
||||||
|
});
|
||||||
return Mono.just(pool);
|
return Mono.just(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract("_, _ -> param1")
|
@Contract("_, _ -> param1")
|
||||||
public Mono<ServicePool> takeFromExistingPool(@NotNull ServicePool pool, IService... services) {
|
public @NotNull Mono<ServicePool> addToExistingPool(@NotNull ServicePool pool, IService... services) {
|
||||||
Stream.of(services).forEach(pool::removeService);
|
Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> {
|
||||||
|
pool.addService(s.get());
|
||||||
|
});
|
||||||
return Mono.just(pool);
|
return Mono.just(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Flux<ServicePool> getServicePools() {
|
@Contract("_, _ -> param1")
|
||||||
|
public @NotNull Mono<ServicePool> takeFromExistingPool(@NotNull ServicePool pool, IService... services) {
|
||||||
|
Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> {
|
||||||
|
pool.removeService(s.get());
|
||||||
|
});
|
||||||
|
return Mono.just(pool);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Flux<ServicePool> getServicePools() {
|
||||||
return Flux.fromIterable(servicePools);
|
return Flux.fromIterable(servicePools);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,12 +17,14 @@ import java.util.concurrent.TimeUnit;
|
|||||||
public final class ServicePool {
|
public final class ServicePool {
|
||||||
private final Set<IService> associatedServices;
|
private final Set<IService> associatedServices;
|
||||||
private final Scheduler scheduler;
|
private final Scheduler scheduler;
|
||||||
private final ExecutorService executor;
|
|
||||||
|
|
||||||
public ServicePool() {
|
public ServicePool(boolean multithreaded) {
|
||||||
this.associatedServices = new HashSet<>();
|
this.associatedServices = new HashSet<>();
|
||||||
this.executor = Executors.newSingleThreadExecutor();
|
if (multithreaded) {
|
||||||
this.scheduler = Schedulers.fromExecutorService(executor);
|
this.scheduler = Schedulers.fromExecutorService(Executors.newFixedThreadPool(4));
|
||||||
|
} else {
|
||||||
|
this.scheduler = Schedulers.fromExecutorService(Executors.newSingleThreadExecutor());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void addService(IService service) {
|
void addService(IService service) {
|
||||||
@ -38,6 +40,22 @@ public final class ServicePool {
|
|||||||
return associatedServices;
|
return associatedServices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Mono<Disposable> startService(int serviceID) {
|
||||||
|
Mono<IService> 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<Disposable> startServices() {
|
public Flux<Disposable> startServices() {
|
||||||
return Mono.just(getAssociatedServices()).flatMapMany(services -> {
|
return Mono.just(getAssociatedServices()).flatMapMany(services -> {
|
||||||
Set<Disposable> disposables = new HashSet<>();
|
Set<Disposable> disposables = new HashSet<>();
|
||||||
@ -47,10 +65,6 @@ public final class ServicePool {
|
|||||||
service.getDelay() * 5,
|
service.getDelay() * 5,
|
||||||
service.getPeriod() * 5,
|
service.getPeriod() * 5,
|
||||||
TimeUnit.MILLISECONDS));
|
TimeUnit.MILLISECONDS));
|
||||||
} else if (service.isDelayed()) {
|
|
||||||
disposables.add(scheduler.schedule(service,
|
|
||||||
service.getDelay() * 5,
|
|
||||||
TimeUnit.MILLISECONDS));
|
|
||||||
} else {
|
} else {
|
||||||
disposables.add(scheduler.schedule(service));
|
disposables.add(scheduler.schedule(service));
|
||||||
}
|
}
|
||||||
@ -61,11 +75,11 @@ public final class ServicePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Mono<Void> stopServices(Flux<Disposable> disposableThread) {
|
public Mono<Void> stopServices(Flux<Disposable> disposableThread) {
|
||||||
return Mono.just(getAssociatedServices()).doOnNext(services -> {
|
return disposableThread.doOnNext(Disposable::dispose).then();
|
||||||
for (IService service : services) {
|
}
|
||||||
disposableThread.doOnNext(Disposable::dispose);
|
|
||||||
}
|
public Mono<Void> stopService(int serviceID) {
|
||||||
}).then();
|
return getService(serviceID).doOnNext(IService::stop).then();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Mono<IService> getService(int serviceID) {
|
public Mono<IService> getService(int serviceID) {
|
||||||
@ -78,8 +92,8 @@ public final class ServicePool {
|
|||||||
getAssociatedServices().remove(service);
|
getAssociatedServices().remove(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ServicePool recycle() {
|
public Mono<ServicePool> recycle() {
|
||||||
this.getAssociatedServices().clear();
|
this.getAssociatedServices().clear();
|
||||||
return this;
|
return Mono.just(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user