All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.com.bancolombia.commons.jms.utils.ReactiveReplyRouter Maven / Gradle / Ivy

The newest version!
package co.com.bancolombia.commons.jms.utils;

import co.com.bancolombia.commons.jms.exceptions.RelatedMessageNotFoundException;
import jakarta.jms.Message;
import lombok.extern.log4j.Log4j2;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;

import java.time.Duration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeoutException;

@Log4j2
public class ReactiveReplyRouter {
    public static final long DEFAULT_TIMEOUT = 30L;
    private final ConcurrentHashMap> processors = new ConcurrentHashMap<>();


    public Mono wait(String messageId) {
        return wait(messageId, Duration.ofSeconds(DEFAULT_TIMEOUT));
    }

    public Mono wait(String messageId, Duration timeout) {
        final Sinks.One processor = Sinks.one();
        processors.put(messageId, processor);
        log.info("Waiting for: {}", messageId);
        return processor.asMono().timeout(timeout).doOnError(TimeoutException.class, e -> clean(messageId));
    }

    public void reply(String correlationID, T response) {
        if (correlationID != null) {
            log.info("Replying with id: {}", correlationID);
            final Sinks.One processor = processors.remove(correlationID);
            if (processor == null) {
                throw new RelatedMessageNotFoundException(correlationID);
            } else {
                processor.tryEmitValue(response);
            }
        }
    }

    public void error(String correlationID, Throwable error) {
        if (correlationID != null) {
            log.info("Replying with id: {}", correlationID);
            final Sinks.One processor = processors.remove(correlationID);
            if (processor == null) {
                throw new RelatedMessageNotFoundException(correlationID);
            } else {
                processor.tryEmitError(error);
            }
        }
    }

    public void clean(String correlationId) {
        processors.remove(correlationId);
    }

    public boolean hasKey(String correlationID) {
        return processors.containsKey(correlationID);
    }

    public Mono remoteReply(String correlationID, Message response) {
        return Mono.error(() ->
                new UnsupportedOperationException("remoteReply not implemented error with "
                        + correlationID + " " + response));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy