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

com.xxdb.streaming.client.TopicPoller Maven / Gradle / Ivy

Go to download

The messaging and data conversion protocol between Java and DolphinDB server

There is a newer version: 1.0.27
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy