Example usage for com.google.common.collect Queues newLinkedBlockingQueue

List of usage examples for com.google.common.collect Queues newLinkedBlockingQueue

Introduction

In this page you can find the example usage for com.google.common.collect Queues newLinkedBlockingQueue.

Prototype

public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue() 

Source Link

Document

Creates an empty LinkedBlockingQueue with a capacity of Integer#MAX_VALUE .

Usage

From source file:org.apache.streams.test.component.FileReaderProvider.java

private Queue<StreamsDatum> constructQueue(Scanner scanner) {
    Queue<StreamsDatum> data = Queues.newLinkedBlockingQueue();
    while (scanner.hasNextLine()) {
        data.add(converter.convert(scanner.nextLine()));
    }//  w  w w .  j  a v  a  2s.co  m
    cleanUp();
    return data;
}

From source file:org.apache.streams.data.moreover.MoreoverProvider.java

@Override
public synchronized StreamsResultSet readCurrent() {

    LOGGER.debug("readCurrent: {}", providerQueue.size());

    Collection<StreamsDatum> currentIterator = Lists.newArrayList();
    int batchSize = 0;
    BlockingQueue<StreamsDatum> batch = Queues.newLinkedBlockingQueue();
    while (!this.providerQueue.isEmpty() && batchSize < MAX_BATCH_SIZE) {
        StreamsDatum datum = this.providerQueue.poll();
        if (datum != null) {
            ++batchSize;/*ww  w  .j  a v a  2  s .c o m*/
            try {
                batch.put(datum);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(ie);
            }
        }
    }
    return new StreamsResultSet(batch);
}

From source file:org.terasology.world.liquid.LiquidSimulator.java

@Override
public void initialise() {
    world = CoreRegistry.get(WorldProvider.class);
    air = BlockManager.getInstance().getAir();
    grass = BlockManager.getInstance().getBlock("engine:Grass");
    snow = BlockManager.getInstance().getBlock("engine:Snow");
    dirt = BlockManager.getInstance().getBlock("engine:Dirt");
    water = BlockManager.getInstance().getBlock("engine:Water");
    lava = BlockManager.getInstance().getBlock("engine:Lava");

    blockQueue = Queues.newLinkedBlockingQueue();

    executor = Executors.newFixedThreadPool(NUM_THREADS);
    for (int i = 0; i < NUM_THREADS; ++i) {
        executor.execute(new Runnable() {
            @Override//from   www. ja v  a 2s  .co m
            public void run() {
                Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
                while (true) {
                    try {
                        LiquidSimulationTask task = blockQueue.take();
                        if (task.shutdownThread()) {
                            break;
                        }
                        task.run();
                    } catch (InterruptedException e) {
                        logger.debug("Interrupted");
                    } catch (Exception e) {
                        logger.error("Error in water simulation", e);
                    }
                }
            }
        });
    }
}

From source file:org.terasology.logic.world.liquid.LiquidSimulator.java

@Override
public void initialise() {
    world = CoreRegistry.get(WorldProvider.class);
    air = BlockManager.getInstance().getAir();
    grass = BlockManager.getInstance().getBlock("Grass");
    snow = BlockManager.getInstance().getBlock("Snow");
    dirt = BlockManager.getInstance().getBlock("Dirt");
    water = BlockManager.getInstance().getBlock("Water");
    lava = BlockManager.getInstance().getBlock("Lava");

    blockQueue = Queues.newLinkedBlockingQueue();

    executor = Executors.newFixedThreadPool(NUM_THREADS);
    for (int i = 0; i < NUM_THREADS; ++i) {
        executor.execute(new Runnable() {
            @Override//from   w  w  w.ja v a2s  .  com
            public void run() {
                Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
                while (true) {
                    try {
                        LiquidSimulationTask task = blockQueue.take();
                        if (task.shutdownThread()) {
                            break;
                        }
                        task.run();
                    } catch (InterruptedException e) {
                        logger.log(Level.INFO, "Interrupted");
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, "Error in water simulation", e);
                    }
                }
            }
        });
    }
}

From source file:org.apache.drill.test.framework.ConnectionPool.java

public synchronized Connection getOrCreateConnection(String username, String password) throws SQLException {
    final String key = username + password;
    if (connections.containsKey(key)) {
        final Connection connection = connections.get(key).poll();
        if (connection == null) {
            return DriverManager.getConnection(URL_STRING, username, password);
        } else {//  w  ww  .ja v  a 2  s  .  co  m
            return connection;
        }
    } else {
        final Queue<Connection> connectionQueue = Queues.newLinkedBlockingQueue();
        connections.put(key, connectionQueue);
        return DriverManager.getConnection(URL_STRING, username, password);
    }
}

From source file:org.apache.tajo.worker.NodeStatusUpdater.java

@Override
public void serviceInit(Configuration conf) throws Exception {

    this.systemConf = TUtil.checkTypeAndGet(conf, TajoConf.class);
    this.rpcParams = RpcParameterFactory.get(this.systemConf);
    this.heartBeatRequestQueue = Queues.newLinkedBlockingQueue();
    this.serviceTracker = ServiceTrackerFactory.get(systemConf);
    this.workerContext.getNodeResourceManager().getDispatcher().register(NodeStatusEvent.EventType.class, this);
    this.heartBeatInterval = systemConf.getIntVar(TajoConf.ConfVars.WORKER_HEARTBEAT_IDLE_INTERVAL);
    this.updaterThread = new StatusUpdaterThread();
    this.updaterThread.setName("NodeStatusUpdater");
    super.serviceInit(conf);
}

From source file:com.lithium.flow.compress.ParallelCoder.java

@Override
@Nonnull/* w  w  w .jav a 2s  . c om*/
public OutputStream wrapOut(@Nonnull OutputStream out, int option) throws IOException {
    return new FilterOutputStream(out) {
        private ByteArrayOutputStream buf = new ByteArrayOutputStream();
        private volatile boolean running;
        private BlockingQueue<Future<byte[]>> queue;
        private ExecutorService service;
        private Thread thread;
        private Exception exception;

        @Override
        public void write(int b) throws IOException {
            buf.write(b);
            if (buf.size() > chunkSize) {
                cycle();
            }
        }

        @Override
        public void write(@Nonnull byte[] b) throws IOException {
            buf.write(b);
            if (buf.size() > chunkSize) {
                cycle();
            }
        }

        @Override
        public void write(@Nonnull byte b[], int off, int len) throws IOException {
            buf.write(b, off, len);
            if (buf.size() > chunkSize) {
                cycle();
            }
        }

        @Override
        public void flush() throws IOException {
            cycle();
            running = false;
            service.shutdown();
            Sleep.softly(thread::join);
            if (exception != null) {
                throw new IOException(exception);
            }

            out.flush();
        }

        private void cycle() {
            if (!running) {
                running = true;
                queue = Queues.newLinkedBlockingQueue();
                service = Executors.newFixedThreadPool(threads);
                thread = new Thread(this::run);
                thread.start();
            }

            byte[] bytes = buf.toByteArray();
            buf = new ByteArrayOutputStream();

            queue.add(service.submit(() -> {
                ByteArrayOutputStream compressBuf = new ByteArrayOutputStream();
                OutputStream compressOut = delegate.wrapOut(compressBuf, option);
                compressOut.write(bytes);
                compressOut.close();
                return compressBuf.toByteArray();
            }));
        }

        private void run() {
            while (running || queue.size() > 0) {
                try {
                    out.write(queue.take().get());
                } catch (Exception e) {
                    exception = e;
                    break;
                }
            }
        }
    };
}

From source file:com.cyngn.vertx.opentsdb.service.OpenTsDbService.java

@Override
public void start(final Future<Void> startedResult) {
    boolean hasLock = obtainLock();
    if (!hasLock) {
        logger.warn(//from w  ww.  ja  v a 2  s .c om
                "It appears someone has already deployed the verticle you only need one instance initialized");
        startedResult.fail("Multiple instances should not be started of the service");
        return;
    }

    JsonObject config = context.config();
    options = new OpenTsDbOptions(config);
    eventBus = vertx.eventBus();
    metricsParser = new MetricsParser(options.getPrefix(), options.getDefaultTags(), this::sendError);

    // create the list of workers
    workers = new ArrayList<>(options.getHosts().size());
    metrics = Queues.newLinkedBlockingQueue();

    initializeWorkers(startedResult);
    createMessageHandlers();

    eventBus.consumer(options.getAddress(), this);
}

From source file:org.terasology.core.world.liquid.LiquidSimulator.java

@Override
public void initialise() {
    air = BlockManager.getAir();//from   w  ww  . j  a v  a 2 s .c  om
    grass = blockManager.getBlock("core:Grass");
    snow = blockManager.getBlock("core:Snow");
    dirt = blockManager.getBlock("core:Dirt");
    water = blockManager.getBlock("core:water");
    lava = blockManager.getBlock("core:lava");

    blockQueue = Queues.newLinkedBlockingQueue();

    executor = Executors.newFixedThreadPool(NUM_THREADS);
    for (int i = 0; i < NUM_THREADS; ++i) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
                while (true) {
                    try {
                        LiquidSimulationTask task = blockQueue.take();
                        if (task.shutdownThread()) {
                            break;
                        }
                        try (ThreadActivity ignored = ThreadMonitor.startThreadActivity(task.getName())) {
                            task.run();
                        }
                    } catch (InterruptedException e) {
                        ThreadMonitor.addError(e);
                        logger.debug("Interrupted");
                    } catch (Exception e) {
                        ThreadMonitor.addError(e);
                        logger.error("Error in water simulation", e);
                    }
                }
            }
        });
    }
}

From source file:org.kitesdk.apps.spark.test.SparkTestHarness.java

/**
 * Runs the stream with the given streams and views.
 *//*from   w  w w.ja va2  s.c  o  m*/
public void runStreams(Map<String, List<?>> streams, Map<String, View> views) {

    List<StreamDescription> descriptions = app.getStreamDescriptions();

    for (StreamDescription description : descriptions) {

        Class<? extends StreamingJob> jobClass = description.getJobClass();

        StreamingJob job;

        try {
            job = jobClass.newInstance();
        } catch (InstantiationException e) {
            throw new AppException(e);
        } catch (IllegalAccessException e) {
            throw new AppException(e);
        }

        Method runMethod = JobReflection.resolveRunMethod(job.getClass());

        Map<String, Object> namedArgs = Maps.newHashMap();

        for (Map.Entry<String, List<?>> stream : streams.entrySet()) {

            Queue<JavaRDD<Object>> queue = Queues.newLinkedBlockingQueue();

            JavaRDD rdd = streamingContext.sc().parallelize(stream.getValue());

            queue.add(rdd);

            JavaDStream dstream = streamingContext.queueStream(queue);

            namedArgs.put(stream.getKey(), dstream);
        }

        // Add all views to the named arguments.
        namedArgs.putAll(views);

        Object[] args = JobReflection.getArgs(runMethod, namedArgs);

        // TODO: setup Spark context and population input...

        try {
            runMethod.invoke(job, args);
        } catch (IllegalAccessException e) {
            throw new AppException(e);
        } catch (InvocationTargetException e) {
            throw new AppException(e);
        }

        streamingContext.start();
        clock.advance(120000);

        SparkContextFactory.shutdown();
    }
}