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

io.deepsense.neptune.clientlibrary.threads.ChannelValueSendingThread Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.threads;

import com.google.common.base.Preconditions;
import io.deepsense.neptune.apiclient.model.ChannelValues;
import io.deepsense.neptune.clientlibrary.config.internal.ChannelValueSendingThreadConfiguration;
import io.deepsense.neptune.clientlibrary.services.channelvaluesender.ChannelValueSender;
import io.deepsense.neptune.clientlibrary.services.channelvaluesender.ChannelValueSendingProxy;
import io.deepsense.neptune.clientlibrary.utils.ThreadBrowser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import static io.deepsense.neptune.clientlibrary.api.JsonSerializationUtils.serializeSwaggerModel;

public class ChannelValueSendingThread extends JobInternalThread implements ChannelValueSendingProxy {

    public static final String CHANNEL_VALUE_SENDING_THREAD_NAME = "channel-value-sending-thread";

    private static final Logger logger = LoggerFactory.getLogger(ChannelValueSendingThread.class);

    private static final int IDLE_TIME_MILLIS = 100;

    private final BlockingQueue sendRequestsQueue;

    private final ChannelValueSender channelValueSender;

    public ChannelValueSendingThread(
            ThreadBrowser threadBrowser,
            ChannelValueSender channelValueSender,
            ChannelValueSendingThreadConfiguration channelValueSendingThreadConfiguration) {
        super(threadBrowser, CHANNEL_VALUE_SENDING_THREAD_NAME, false);
        this.sendRequestsQueue = new LinkedBlockingQueue<>(
                Preconditions.checkNotNull(channelValueSendingThreadConfiguration).getSendRequestsQueueSize());
        this.channelValueSender = Preconditions.checkNotNull(channelValueSender);
    }

    @Override
    public void delegateSend(ChannelValues channelValuePackage) {
        boolean queueFull = !sendRequestsQueue.offer(channelValuePackage);
        if (queueFull) {
            logger.warn("The message queue has been filled up! Skipping channel value: %s",
                    serializeSwaggerModel(channelValuePackage));
        }
    }

    @Override
    public void run() {
        while (!interrupted() && areOtherThreadsAlive()) {
            try {
                ChannelValues channelValuePackageToSend = sendRequestsQueue.poll(
                        IDLE_TIME_MILLIS,
                        TimeUnit.MILLISECONDS);
                if (channelValuePackageToSend != null) {
                    channelValueSender.delegateSend(channelValuePackageToSend);
                } else {
                    channelValueSender.flush();
                }
            } catch (InterruptedException exc) {
                Thread.currentThread().interrupt();
            } catch (Exception exc) {
                logger.error("Sending channel values failed!", exc);
            }
        }
        sendRemainingMessages();
    }

    private void sendRemainingMessages() {
        sendRequestsQueue.forEach(channelValueSender::delegateSend);
        channelValueSender.flush();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy