mirror of
https://github.com/SimplexDevelopment/SimplexSS.git
synced 2024-12-22 05:07:36 +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 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;
|
||||
|
@ -8,8 +8,6 @@ import java.util.concurrent.RunnableScheduledFuture;
|
||||
public interface IService extends RunnableScheduledFuture<IService> {
|
||||
int getServiceID();
|
||||
|
||||
boolean isDelayed();
|
||||
|
||||
boolean isRepeating();
|
||||
|
||||
long getPeriod();
|
||||
|
@ -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<ServicePool> 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<ServicePool> addToExistingPool(@NotNull ServicePool pool, IService... services) {
|
||||
Stream.of(services).forEach(pool::addService);
|
||||
public @NotNull Mono<ServicePool> 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<ServicePool> takeFromExistingPool(@NotNull ServicePool pool, IService... services) {
|
||||
Stream.of(services).forEach(pool::removeService);
|
||||
public @NotNull Mono<ServicePool> addToExistingPool(@NotNull ServicePool pool, IService... services) {
|
||||
Flux.fromIterable(Arrays.asList(services)).doOnEach(s -> {
|
||||
pool.addService(s.get());
|
||||
});
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -17,12 +17,14 @@ import java.util.concurrent.TimeUnit;
|
||||
public final class ServicePool {
|
||||
private final Set<IService> 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<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() {
|
||||
return Mono.just(getAssociatedServices()).flatMapMany(services -> {
|
||||
Set<Disposable> 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<Void> stopServices(Flux<Disposable> disposableThread) {
|
||||
return Mono.just(getAssociatedServices()).doOnNext(services -> {
|
||||
for (IService service : services) {
|
||||
disposableThread.doOnNext(Disposable::dispose);
|
||||
}
|
||||
}).then();
|
||||
return disposableThread.doOnNext(Disposable::dispose).then();
|
||||
}
|
||||
|
||||
public Mono<Void> stopService(int serviceID) {
|
||||
return getService(serviceID).doOnNext(IService::stop).then();
|
||||
}
|
||||
|
||||
public Mono<IService> getService(int serviceID) {
|
||||
@ -78,8 +92,8 @@ public final class ServicePool {
|
||||
getAssociatedServices().remove(service);
|
||||
}
|
||||
|
||||
public ServicePool recycle() {
|
||||
public Mono<ServicePool> recycle() {
|
||||
this.getAssociatedServices().clear();
|
||||
return this;
|
||||
return Mono.just(this);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user