com.xxdb.streaming.client.TopicPoller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-java Show documentation
Show all versions of api-java Show documentation
The messaging and data conversion protocol between Java and DolphinDB server
package com.xxdb.streaming.client;
import com.xxdb.streaming.client.IMessage;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class TopicPoller {
BlockingQueue> queue;
ArrayList cache = null;
public TopicPoller(BlockingQueue> queue) {
this.queue = queue;
}
public void setQueue(BlockingQueue> queue) {
this.queue = queue;
}
private void fillCache(long timeout) {
assert (cache == null);
List list = null;
if (cache == null) {
try {
if (timeout >= 0)
list = queue.poll(timeout, TimeUnit.MILLISECONDS);
else
list = queue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (list != null) {
cache = new ArrayList<>(list.size());
cache.addAll(list);
}
}
public ArrayList poll(long timeout) {
if (cache == null) {
fillCache(timeout);
}
ArrayList cachedMessages = cache;
cache = null;
return cachedMessages;
}
// take one message from the topic, block if necessary
public IMessage take() {
if (cache == null)
fillCache(-1);
return cache.remove(0);
}
}