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

io.castled.pubsub.MessageSubscriber Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package io.castled.pubsub;

import com.google.common.collect.Maps;
import com.google.inject.Singleton;
import io.castled.pubsub.registry.Message;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@Singleton
public class MessageSubscriber {
    private final Map> messageListeners = Maps.newConcurrentMap();

    public void subscribe(MessageType messageType, MessageListener messageListener) {
        this.messageListeners.computeIfAbsent(messageType, key -> new CopyOnWriteArrayList<>());
        this.messageListeners.get(messageType).add(messageListener);
    }

    public void consumeMessage(Message message) {
        for (MessageListener messageListener : messageListeners.get(message.getType())) {
            messageListener.handleMessage(message);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy