reworked MessageBroker, now also with in-memory implementation

This commit is contained in:
kento2 2026-07-07 15:05:36 +02:00
parent d9baa4113e
commit 7e5bd1bb6d
15 changed files with 249 additions and 157 deletions

View file

@ -12,7 +12,8 @@ import de.kentoj.scrowlib.friends.friendship.PostgresFriendshipService;
import de.kentoj.scrowlib.friends.request.InMemoryFriendRequestService;
import de.kentoj.scrowlib.friends.request.PostgresFriendRequestService;
import de.kentoj.scrowlib.instancemanager.InstanceManagerImpl;
import de.kentoj.scrowlib.messaging.MessageBroker;
import de.kentoj.scrowlib.messaging.InMemoyMessageBroker;
import de.kentoj.scrowlib.messaging.NatsMessageBroker;
import de.kentoj.scrowlib.player.NetworkPlayerImpl;
import de.kentoj.scrowlib.utils.EnvUtils;
import io.nats.client.Nats;
@ -33,25 +34,33 @@ public class ScrowAPISurface {
public static void initScrowAPI(Plugin plugin) {
ScrowAPI.setCorePlugin(plugin);
try {
String natsHost = EnvUtils.envOrThrow("HOST_NATS");
var options = Options.builder()
.server(natsHost)
.pedantic()
.build();
ScrowAPI.setMessageBroker(new MessageBroker(Nats.connect(options)));
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
var natsHost = System.getenv("HOST_NATS");
if (natsHost!= null) {
try {
var options = Options.builder()
.server(natsHost)
.pedantic()
.build();
ScrowAPI.setMessageBroker(new NatsMessageBroker(Nats.connect(options)));
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
} else {
log.error("HOST_NATS not set -> Using in-memory dummy message broker");
ScrowAPI.setMessageBroker(new InMemoyMessageBroker());
}
try {
Class.forName("org.postgresql.Driver", true, ScrowAPISurface.class.getClassLoader());
var props = new Properties();
props.setProperty("user", "postgres");
ScrowAPI.setJdbi(Jdbi.create(EnvUtils.envOrThrow("HOST_POSTGRES"), props));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (IllegalStateException e) {
var hostPostgres = System.getenv().get("HOST_POSTGRES");
if (hostPostgres != null) {
try {
Class.forName("org.postgresql.Driver", true, ScrowAPISurface.class.getClassLoader());
var props = new Properties();
props.setProperty("user", "postgres");
ScrowAPI.setJdbi(Jdbi.create(hostPostgres));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
log.error("HOST_POSTGRES not set -> Using in-memory database");
}