dev.soffa.foundation.pubsub.nats.NatsMessageHandler Maven / Gradle / Ivy
package dev.soffa.foundation.pubsub.nats;
import dev.soffa.foundation.commons.Logger;
import dev.soffa.foundation.commons.Mappers;
import dev.soffa.foundation.commons.TextUtil;
import dev.soffa.foundation.error.ManagedException;
import dev.soffa.foundation.message.MessageResponse;
import io.nats.client.Connection;
import io.nats.client.Message;
import io.nats.client.MessageHandler;
import lombok.AllArgsConstructor;
import java.util.Optional;
@AllArgsConstructor
public class NatsMessageHandler implements MessageHandler {
private static final Logger LOG = Logger.get(NatsMessageHandler.class);
private final Connection connection;
private final dev.soffa.foundation.message.MessageHandler handler;
private boolean accept(Message msg) {
if (msg == null) {
return false;
}
if (msg.isStatusMessage()) {
return false;
}
return msg.getData() != null;
}
@Override
public void onMessage(Message msg) {
if (!accept(msg)) {
return;
}
boolean sendReply = !msg.isJetStream() && TextUtil.isNotEmpty(msg.getReplyTo());
dev.soffa.foundation.message.Message message;
try {
message = Mappers.JSON_DEFAULT.deserialize(msg.getData(), dev.soffa.foundation.message.Message.class);
} catch (Exception e) {
//TODO: handle lost payloads (audit)
LOG.error(e, "Invalid payload, message will be discarded -- %s", e.getMessage());
return;
}
if (message == null) {
return;
}
try {
Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy