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

@ -1,14 +1,13 @@
package de.kentoj.scrowlib.instancemanager;
import de.kentoj.scrowlib.messaging.MessageBroker;
import de.kentoj.scrowlib.messaging.RemoteException;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
import org.jspecify.annotations.Nullable;
import java.util.List;
import static de.kentoj.scrowlib.utils.ResultUtils.unwrap;
@RequiredArgsConstructor
public class InstanceManagerImpl implements InstanceManager {
@ -16,31 +15,34 @@ public class InstanceManagerImpl implements InstanceManager {
@Override
public ServerInstance deployInstance(String template) {
var result = messageBroker.request("instance.deploy", new Document("template", template));
var document = unwrap(result, "failed to deploy instance of " + template);
var document = messageBroker.request("instance.deploy", new Document("template", template));
return ServerInstance.fromDocument(document);
}
@Override
public boolean destroyInstance(String handle) {
var result = messageBroker.request("instance.destroy", new Document("handle", handle));
return unwrap(result.mapSuccess(__ -> true)
.recover(this::wasNotFound, __ -> false), "failed to destroy instance " + handle);
try {
messageBroker.request("instance.destroy", new Document("handle", handle));
return true;
} catch (RemoteException ex) {
if (wasNotFound(ex.getMessage())) return false;
throw ex;
}
}
@Override
public List<ServerInstance> getInstances() {
var result = messageBroker.request("instance.list", new Document());
return unwrap(result.mapSuccess(doc -> doc.getList("instances", Document.class)
.stream().map(ServerInstance::fromDocument).toList()),
"failed to get instances");
var document = messageBroker.request("instance.list", new Document());
return document.getList("instances", Document.class)
.stream()
.map(ServerInstance::fromDocument)
.toList();
}
@Override
public @Nullable ServerInstance getInstance(String handle) {
var result = messageBroker.request("instance.get", new Document("handle", handle));
return unwrap(result.mapSuccess(ServerInstance::fromDocument)
.recover(this::wasNotFound, __ -> null), "failed to get instance " + handle);
var document = messageBroker.request("instance.get", new Document("handle", handle));
return ServerInstance.fromDocument(document);
}
private boolean wasNotFound(String failure) {

View file

@ -0,0 +1,52 @@
package de.kentoj.scrowlib.messaging;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
@Slf4j
public class InMemoyMessageBroker implements MessageBroker {
private final Map<String, List<Consumer<Document>>> subscribers = new HashMap<>();
private final Map<String, RequestHandler> handlers = new HashMap<>();
@Override
public void publish(String subject, Document document) {
var subs = subscribers.get(subject);
if (subs == null) return;
subs.forEach(s -> s.accept(document));
}
@Override
public void subscribe(String subject, Consumer<Document> consumer) {
subscribers.computeIfAbsent(subject, __ -> new ArrayList<>())
.add(consumer);
}
@Override
public void handle(String subject, RequestHandler handler) {
var oldValue = handlers.put(subject, handler);
if (oldValue != null) log.warn("handler overwrites previous handler for subject " + subject);
}
@Override
public Document request(String subject, Document document) {
var handler = handlers.get(subject);
if (handler == null)
throw new RuntimeException("No request handler for " + subject + " found");
try {
return handler.handle(document).get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -1,84 +1,41 @@
package de.kentoj.scrowlib.messaging;
import com.leakyabstractions.result.api.Result;
import io.nats.client.Connection;
import io.nats.client.Dispatcher;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
@Slf4j
public class MessageBroker {
private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
private final Connection nats;
protected final Dispatcher dispatcher;
public MessageBroker(Connection nats) {
this.nats = nats;
this.dispatcher = nats.createDispatcher();
}
public interface MessageBroker {
/**
* Runs blocking.
* publishes to the message broker
*
* @param subject <a href="https://docs.nats.io/nats-concepts/subjects">subject</a> to publish to
*/
public void publish(String subject, Document document) {
nats.publish(subject, DocumentRepository.toBytes(document));
}
void publish(String subject, Document document);
/**
* Runs blocking.
* publishes to the message broker
* subscribes to a subject, consumer may run asynchronously
*
* @param subject <a href="https://docs.nats.io/nats-concepts/subjects">subject</a> to publish to
*/
public void publish(String subject, Result<Document, String> result) {
this.publish(subject, ResultRepository.toDocument(result));
}
void subscribe(String subject, Consumer<Document> consumer);
/**
* subscribes to a subject, handler may run asynchronously
* Registers a handler for a subject, the handler will receive requests and compute some result.
* The result is sent back as a reply.
*
* @param subject <a href="https://docs.nats.io/nats-concepts/subjects">subject</a> to publish to
*/
public void subscribe(String subject, Consumer<Document> handler) {
dispatcher.subscribe(subject, msg ->
executor.submit(() -> handler.accept(DocumentRepository.fromMessage(msg))));
}
/**
* handler may run asynchronously
*/
public void handle(String subject, RequestHandler handler) {
dispatcher.subscribe(subject, msg -> {
if (msg.getReplyTo() == null) {
log.warn("request in {} has replyTo unset; ignoring", subject);
return;
}
handler.handle(DocumentRepository.fromMessage(msg))
.thenAccept(result ->
executor.submit(() -> publish(msg.getReplyTo(), result))
);
});
}
void handle(String subject, RequestHandler handler);
/**
* Runs blocking.
* Executes a RPC call: The document is published and this method starts listening for incoming
* Executes an RPC call: The document is published and this method starts listening for incoming
* replies. The returned Result holds either a document with the requested data, or an error as a string
*/
public Result<Document, String> request(String subject, Document document) {
var response = nats.request(subject, DocumentRepository.toBytes(document)).join();
return ResultRepository.fromDocument(DocumentRepository.fromMessage(response));
}
public static <S> S unwrap(Result<S, String> result, String message) {
Optional<String> err = result.getFailure();
if (err.isPresent())
throw new RuntimeException(message + ": " + err.orElseThrow());
return result.getSuccess().orElseThrow();
}
*
* @param subject <a href="https://docs.nats.io/nats-concepts/subjects">subject</a> to publish to
* @return RemoteException if the remote throws an exception while handling the request
*/
Document request(String subject, Document document);
}

View file

@ -0,0 +1,78 @@
package de.kentoj.scrowlib.messaging;
import com.leakyabstractions.result.api.Result;
import com.leakyabstractions.result.core.Results;
import de.kentoj.scrowlib.utils.DocumentRepository;
import io.nats.client.Connection;
import io.nats.client.Dispatcher;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
@Slf4j
public class NatsMessageBroker implements MessageBroker {
private static final ExecutorService EXECUTOR = Executors.newVirtualThreadPerTaskExecutor();
private final Connection nats;
private final Dispatcher dispatcher;
public NatsMessageBroker(Connection nats) {
this.nats = nats;
this.dispatcher = nats.createDispatcher();
}
@Override
public void publish(String subject, Document document) {
nats.publish(subject, DocumentRepository.toBytes(document));
}
@Override
public void subscribe(String subject, Consumer<Document> consumer) {
dispatcher.subscribe(subject, msg ->
EXECUTOR.submit(() -> consumer.accept(DocumentRepository.fromBytes(msg.getData()))));
}
@Override
public void handle(String subject, RequestHandler handler) {
dispatcher.subscribe(subject, msg -> {
if (msg.getReplyTo() == null) {
log.warn("request in {} has replyTo unset; ignoring", subject);
return;
}
Result<Document, String> result;
try {
var reply = handler.handle(DocumentRepository.fromBytes(msg.getData())).get();
result = Results.success(reply);
} catch (ExecutionException ex) {
result = Results.failure(ex.getCause().getMessage());
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw ex;
}
final var finalResult = result;
EXECUTOR.submit(() -> publish(msg.getReplyTo(), ResultRepository.toDocument(finalResult)));
});
}
@Override
public Document request(String subject, Document document) {
var reply = nats.request(subject, DocumentRepository.toBytes(document)).join();
var result = ResultRepository.fromDocument(DocumentRepository.fromBytes(reply.getData()));
return unwrap(result);
}
@SuppressWarnings("OptionalGetWithoutIsPresent")
private static <S> S unwrap(Result<S, String> result) {
Optional<String> err = result.getFailure();
if (err.isPresent())
throw new RemoteException(err.get());
return result.getSuccess().get();
}
}

View file

@ -1,16 +1,15 @@
package de.kentoj.scrowlib.messaging;
import com.leakyabstractions.result.api.Result;
import org.bson.Document;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Future;
@FunctionalInterface
public interface RequestHandler {
/**
* Processes a request and replies
*
* @return result to send back to the requesting client.
* @return document to reply with
*/
CompletionStage<Result<Document, String>> handle(Document doc);
Future<Document> handle(Document doc);
}

View file

@ -8,15 +8,15 @@ import org.bson.Document;
@NoArgsConstructor(access = AccessLevel.NONE)
public class ResultRepository {
class ResultRepository {
public static Document toDocument(Result<Document, String> result) {
static Document toDocument(Result<Document, String> result) {
return result.hasSuccess()
? new Document("data", result.getSuccess().orElseThrow())
: new Document("error", result.getFailure().orElseThrow());
}
public static Result<Document, String> fromDocument(Document doc) {
static Result<Document, String> fromDocument(Document doc) {
var error = doc.getString("error");
return error != null
? Results.failure(error)

View file

@ -1,17 +1,16 @@
package de.kentoj.scrowlib.messaging;
import com.leakyabstractions.result.api.Result;
import org.bson.Document;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Future;
@FunctionalInterface
public interface SyncRequestHandler extends RequestHandler{
Result<Document, String> handleSync(Document doc);
public interface SyncRequestHandler extends RequestHandler {
Document handleSync(Document doc);
@Override
default CompletionStage<Result<Document, String>> handle(Document doc) {
default Future<Document> handle(Document doc) {
return CompletableFuture.completedFuture(handleSync(doc));
}
}

View file

@ -1,5 +1,6 @@
package de.kentoj.scrowlib.player;
import de.kentoj.scrowlib.messaging.MessageBroker;
import net.kyori.adventure.audience.Audience;
/**
@ -7,15 +8,15 @@ import net.kyori.adventure.audience.Audience;
* NetworkPlayer represents a Player that MAY be online on any of the minecraft, wrapped
* in an {@link Audience}
* servers in the network.
*
* <p>
* If the player is not online, these methods DO NOTHING.
* Most methods here do nothing even if the Player is valid(as is the nature of Audiences)
*
* this interface should be used mainly for basic communication(e.g sending messages).
* <p>
* this interface should be used mainly for basic communication(e.g. sending messages).
* For more complex operations, use the {@link MessageBroker}.
*
* @see {@link NetworkPlayerImpl}
* @see {@link MessageBroker}
* @see NetworkPlayerImpl
* @see MessageBroker
*/
public interface NetworkPlayer extends Audience {

View file

@ -1,6 +1,5 @@
package de.kentoj.scrowlib.messaging;
package de.kentoj.scrowlib.utils;
import io.nats.client.Message;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.bson.Document;
@ -10,8 +9,8 @@ import org.jetbrains.annotations.ApiStatus;
public class DocumentRepository {
@ApiStatus.Obsolete
public static Document fromMessage(Message message) {
return Document.parse(new String(message.getData()));
public static Document fromBytes(byte[] bytes) {
return Document.parse(new String(bytes));
}
@ApiStatus.Obsolete

View file

@ -1,11 +0,0 @@
package de.kentoj.scrowlib.utils;
import com.leakyabstractions.result.api.Result;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.Optional;
@NoArgsConstructor(access = AccessLevel.NONE)
public class ResultUtils {
}