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

org.graylog2.plugin.buffers.MessageEvent Maven / Gradle / Ivy

There is a newer version: 5.2.7
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog2.plugin.buffers;

import com.google.common.base.MoreObjects;
import com.lmax.disruptor.EventFactory;
import org.graylog2.plugin.Message;
import org.graylog2.plugin.journal.RawMessage;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;

/**
 * @author Lennart Koopmann 
 */
public class MessageEvent {

    public final static EventFactory EVENT_FACTORY = new EventFactory() {
        @Override
        public MessageEvent newInstance()
        {
            return new MessageEvent();
        }
    };

    private RawMessage raw;
    private Message msg;
    private Collection messages;

    public boolean isSingleMessage() {
        return msg != null;
    }

    @Nullable
    public Message getMessage()
    {
        return msg;
    }

    public void setMessage(@Nullable final Message msg)
    {
        this.msg = msg;
    }

    @Nullable
    public Collection getMessages() {
        return messages;
    }

    public void setMessages(@Nullable final Collection messages) {
        this.messages = messages;
    }

    public void clearMessages() {
        setMessage(null);
        setMessages(null);
    }

    /**
     * Sets the raw message but also clears out the {@link #getMessage() message} and {@link #getMessages() messages}
     * references to avoid handling stale messages and to let older messages be garbage collected earlier.
     *
     * @param raw
     */
    public void setRaw(@Nonnull RawMessage raw) {
        this.raw = raw;
        clearMessages();
    }

    public void clearRaw() {
        this.raw = null;
    }

    @Nonnull
    public RawMessage getRaw() {
        return raw;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("raw", raw)
                .add("message", msg)
                .add("messages", messages)
                .toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy