replace singletons
parent
eadf1eaf5b
commit
0477142233
@ -1,30 +1,32 @@
|
||||
package eu.jonahbauer.chat.server;
|
||||
|
||||
import eu.jonahbauer.chat.server.bot.ChatBotSupervisor;
|
||||
import eu.jonahbauer.chat.server.management.impl.ChatBotManager;
|
||||
import eu.jonahbauer.chat.server.management.impl.SocketManager;
|
||||
import eu.jonahbauer.chat.server.socket.SocketSupervisor;
|
||||
import eu.jonahbauer.chat.server.util.Lazy.MutableLazy;
|
||||
|
||||
import javax.management.JMException;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
// initialize ChatBotManager and SocketManager
|
||||
var _ = ChatBotManager.INSTANCE;
|
||||
var _ = SocketManager.INSTANCE;
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException, JMException {
|
||||
var config = Config.load();
|
||||
ChatBotSupervisor.INSTANCE.start(config);
|
||||
SocketSupervisor.INSTANCE.start(config);
|
||||
var chatBotSupervisorLazy = new MutableLazy<ChatBotSupervisor>();
|
||||
var socketSupervisorLazy = new MutableLazy<SocketSupervisor>();
|
||||
try (
|
||||
var chatBotSupervisor = chatBotSupervisorLazy.set(new ChatBotSupervisor(socketSupervisorLazy));
|
||||
var socketSupervisor = socketSupervisorLazy.set(new SocketSupervisor(chatBotSupervisorLazy))
|
||||
) {
|
||||
chatBotSupervisor.start(config);
|
||||
socketSupervisor.start(config);
|
||||
|
||||
try {
|
||||
// keep main thread running
|
||||
new CountDownLatch(1).await();
|
||||
} catch (InterruptedException e) {
|
||||
SocketSupervisor.INSTANCE.stop();
|
||||
ChatBotSupervisor.INSTANCE.stop();
|
||||
} catch (InterruptedException _) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package eu.jonahbauer.chat.server.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public interface Lazy<T> {
|
||||
@NotNull T get();
|
||||
|
||||
class MutableLazy<T> implements Lazy<T> {
|
||||
private T value;
|
||||
|
||||
@Override
|
||||
public @NotNull T get() {
|
||||
var value = this.value;
|
||||
if (value == null) throw new IllegalStateException();
|
||||
return value;
|
||||
}
|
||||
|
||||
public @NotNull T set(@NotNull T value) {
|
||||
if (this.value != null) throw new IllegalStateException();
|
||||
this.value = Objects.requireNonNull(value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue