Example usage for java.util.concurrent BlockingQueue offer

List of usage examples for java.util.concurrent BlockingQueue offer

Introduction

In this page you can find the example usage for java.util.concurrent BlockingQueue offer.

Prototype

boolean offer(E e);

Source Link

Document

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

Usage

From source file:com.brienwheeler.svc.monitor.telemetry.impl.AsynchronousTelemetryInfoProcessor.java

@Override
@GracefulShutdown//from   w ww.  j a va 2  s.  com
public void process(TelemetryInfo telemetryInfo) {
    telemetryInfo.checkPublished();

    BlockingQueue<TelemetryInfo> queue = this.queue.get();

    switch (queueFullPolicy.get()) {
    case DISCARD_OFFERED:
        queue.offer(telemetryInfo);
        // return regardless of success
        return;

    case DISCARD_OLDEST:
        while (!queue.offer(telemetryInfo)) {
            // if offer failed, remove and discard one element from queue and try
            // again
            queue.poll();
        }
        return;
    }
}

From source file:com.slytechs.jnetstream.livecapture.AbstractLiveCapture.java

/**
 * Dispatches a packet that is ready to clients. A concurrent blocking queue
 * is used to pass packets between this server thread and clients. Each client
 * maintains its own queue through which the packet is exchanged. If
 * particular clients queue is full, the packet is dropped and the appropriate
 * counter incremented to indicate a drop, on this particular client's queue.
 *//* w ww.ja  va  2s .  c o m*/
public void dispatch(final LivePacket packet) {
    if (client == null) {
        return; // Nothing to do, ignore any packets received
    }

    if (logger.isTraceEnabled()) {
        logger.trace("Received packet");
    }

    final BlockingQueue<LivePacket> queue = client.getPacketQueue();

    if (queue.remainingCapacity() == 0) {
        client.incrementDropCounter(1);

    } else {
        queue.offer(packet);
    }

    synchronized (DISPATCHED) {
        this.dispatched++;
    }
}

From source file:libra.preprocess.common.kmerindex.KmerIndexReader.java

private void fillBuffer() throws IOException {
    if (!this.eof) {
        CompressedSequenceWritable lastBufferedKey = null;
        int added = 0;
        while (added < BUFFER_SIZE) {
            CompressedSequenceWritable key = new CompressedSequenceWritable();
            IntWritable val = new IntWritable();
            if (this.mapfileReaders[this.currentIndex].next(key, val)) {
                KmerIndexBufferEntry entry = new KmerIndexBufferEntry(key, val);
                if (!this.buffer.offer(entry)) {
                    throw new IOException("buffer is full");
                }//from   w w w . jav a2s. c  om

                lastBufferedKey = key;
                added++;
            } else {
                // EOF of this part
                this.mapfileReaders[this.currentIndex].close();
                this.mapfileReaders[this.currentIndex] = null;
                this.currentIndex++;

                if (this.currentIndex == this.mapfileReaders.length) {
                    // last
                    this.eof = true;
                    break;
                } else {
                    this.mapfileReaders[this.currentIndex] = new IndexCloseableMapFileReader(this.fs,
                            this.indexPartFilePaths[this.currentIndex].toString(), this.conf);
                    this.mapfileReaders[this.currentIndex].closeIndex();
                }
            }
        }

        if (this.endKey != null && lastBufferedKey != null) {
            if (lastBufferedKey.compareTo(this.endKey) > 0) {
                // recheck buffer
                BlockingQueue<KmerIndexBufferEntry> new_buffer = new LinkedBlockingQueue<KmerIndexBufferEntry>();

                KmerIndexBufferEntry entry = this.buffer.poll();
                while (entry != null) {
                    if (entry.getKey().compareTo(this.endKey) <= 0) {
                        if (!new_buffer.offer(entry)) {
                            throw new IOException("buffer is full");
                        }
                    }

                    entry = this.buffer.poll();
                }

                this.buffer = new_buffer;
                this.eof = true;
            }
        }
    }
}

From source file:com.netflix.curator.framework.recipes.cache.TestPathChildrenCache.java

@Test
public void testAsyncInitialPopulation() throws Exception {
    PathChildrenCache cache = null;//w  ww  .j  a  v  a  2  s  .c om
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try {
        client.start();

        client.create().forPath("/test");
        client.create().forPath("/test/one", "hey there".getBytes());

        final BlockingQueue<PathChildrenCacheEvent> events = new LinkedBlockingQueue<PathChildrenCacheEvent>();
        cache = new PathChildrenCache(client, "/test", true);
        cache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                events.offer(event);
            }
        });
        cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);

        PathChildrenCacheEvent event = events.poll(10, TimeUnit.SECONDS);
        Assert.assertEquals(event.getType(), PathChildrenCacheEvent.Type.CHILD_ADDED);

        event = events.poll(10, TimeUnit.SECONDS);
        Assert.assertEquals(event.getType(), PathChildrenCacheEvent.Type.INITIALIZED);
        Assert.assertEquals(event.getInitialData().size(), 1);
    } finally {
        IOUtils.closeQuietly(cache);
        IOUtils.closeQuietly(client);
    }
}

From source file:com.netflix.curator.framework.recipes.cache.TestPathChildrenCache.java

@Test
public void testBasics() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();//from ww  w . j av a  2  s .co  m
    try {
        client.create().forPath("/test");

        final BlockingQueue<PathChildrenCacheEvent.Type> events = new LinkedBlockingQueue<PathChildrenCacheEvent.Type>();
        PathChildrenCache cache = new PathChildrenCache(client, "/test", true);
        cache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                if (event.getData().getPath().equals("/test/one")) {
                    events.offer(event.getType());
                }
            }
        });
        cache.start();

        client.create().forPath("/test/one", "hey there".getBytes());
        Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_ADDED);

        client.setData().forPath("/test/one", "sup!".getBytes());
        Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_UPDATED);
        Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "sup!");

        client.delete().forPath("/test/one");
        Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_REMOVED);

        cache.close();
    } finally {
        client.close();
    }
}

From source file:com.wordnik.swaggersocket.server.SwaggerSocketProtocolInterceptor.java

@Override
public Action inspect(final AtmosphereResource r) {

    final AtmosphereRequest request = r.getRequest();
    r.addEventListener(new AtmosphereResourceEventListenerAdapter() {
        /**/* ww  w. jav  a2s.c  o  m*/
         * {@inheritDoc}
         */
        @Override
        public void onSuspend(AtmosphereResourceEvent event) {
            AsyncIOWriter writer = event.getResource().getResponse().getAsyncIOWriter();
            if (writer == null) {
                writer = new AtmosphereInterceptorWriter();
                r.getResponse().asyncIOWriter(writer);
            }

            if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
                AtmosphereInterceptorWriter.class.cast(writer).interceptor(interceptor);
            }
        }
    });

    boolean ok = false;
    if (request.getHeader("SwaggerSocket") != null) {
        ok = true;
    }

    if (ok && request.attributes().get(SWAGGER_SOCKET_DISPATCHED) == null) {

        AtmosphereResponse response = new WrappedAtmosphereResponse(r.getResponse(), request);

        logger.debug("Method {} Transport {}", request.getMethod(), r.transport());
        // Suspend to keep the connection OPEN.
        if (request.getMethod() == "GET" && r.transport().equals(AtmosphereResource.TRANSPORT.LONG_POLLING)) {
            r.resumeOnBroadcast(true).suspend();

            BlockingQueue<AtmosphereResource> queue = (BlockingQueue<AtmosphereResource>) getContextValue(
                    request, SUSPENDED_RESPONSE);
            if (queue == null) {
                queue = new LinkedBlockingQueue<AtmosphereResource>();
                request.getSession().setAttribute(SUSPENDED_RESPONSE, queue);
            }
            queue.offer(r);

            String identity = (String) getContextValue(request, IDENTITY);
            schedule(r, identity);

            return Action.SUSPEND;
        }

        AtmosphereFramework framework = r.getAtmosphereConfig().framework();
        StringBuilder d = new StringBuilder();
        try {
            InputStreamReader isr = new InputStreamReader(request.getInputStream());
            BufferedReader bufReader = new BufferedReader(isr);
            char[] charBuffer = new char[8192];

            for (int readCount = bufReader.read(charBuffer); readCount > -1; readCount = bufReader
                    .read(charBuffer)) {
                d.append(charBuffer, 0, readCount);
            }

            String data = d.toString();

            if (data.length() == 0) {
                return Action.CANCELLED;
            }

            String message = data.substring(0, 20).replaceAll(" ", "");
            logger.debug(data);
            if (message.startsWith("{\"handshake\"")) {
                // This will fail if the message is not well formed.
                HandshakeMessage handshakeMessage = mapper.readValue(data, HandshakeMessage.class);

                // If we missed the CloseReason for whatever reason (IE is a good candidate), make sure we swap the previous session anyway.
                String identity = (String) getContextValue(request, IDENTITY);
                if (identity == null) {
                    identity = UUID.randomUUID().toString();
                } else {
                    logger.debug("Client disconnected {}, cleaning session {}", identity);
                    try {
                        Enumeration<String> e = request.getSession().getAttributeNames();
                        while (e.hasMoreElements()) {
                            request.getSession().removeAttribute(e.nextElement());
                        }
                    } catch (Exception ex) {
                        logger.warn("", ex);
                    }
                }
                addContextValue(request, IDENTITY, identity);

                StatusMessage statusMessage = new StatusMessage.Builder()
                        .status(new StatusMessage.Status(200, "OK")).identity(identity).build();
                response.setContentType("application/json");
                response.getOutputStream().write(mapper.writeValueAsBytes(statusMessage));

                if (r.transport() == AtmosphereResource.TRANSPORT.WEBSOCKET) {
                    schedule(r, identity);
                }
            } else if (message.startsWith("{\"close\"")) {
                CloseMessage c = mapper.readValue(data, CloseMessage.class);

                logger.debug("Client disconnected {} with reason {}", c.getClose().getIdentity(),
                        c.getClose().getReason());
                try {
                    request.getSession().invalidate();
                } catch (Exception ex) {
                    logger.warn("", ex);
                }
                return Action.CANCELLED;
            } else {
                Message swaggerSocketMessage = mapper.readValue(data, Message.class);
                swaggerSocketMessage.transactionID(UUID.randomUUID().toString());

                String identity = (String) getContextValue(request, IDENTITY);

                if (!swaggerSocketMessage.getIdentity().equals(identity)) {
                    StatusMessage statusMessage = new StatusMessage.Builder()
                            .status(new StatusMessage.Status(503, "Not Allowed"))
                            .identity(swaggerSocketMessage.getIdentity()).build();
                    response.getOutputStream().write(mapper.writeValueAsBytes(statusMessage));
                    return Action.CANCELLED;
                }

                transactionIdentity.set(swaggerSocketMessage.transactionID());

                List<Request> requests = swaggerSocketMessage.getRequests();
                addContextValue(request, swaggerSocketMessage.transactionID() + RESPONSE_COUNTER,
                        new AtomicInteger(requests.size()));

                AtmosphereRequest ar;
                for (Request req : requests) {
                    ar = toAtmosphereRequest(request, req);
                    try {
                        ar.attributes().put(SWAGGER_SOCKET_DISPATCHED, "true");

                        // This is a new request, we must clean the Websocket AtmosphereResource.
                        request.removeAttribute(INJECTED_ATMOSPHERE_RESOURCE);
                        response.request(ar);
                        attachWriter(r);
                        ssRequest.set(req);
                        request.setAttribute("swaggerSocketRequest", req);

                        Action action = framework.doCometSupport(ar, response);
                        if (action.type() == Action.TYPE.SUSPEND) {
                            ar.destroyable(false);
                            response.destroyable(false);
                        }
                    } catch (Exception e) {
                        logger.warn("", e);
                        //REVISIT might want to optionally return the body entity?
                        response.setStatus(500, "Server Error");
                        ResponseMessage responseMessage = new ResponseMessage(identity,
                                createResponseBuilder(response, null).build());
                        response.getOutputStream().write(mapper.writeValueAsBytes(responseMessage));
                    }
                }
            }
            return Action.CANCELLED;
        } catch (IOException e) {
            logger.warn("", e);
            return Action.CONTINUE;
        }

    } else {
        if (!ok) {
            request.setAttribute(TrackMessageSizeInterceptor.SKIP_INTERCEPTOR, "true");
        }
    }
    return Action.CONTINUE;
}

From source file:com.clickha.nifi.processors.FetchFileTransferV2.java

/**
 * Close connections that are idle or optionally close all connections.
 * Connections are considered "idle" if they have not been used in 10 seconds.
 *
 * @param closeNonIdleConnections if <code>true</code> will close all connection; if <code>false</code> will close only idle connections
 *///from  www  . j a  v  a2s  . c o  m
private void closeConnections(final boolean closeNonIdleConnections) {
    for (final Map.Entry<Tuple<String, Integer>, BlockingQueue<FileTransferIdleWrapper>> entry : fileTransferMap
            .entrySet()) {
        final BlockingQueue<FileTransferIdleWrapper> wrapperQueue = entry.getValue();

        final List<FileTransferIdleWrapper> putBack = new ArrayList<>();
        FileTransferIdleWrapper wrapper;
        while ((wrapper = wrapperQueue.poll()) != null) {
            final long lastUsed = wrapper.getLastUsed();
            final long nanosSinceLastUse = System.nanoTime() - lastUsed;
            if (!closeNonIdleConnections
                    && TimeUnit.NANOSECONDS.toMillis(nanosSinceLastUse) < IDLE_CONNECTION_MILLIS) {
                putBack.add(wrapper);
            } else {
                try {
                    wrapper.getFileTransfer().close();
                } catch (final IOException ioe) {
                    getLogger().warn("Failed to close Idle Connection due to {}", new Object[] { ioe }, ioe);
                }
            }
        }

        for (final FileTransferIdleWrapper toPutBack : putBack) {
            wrapperQueue.offer(toPutBack);
        }
    }
}

From source file:com.smartmarmot.common.db.DBJob.java

@Override
public void run() {

    SmartLogger.logThis(Level.DEBUG, "Starting dbJob on database " + _dbname + " " + _queriesGroup);
    final long start = System.currentTimeMillis();

    try {/* w ww. j  a va2  s .c o  m*/
        Connection dbConn = this._spds.getConnection();
        /*         if (dbConn.isClosed()){
                    dbConn = this._spds.getConnection();
                 }*/
        if (Alive(dbConn)) {
            _queue.offer(new ZabbixItem("alive", "1", this._dbname));
            _queue.offer(
                    new ZabbixItem(Constants.PROJECT_NAME + "." + "Version", Constants.BANNER, this._dbname));
            ZabbixItem[] zitems = DBEnquiry.execute(this._queries, dbConn, this._dbname);
            if (zitems != null && zitems.length > 0) {
                SmartLogger.logThis(Level.DEBUG,
                        "Item retrieved " + zitems.length + " on database " + this._dbname);
                for (int cnt = 0; cnt < zitems.length; cnt++) {
                    String zItemName = zitems[cnt].getKey();
                    if (this._dgNum > 0) {
                        zItemName = zItemName + "_" + _dgNum;
                    }
                    SmartLogger.logThis(Level.DEBUG, "dbname " + this._dbname + " sending item  "
                            + zitems[cnt].getKey() + " value " + zitems[cnt].getValue());
                    _queue.offer(new ZabbixItem(zItemName, zitems[cnt].getValue(), _dbname));
                }

            }
            dbConn.close();
            Sender sender = new Sender(_queue, _zabbixServers, this._dbname);
            sender.run();
        } else {
            BlockingQueue<ZabbixItem> _queue = new LinkedBlockingQueue<ZabbixItem>();
            _queue.offer(new ZabbixItem("alive", "0", this._dbname));
            _queue.offer(
                    new ZabbixItem(Constants.PROJECT_NAME + "." + "Version", Constants.BANNER, this._dbname));
            for (int cnt = 0; cnt < this._queries.length; cnt++) {
                _queue.offer(new ZabbixItem(_queries[cnt].getName(), _queries[cnt].getNoData(), _dbname));

            }
            Sender sender = new Sender(_queue, _zabbixServers, _dbname);
            sender.run();
        }
    } catch (Exception e) {
        SmartLogger.logThis(Level.ERROR,
                "Error on dbJob for database " + _dbname + " " + _queriesGroup + " error: " + e);
    } finally {
        if (_queries != null)
            _queries = null;
    }
    SmartLogger.logThis(Level.INFO, "Done with dbJob on database " + _dbname + " " + _queriesGroup
            + " elapsed time " + (System.currentTimeMillis() - start) + " ms");
}

From source file:com.clickha.nifi.processors.FetchFileTransferV2.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();/*  ww  w  .j a  va 2  s.  c o  m*/
    if (flowFile == null) {
        return;
    }

    final StopWatch stopWatch = new StopWatch(true);
    final String host = context.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue();
    final int port = context.getProperty(UNDEFAULTED_PORT).evaluateAttributeExpressions(flowFile).asInteger();
    final String filename = context.getProperty(REMOTE_FILENAME).evaluateAttributeExpressions(flowFile)
            .getValue();

    // Try to get a FileTransfer object from our cache.
    BlockingQueue<FileTransferIdleWrapper> transferQueue;
    synchronized (fileTransferMap) {
        final Tuple<String, Integer> tuple = new Tuple<>(host, port);

        transferQueue = fileTransferMap.get(tuple);
        if (transferQueue == null) {
            transferQueue = new LinkedBlockingQueue<>();
            fileTransferMap.put(tuple, transferQueue);
        }

        // periodically close idle connections
        if (System.currentTimeMillis() - lastClearTime > IDLE_CONNECTION_MILLIS) {
            closeConnections(false);
            lastClearTime = System.currentTimeMillis();
        }
    }

    // we have a queue of FileTransfer Objects. Get one from the queue or create a new one.
    FileTransferV2 transfer;
    FileTransferIdleWrapper transferWrapper = transferQueue.poll();
    if (transferWrapper == null) {
        transfer = createFileTransfer(context);
    } else {
        transfer = transferWrapper.getFileTransfer();
    }

    // Pull data from remote system.
    final InputStream in;
    try {
        in = transfer.getInputStream(filename, flowFile);

        flowFile = session.write(flowFile, new OutputStreamCallback() {
            @Override
            public void process(final OutputStream out) throws IOException {
                StreamUtils.copy(in, out);
                transfer.flush();
            }
        });
        transferQueue.offer(new FileTransferIdleWrapper(transfer, System.nanoTime()));
    } catch (final FileNotFoundException e) {
        getLogger().error(
                "Failed to fetch content for {} from filename {} on remote host {} because the file could not be found on the remote system; routing to {}",
                new Object[] { flowFile, filename, host, REL_NOT_FOUND.getName() });
        session.transfer(session.penalize(flowFile), REL_NOT_FOUND);
        session.getProvenanceReporter().route(flowFile, REL_NOT_FOUND);
        return;
    } catch (final PermissionDeniedException e) {
        getLogger().error(
                "Failed to fetch content for {} from filename {} on remote host {} due to insufficient permissions; routing to {}",
                new Object[] { flowFile, filename, host, REL_PERMISSION_DENIED.getName() });
        session.transfer(session.penalize(flowFile), REL_PERMISSION_DENIED);
        session.getProvenanceReporter().route(flowFile, REL_PERMISSION_DENIED);
        return;
    } catch (final ProcessException | IOException e) {
        try {
            transfer.close();
        } catch (final IOException e1) {
            getLogger().warn("Failed to close connection to {}:{} due to {}",
                    new Object[] { host, port, e.toString() }, e);
        }

        getLogger().error(
                "Failed to fetch content for {} from filename {} on remote host {}:{} due to {}; routing to comms.failure",
                new Object[] { flowFile, filename, host, port, e.toString() }, e);
        session.transfer(session.penalize(flowFile), REL_COMMS_FAILURE);
        return;
    }

    // Add FlowFile attributes
    final String protocolName = transfer.getProtocolName();
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(protocolName + ".remote.host", host);
    attributes.put(protocolName + ".remote.port", String.valueOf(port));
    attributes.put(protocolName + ".remote.filename", filename);

    if (filename.contains("/")) {
        final String path = StringUtils.substringBeforeLast(filename, "/");
        final String filenameOnly = StringUtils.substringAfterLast(filename, "/");
        attributes.put(CoreAttributes.PATH.key(), path);
        attributes.put(CoreAttributes.FILENAME.key(), filenameOnly);
    } else {
        attributes.put(CoreAttributes.FILENAME.key(), filename);
    }
    flowFile = session.putAllAttributes(flowFile, attributes);

    // emit provenance event and transfer FlowFile
    session.getProvenanceReporter().fetch(flowFile, protocolName + "://" + host + ":" + port + "/" + filename,
            stopWatch.getElapsed(TimeUnit.MILLISECONDS));
    session.transfer(flowFile, REL_SUCCESS);

    // it is critical that we commit the session before moving/deleting the remote file. Otherwise, we could have a situation where
    // we ingest the data, delete/move the remote file, and then NiFi dies/is shut down before the session is committed. This would
    // result in data loss! If we commit the session first, we are safe.
    session.commit();

    final String completionStrategy = context.getProperty(COMPLETION_STRATEGY).getValue();
    if (COMPLETION_DELETE.getValue().equalsIgnoreCase(completionStrategy)) {
        try {
            transfer.deleteFile(null, filename);
        } catch (final FileNotFoundException e) {
            // file doesn't exist -- effectively the same as removing it. Move on.
        } catch (final IOException ioe) {
            getLogger().warn(
                    "Successfully fetched the content for {} from {}:{}{} but failed to remove the remote file due to {}",
                    new Object[] { flowFile, host, port, filename, ioe }, ioe);
        }
    } else if (COMPLETION_MOVE.getValue().equalsIgnoreCase(completionStrategy)) {
        String targetDir = context.getProperty(MOVE_DESTINATION_DIR).evaluateAttributeExpressions(flowFile)
                .getValue();
        if (!targetDir.endsWith("/")) {
            targetDir = targetDir + "/";
        }
        final String simpleFilename = StringUtils.substringAfterLast(filename, "/");
        final String target = targetDir + simpleFilename;

        try {
            transfer.rename(filename, target);
        } catch (final IOException ioe) {
            getLogger().warn(
                    "Successfully fetched the content for {} from {}:{}{} but failed to rename the remote file due to {}",
                    new Object[] { flowFile, host, port, filename, ioe }, ioe);
        }
    }
}

From source file:net.tomp2p.simgrid.SimGridTomP2P.java

public static void addQueue(Number160 senderID, SendingMessage sendingMessage) {
    BlockingQueue<SendingMessage> queue = pendingMessages.get(senderID);
    if (queue == null) {
        queue = new LinkedBlockingQueue<SendingMessage>();
        pendingMessages.put(senderID, queue);
    }//  w w  w. j  ava  2  s  . c o m
    queue.offer(sendingMessage);
    notify(senderID);
}