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

org.reactivecommons.async.rabbit.RabbitDomainEventBus Maven / Gradle / Ivy

package org.reactivecommons.async.rabbit;

import io.cloudevents.CloudEvent;
import org.reactivecommons.api.domain.DomainEvent;
import org.reactivecommons.api.domain.DomainEventBus;
import org.reactivecommons.async.commons.config.BrokerConfig;
import org.reactivecommons.async.rabbit.communications.ReactiveMessageSender;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;

import java.util.Collections;

public class RabbitDomainEventBus implements DomainEventBus {

    private final ReactiveMessageSender sender;
    private final String exchange;
    private final boolean persistentEvents;

    public RabbitDomainEventBus(ReactiveMessageSender sender, String exchange) {
        this(sender, exchange, new BrokerConfig());
    }

    public RabbitDomainEventBus(ReactiveMessageSender sender, String exchange, BrokerConfig config) {
        this.sender = sender;
        this.exchange = exchange;
        persistentEvents = config.isPersistentEvents();
    }

    @Override
    public  Mono emit(DomainEvent event) {
        return sender.sendWithConfirm(event, exchange, event.getName(), Collections.emptyMap(), persistentEvents)
                .onErrorMap(err -> new RuntimeException("Event send failure: " + event.getName(), err));
    }

    @Override
    public  Publisher emit(String domain, DomainEvent event) {
        throw new UnsupportedOperationException("Not implemented yet");
    }

    @Override
    public Publisher emit(CloudEvent cloudEvent) {
        return sender.sendWithConfirm(cloudEvent, exchange, cloudEvent.getType(),
                        Collections.emptyMap(), persistentEvents)
                .onErrorMap(err -> new RuntimeException("Event send failure: " + cloudEvent.getType(), err));
    }

    @Override
    public Publisher emit(String domain, CloudEvent event) {
        throw new UnsupportedOperationException("Not implemented yet");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy