io.deepsense.neptune.clientlibrary.threads.ChannelValueSendingThread Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neptune-client-library Show documentation
Show all versions of neptune-client-library Show documentation
Enables integration with Neptune in your Java code
/**
* Copyright (c) 2016, CodiLime Inc.
*/
package io.deepsense.neptune.clientlibrary.threads;
import com.google.common.base.Preconditions;
import io.deepsense.neptune.apiclient.model.InputChannelValues;
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(InputChannelValues 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 (!isShutdownRequested() && areOtherThreadsAlive()) {
try {
InputChannelValues channelValuePackageToSend = sendRequestsQueue.poll(
IDLE_TIME_MILLIS,
TimeUnit.MILLISECONDS);
if (channelValuePackageToSend != null) {
channelValueSender.delegateSend(channelValuePackageToSend);
} else {
channelValueSender.flush();
}
} 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