Example usage for java.util.concurrent BlockingQueue size

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

Introduction

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

Prototype

int size();

Source Link

Document

Returns the number of elements in this collection.

Usage

From source file:net.yacy.http.servlets.YaCyDefaultServlet.java

/**
 * TODO: add same functionality & checks as in HTTPDemon.parseMultipart
 *
 * parse multi-part form data for formfields, see also original
 * implementation in HTTPDemon.parseMultipart
 *
 * For file data the parameter for the formfield contains the filename and a
 * additional parameter with appendix [fieldname]$file conteins the upload content
 * (e.g. <input type="file" name="upload">  upload="local/filename" upload$file=[content])
 *
 * @param request//from  w  w w.j a v  a 2s  . c  o  m
 * @param args found fields/values are added to the map
 */
protected void parseMultipart(final HttpServletRequest request, final serverObjects args) throws IOException {

    // reject too large uploads
    if (request.getContentLength() > SIZE_FILE_THRESHOLD)
        throw new IOException("FileUploadException: uploaded file too large = " + request.getContentLength());

    // check if we have enough memory
    if (!MemoryControl.request(request.getContentLength() * 3, false)) {
        throw new IOException("not enough memory available for request. request.getContentLength() = "
                + request.getContentLength() + ", MemoryControl.available() = " + MemoryControl.available());
    }
    ServletFileUpload upload = new ServletFileUpload(DISK_FILE_ITEM_FACTORY);
    upload.setFileSizeMax(SIZE_FILE_THRESHOLD);
    try {
        // Parse the request to get form field items
        List<FileItem> fileItems = upload.parseRequest(request);
        // Process the uploaded file items
        Iterator<FileItem> i = fileItems.iterator();
        final BlockingQueue<Map.Entry<String, byte[]>> files = new LinkedBlockingQueue<>();
        while (i.hasNext()) {
            FileItem item = i.next();
            if (item.isFormField()) {
                // simple text
                if (item.getContentType() == null || !item.getContentType().contains("charset")) {
                    // old yacy clients use their local default charset, on most systems UTF-8 (I hope ;)
                    args.add(item.getFieldName(), item.getString(StandardCharsets.UTF_8.name()));
                } else {
                    // use default encoding (given as header or ISO-8859-1)
                    args.add(item.getFieldName(), item.getString());
                }
            } else {
                // read file upload
                args.add(item.getFieldName(), item.getName()); // add the filename to the parameters
                InputStream filecontent = null;
                try {
                    filecontent = item.getInputStream();
                    files.put(new AbstractMap.SimpleEntry<String, byte[]>(item.getFieldName(),
                            FileUtils.read(filecontent)));
                } catch (IOException e) {
                    ConcurrentLog.info("FILEHANDLER", e.getMessage());
                } finally {
                    if (filecontent != null)
                        try {
                            filecontent.close();
                        } catch (IOException e) {
                            ConcurrentLog.info("FILEHANDLER", e.getMessage());
                        }
                }
            }
        }
        if (files.size() <= 1) { // TODO: should include additonal checks to limit parameter.size below rel. large SIZE_FILE_THRESHOLD
            for (Map.Entry<String, byte[]> job : files) { // add the file content to parameter fieldname$file
                String n = job.getKey();
                byte[] v = job.getValue();
                String filename = args.get(n);
                if (filename != null && filename.endsWith(".gz")) {
                    // transform this value into base64
                    String b64 = Base64Order.standardCoder.encode(v);
                    args.put(n + "$file", b64);
                    args.remove(n);
                    args.put(n, filename + ".base64");
                } else {
                    args.put(n + "$file", v); // the byte[] is transformed into UTF8. You cannot push binaries here
                }
            }
        } else {
            // do this concurrently (this would all be superfluous if serverObjects could store byte[] instead only String)
            int t = Math.min(files.size(), Runtime.getRuntime().availableProcessors());
            final Map.Entry<String, byte[]> POISON = new AbstractMap.SimpleEntry<>(null, null);
            Thread[] p = new Thread[t];
            for (int j = 0; j < t; j++) {
                files.put(POISON);
                p[j] = new Thread("YaCyDefaultServlet.parseMultipart-" + j) {
                    @Override
                    public void run() {
                        Map.Entry<String, byte[]> job;
                        try {
                            while ((job = files.take()) != POISON) {
                                String n = job.getKey();
                                byte[] v = job.getValue();
                                String filename = args.get(n);
                                String b64 = Base64Order.standardCoder.encode(v);
                                synchronized (args) {
                                    args.put(n + "$file", b64);
                                    args.remove(n);
                                    args.put(n, filename + ".base64");
                                }
                            }
                        } catch (InterruptedException e) {
                        }
                    }
                };
                p[j].start();
            }
            for (int j = 0; j < t; j++)
                p[j].join();
        }
    } catch (Exception ex) {
        ConcurrentLog.info("FILEHANDLER", ex.getMessage());
    }
}

From source file:io.nats.client.ITClusterTest.java

@Test
public void testHotSpotReconnect() throws InterruptedException {
    int numClients = 100;
    ExecutorService executor = Executors.newFixedThreadPool(numClients,
            new NatsThreadFactory("testhotspotreconnect"));

    final BlockingQueue<String> rch = new LinkedBlockingQueue<String>();
    final BlockingQueue<Integer> dch = new LinkedBlockingQueue<Integer>();
    final AtomicBoolean shutdown = new AtomicBoolean(false);
    try (NatsServer s1 = runServerOnPort(1222)) {
        try (NatsServer s2 = runServerOnPort(1224)) {
            try (NatsServer s3 = runServerOnPort(1226)) {

                final class NATSClient implements Runnable {
                    Connection nc = null;
                    final AtomicInteger numReconnects = new AtomicInteger(0);
                    final AtomicInteger numDisconnects = new AtomicInteger(0);
                    String currentUrl = null;
                    final AtomicInteger instance = new AtomicInteger(-1);

                    final Options opts;

                    NATSClient(int inst) {
                        this.instance.set(inst);
                        opts = defaultOptions();
                        opts.servers = Nats.processUrlArray(testServers);

                        opts.disconnectedCb = new DisconnectedCallback() {
                            public void onDisconnect(ConnectionEvent event) {
                                numDisconnects.incrementAndGet();
                                try {
                                    dch.put(instance.get());
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }//from ww w.ja  v  a  2 s.c o  m
                                nc.setDisconnectedCallback(null);
                            }
                        };
                        opts.reconnectedCb = new ReconnectedCallback() {
                            public void onReconnect(ConnectionEvent event) {
                                numReconnects.incrementAndGet();
                                currentUrl = nc.getConnectedUrl();
                                try {
                                    rch.put(currentUrl);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        };
                    }

                    @Override
                    public void run() {
                        try {
                            nc = opts.connect();
                            assertTrue(!nc.isClosed());
                            assertNotNull(nc.getConnectedUrl());
                            currentUrl = nc.getConnectedUrl();
                            // System.err.println("Instance " + instance + " connected to " +
                            // currentUrl);
                            while (!shutdown.get()) {
                                sleep(10);
                            }
                            nc.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    public synchronized boolean isConnected() {
                        return (nc != null && !nc.isClosed());
                    }

                    public void shutdown() {
                        shutdown.set(true);
                    }
                }

                List<NATSClient> tasks = new ArrayList<NATSClient>(numClients);
                for (int i = 0; i < numClients; i++) {
                    NATSClient task = new NATSClient(i);
                    tasks.add(task);
                    executor.submit(task);
                }

                Map<String, Integer> cs = new HashMap<String, Integer>();

                int numReady = 0;
                while (numReady < numClients) {
                    numReady = 0;
                    for (NATSClient cli : tasks) {
                        if (cli.isConnected()) {
                            numReady++;
                        }
                    }
                    sleep(100);
                }

                s1.shutdown();
                sleep(1000);

                int disconnected = 0;
                // wait for disconnects
                while (dch.size() > 0 && disconnected < numClients) {
                    Integer instance = -1;
                    instance = dch.poll(5, TimeUnit.SECONDS);
                    assertNotNull("timed out waiting for disconnect signal", instance);
                    disconnected++;
                }
                assertTrue(disconnected > 0);

                int reconnected = 0;
                // wait for reconnects
                for (int i = 0; i < disconnected; i++) {
                    String url = null;
                    while (rch.size() == 0) {
                        sleep(50);
                    }
                    url = rch.poll(5, TimeUnit.SECONDS);
                    assertNotNull("timed out waiting for reconnect signal", url);
                    reconnected++;
                    Integer count = cs.get(url);
                    if (count != null) {
                        cs.put(url, ++count);
                    } else {
                        cs.put(url, 1);
                    }
                }

                for (NATSClient client : tasks) {
                    client.shutdown();
                }
                executor.shutdownNow();
                assertTrue(executor.awaitTermination(2, TimeUnit.SECONDS));

                assertEquals(disconnected, reconnected);

                int numServers = 2;

                assertEquals(numServers, cs.size());

                int expected = numClients / numServers;
                // We expect a 40 percent variance
                int var = (int) ((float) expected * 0.40);

                int delta = Math.abs(cs.get(testServers[2]) - cs.get(testServers[4]));
                // System.err.printf("var = %d, delta = %d\n", var, delta);
                if (delta > var) {
                    String str = String.format("Connected clients to servers out of range: %d/%d", delta, var);
                    fail(str);
                }
            }
        }
    }
}