io.github.laskowski.shell.runner.DefaultShellTaskRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shell-commander Show documentation
Show all versions of shell-commander Show documentation
Library to launch .bat and .sh scripts of your choice with different configurations. Allows you to read tasks and services output from terminal
The newest version!
package io.github.laskowski.shell.runner;
import io.github.laskowski.shell.exceptions.ErrorDetectedException;
import io.github.laskowski.shell.output.OutputListener;
import io.github.laskowski.shell.output.Publisher;
import io.github.laskowski.shell.output.StringSubscriber;
import io.github.laskowski.shell.output.service.ServiceReadyStrategy;
import io.github.laskowski.shell.task.ShellTask;
import io.github.rafal.laskowski.wait.Wait;
import java.util.ArrayList;
import java.util.List;
public class DefaultShellTaskRunner implements ShellTaskRunner {
private static DefaultShellTaskRunner taskRunner;
private final List> tasks = new ArrayList<>();
private DefaultShellTaskRunner() {}
public synchronized static DefaultShellTaskRunner getInstance() {
if (taskRunner == null) {
taskRunner = new DefaultShellTaskRunner();
}
return taskRunner;
}
@Override
public void run(ShellTask shellTask, OutputListener outputListener, ServiceReadyStrategy serviceReadyStrategy) {
Publisher publisher = outputListener.getPublisher();
StringSubscriber subscriber = outputListener.getSubscriber();
Thread outputListenerThread = new Thread(() -> {
Process process = shellTask.getProcess();
publisher.registerSubscriber(subscriber);
publisher.startPublishing(process);
});
outputListenerThread.start();
Thread processThread = new Thread(shellTask::start);
processThread.start();
new Wait()
.withTimeout(serviceReadyStrategy.getTimeout())
.withMessage(serviceReadyStrategy.getTimeoutMessage())
.until(() -> {
try {
return subscriber.getLines().stream().anyMatch(serviceReadyStrategy.getReadyPredicate());
} catch (ErrorDetectedException e) {
return shellTask.getErrorHandler().handle(e);
}
});
tasks.add(shellTask);
}
@Override
public void stop(ShellTask shellTask) {
tasks.remove(shellTask);
shellTask.stop();
}
@Override
public void stopAll() {
tasks.forEach(ShellTask::stop);
}
}