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

org.enodeframework.queue.applicationmessage.DefaultApplicationMessagePublisher Maven / Gradle / Ivy

There is a newer version: 1.1.10
Show newest version
package org.enodeframework.queue.applicationmessage;

import org.enodeframework.common.serializing.ISerializeService;
import org.enodeframework.common.utilities.Ensure;
import org.enodeframework.messaging.IApplicationMessage;
import org.enodeframework.messaging.IMessagePublisher;
import org.enodeframework.queue.ISendMessageService;
import org.enodeframework.queue.QueueMessage;

import java.util.concurrent.CompletableFuture;

public class DefaultApplicationMessagePublisher implements IMessagePublisher {

    private final String topic;

    private final String tag;

    private final ISendMessageService producer;

    private final ISerializeService serializeService;

    public DefaultApplicationMessagePublisher(String topic, String tag, ISendMessageService producer, ISerializeService serializeService) {
        this.topic = topic;
        this.tag = tag;
        this.producer = producer;
        this.serializeService = serializeService;
    }

    protected QueueMessage createApplicationMessage(IApplicationMessage message) {
        Ensure.notNull(topic, "topic");
        String appMessageData = serializeService.serialize(message);
        ApplicationDataMessage appDataMessage = new ApplicationDataMessage(appMessageData, message.getClass().getName());
        String data = serializeService.serialize(appDataMessage);
        String routeKey = message.getId();
        QueueMessage queueMessage = new QueueMessage();
        queueMessage.setBody(data);
        queueMessage.setRouteKey(routeKey);
        queueMessage.setKey(message.getId());
        queueMessage.setTopic(topic);
        queueMessage.setTag(tag);
        return queueMessage;
    }

    @Override
    public CompletableFuture publishAsync(IApplicationMessage message) {
        return producer.sendMessageAsync(createApplicationMessage(message));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy