replace singletons
parent
eadf1eaf5b
commit
0477142233
@ -1,30 +1,32 @@
|
|||||||
package eu.jonahbauer.chat.server;
|
package eu.jonahbauer.chat.server;
|
||||||
|
|
||||||
import eu.jonahbauer.chat.server.bot.ChatBotSupervisor;
|
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.socket.SocketSupervisor;
|
||||||
|
import eu.jonahbauer.chat.server.util.Lazy.MutableLazy;
|
||||||
|
|
||||||
|
import javax.management.JMException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException, InterruptedException {
|
public static void main(String[] args) throws IOException, InterruptedException, JMException {
|
||||||
// initialize ChatBotManager and SocketManager
|
|
||||||
var _ = ChatBotManager.INSTANCE;
|
|
||||||
var _ = SocketManager.INSTANCE;
|
|
||||||
|
|
||||||
var config = Config.load();
|
var config = Config.load();
|
||||||
ChatBotSupervisor.INSTANCE.start(config);
|
var chatBotSupervisorLazy = new MutableLazy<ChatBotSupervisor>();
|
||||||
SocketSupervisor.INSTANCE.start(config);
|
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 {
|
try {
|
||||||
// keep main thread running
|
// keep main thread running
|
||||||
new CountDownLatch(1).await();
|
new CountDownLatch(1).await();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException _) {
|
||||||
SocketSupervisor.INSTANCE.stop();
|
// ignore
|
||||||
ChatBotSupervisor.INSTANCE.stop();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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