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

com.jn.dmmq.core.SimpleProducer Maven / Gradle / Ivy

The newest version!
package com.jn.dmmq.core;

import com.jn.langx.annotation.NonNull;
import com.jn.langx.annotation.Nullable;
import com.jn.langx.util.Preconditions;
import com.jn.langx.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Just send message to the 'DEFAULT' topic if the topicName is not specified
 *
 * @param 
 */
public class SimpleProducer implements Producer {
    private static final Logger logger = LoggerFactory.getLogger(SimpleProducer.class);
    private TopicAllocator topicAllocator;
    private MessageTopicDispatcher dispatcher;

    public TopicAllocator getTopicAllocator() {
        return topicAllocator;
    }

    public void setTopicAllocator(TopicAllocator topicAllocator) {
        this.topicAllocator = topicAllocator;
    }

    @Override
    public void publish(M message) {
        publish(null, message);
    }

    @Override
    public void publish(@Nullable String topicName, @NonNull M message) {
        Preconditions.checkNotNull(message);
        topicName = getTopic(topicName, message);
        if (Strings.isEmpty(topicName)) {
            logger.warn("not found an invalid topic for the message: {}", message);
            return;
        }
        dispatcher.publish(topicName, message);
    }

    private String getTopic(String topicName, M message) {
        if (Strings.isEmpty(topicName)) {
            return topicAllocator.apply(message);
        }
        return topicName;
    }

    @Override
    public void setMessageTopicDispatcher(MessageTopicDispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }

    @Override
    public MessageTopicDispatcher getMessageTopicDispatcher() {
        return dispatcher;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy