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

it.netgrid.bauer.impl.StreamTopic Maven / Gradle / Ivy

package it.netgrid.bauer.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;

import it.netgrid.bauer.EventHandler;
import it.netgrid.bauer.Topic;
import it.netgrid.bauer.helpers.TopicUtils;

public class StreamTopic implements Topic, StreamMessageConsumer {

    private static final Logger log = LoggerFactory.getLogger(StreamTopic.class);

    private final String name;

    private final List> handlers;

    private final StreamManager manager;

    private final StreamMessageFactory messageFactory;

    public StreamTopic(StreamManager streamManager, StreamMessageFactory messageFactory, String name) {
        this.messageFactory = messageFactory;
        this.manager = streamManager;
        this.handlers = new ArrayList<>();
        this.name = name == null ? "" : name;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public synchronized void addHandler(EventHandler handler) {
        this.handlers.add(handler);
        if (this.handlers.size() == 1) {
            this.manager.addMessageConsumer(this);
        }
    }

    @Override
    public void post(E event) {
        try {
            StreamEvent streamEvent = new StreamEvent(name, event);
            JsonNode message = this.messageFactory.buildMessage(streamEvent);
            this.manager.postMessage(message);
        } catch (IOException e) {
            log.warn(String.format("cannot serialize on %s: %s", this.name, e.getMessage()));
        }

    }

    @Override
    public boolean consume(JsonNode message) {

        try {
            StreamEvent streamEvent = this.messageFactory.buildEvent(message);

            if (TopicUtils.match(this.name, streamEvent.topic())) {
                for (EventHandler handler : this.handlers) {
                    try {
                        StreamEvent event = this.messageFactory.buildEvent(message, handler.getEventClass());
                        handler.handle(event.topic(), event.payload());
                    } catch (Exception e) {
                        log.warn(String.format("error on %s %s: %s", streamEvent.topic(), handler.getName(),
                                streamEvent.payload().toString()));
                    }
                }
            }
        } catch (IOException e) {
            log.warn(String.format("invalid content: %s", message.toString()));
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy