Example usage for java.util.concurrent BlockingQueue add

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

Introduction

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

Prototype

boolean add(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 throwing an IllegalStateException if no space is currently available.

Usage

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void upsertMergePatchKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    KvObject<ObjectNode> kvBefore = client.kv(collection(), key).get(ObjectNode.class).get();

    // assert the object is not present before the patch
    assertNull(kvBefore);/*from w  ww  .  j av a 2 s  .  co  m*/

    String name = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);

    client.kv(collection(), key).upsert().merge("{\"name\":\"" + name + "\"}")
            .on(new ResponseAdapter<KvMetadata>() {
                @Override
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(patched);

    final KvObject<ObjectNode> kvObject = client.kv(patched.getCollection(), patched.getKey())
            .get(ObjectNode.class).get();

    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name, kvObject.getValue().get("name").asText());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void getKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final KvMetadata kvMetadata = insertItem(key, "{}");

    final BlockingQueue<KvObject> queue = DataStructures.getLTQInstance(KvObject.class);
    client.kv(kvMetadata.getCollection(), kvMetadata.getKey()).get(String.class)
            .on(new ResponseAdapter<KvObject<String>>() {
                @Override/*from   www  .ja  v  a 2 s  . c  om*/
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvObject<String> object) {
                    queue.add(object);
                }
            });

    @SuppressWarnings("unchecked")
    final KvObject<String> object = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(kvMetadata);
    assertNotNull(object);
    assertEquals(kvMetadata.getCollection(), object.getCollection());
    assertEquals(kvMetadata.getKey(), object.getKey());
    assertEquals(kvMetadata.getRef(), object.getRef());
    assertEquals("{}", object.getValue());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void patchKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final KvMetadata kvMetadata = insertItem(key, "{}");

    String name = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);
    client.kv(collection(), key).patch(JsonPatch.builder().add("name", name).build())
            .on(new ResponseAdapter<KvMetadata>() {
                @Override/*from  w ww .  j a v a2s  .  c o m*/
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotEquals(kvMetadata, patched);

    final KvObject<ObjectNode> kvObject = client.kv(kvMetadata.getCollection(), kvMetadata.getKey())
            .get(ObjectNode.class).get();

    assertNotNull(kvMetadata);
    assertNotNull(kvObject);
    assertEquals(kvMetadata.getCollection(), kvObject.getCollection());
    assertEquals(kvMetadata.getKey(), kvObject.getKey());
    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name, kvObject.getValue().get("name").asText());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
@org.junit.Ignore//from   w ww. j a v a2  s  .c om
public void getKeyWithListener(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    insertItem(key, "{}");

    final BlockingQueue<KvObject> queue = DataStructures.getLTQInstance(KvObject.class);
    final KvObject<String> object = client.kv(collection(), key).get(String.class)
            .on(new ResponseAdapter<KvObject<String>>() {
                @Override
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvObject<String> object) {
                    queue.add(object);
                }
            }).on(new ResponseAdapter<KvObject<String>>() {
                @Override
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvObject<String> object) {
                    queue.add(object);
                }
            }).get();

    @SuppressWarnings("unchecked")
    final KvObject result1 = queue.poll(5000, TimeUnit.MILLISECONDS);
    final KvObject result2 = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(result1);
    assertNotNull(result2);
    assertEquals(result1, result2);
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void putKeyIfMatchAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final String collection = collection();
    final KvMetadata kvMetadata = client.kv(collection, key).put("{}").get();

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);
    client.kv(kvMetadata.getCollection(), kvMetadata.getKey()).ifMatch(kvMetadata.getRef()).put("{}")
            .on(new ResponseAdapter<KvMetadata>() {
                @Override/*from  ww w. j  av  a  2  s. c  o  m*/
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata kvMetadata2 = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(kvMetadata);
    assertNotNull(kvMetadata2);
    assertEquals(collection, kvMetadata2.getCollection());
    assertEquals(key, kvMetadata2.getKey());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void upsertPatchKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    KvObject<ObjectNode> kvBefore = client.kv(collection(), key).get(ObjectNode.class).get();

    // assert the object is not present before the patch
    assertNull(kvBefore);/* www  .  jav a  2  s.  c o  m*/

    String name = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);
    client.kv(collection(), key).upsert().patch(JsonPatch.builder().add("name", name).build())
            .on(new ResponseAdapter<KvMetadata>() {
                @Override
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(patched);

    final KvObject<ObjectNode> kvObject = client.kv(patched.getCollection(), patched.getKey())
            .get(ObjectNode.class).get();

    assertNotNull(kvObject);
    assertEquals(patched.getCollection(), kvObject.getCollection());
    assertEquals(patched.getKey(), kvObject.getKey());
    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name, kvObject.getValue().get("name").asText());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void conditionalMergePatchKeyAsync(@ForAll(sampleSize = 10) final String key)
        throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    String name1 = Long.toHexString(RAND.nextLong());

    final KvMetadata kvMetadata = insertItem(key, "{`name1`:`%s`}", name1);

    String name2 = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);

    client.kv(collection(), key).ifMatch(kvMetadata.getRef()).merge("{\"name2\":\"" + name2 + "\"}")
            .on(new ResponseAdapter<KvMetadata>() {
                @Override//from www. ja  v  a2s  . co  m
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotEquals(kvMetadata, patched);

    final KvObject<ObjectNode> kvObject = client.kv(kvMetadata.getCollection(), kvMetadata.getKey())
            .get(ObjectNode.class).get();

    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name1, kvObject.getValue().get("name1").asText());
    assertEquals(name2, kvObject.getValue().get("name2").asText());
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void pojoKvGetAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));
    User user = new User(key, "description for " + key);

    final KvMetadata userKvMeta = client.kv(collection(), key).put(user).get();

    final BlockingQueue<KvObject> queue = DataStructures.getLTQInstance(KvObject.class);

    final OrchestrateRequest<KvObject<User>> getUserRequest = client.kv(collection(), key).get(User.class)
            .on(new ResponseAdapter<KvObject<User>>() {
                @Override/*from w w  w .j  av  a  2s . c  o m*/
                public void onFailure(final Throwable error) {
                    // handle error condition
                }

                @Override
                public void onSuccess(final KvObject<User> userKv) {
                    queue.add(userKv);
                    User user = userKv.getValue();
                    String userKey = userKv.getKey();
                    String userRef = userKv.getRef();

                    System.out.println(String.format("Read user key:%s, name:%s, ref:%s", userKey, userRef,
                            user.getName()));
                }
            });
    @SuppressWarnings("unchecked")
    final KvObject userKv = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotNull(userKv);
}

From source file:io.orchestrate.client.itest.KvTest.java

@Theory
public void conditionalPatchKeyAsync(@ForAll(sampleSize = 10) final String key) throws InterruptedException {
    assumeThat(key, not(isEmptyString()));

    final KvMetadata kvMetadata = insertItem(key, "{}");

    String name = Long.toHexString(RAND.nextLong());

    final BlockingQueue<KvMetadata> queue = DataStructures.getLTQInstance(KvMetadata.class);
    client.kv(collection(), key).ifMatch(kvMetadata.getRef())
            .patch(JsonPatch.builder().add("name", name).build()).on(new ResponseAdapter<KvMetadata>() {
                @Override/*  ww  w  .  j av a2  s  . com*/
                public void onFailure(final Throwable error) {
                    fail(error.getMessage());
                }

                @Override
                public void onSuccess(final KvMetadata object) {
                    queue.add(object);
                }
            });

    final KvMetadata patched = queue.poll(5000, TimeUnit.MILLISECONDS);

    assertNotEquals(kvMetadata, patched);

    final KvObject<ObjectNode> kvObject = client.kv(kvMetadata.getCollection(), kvMetadata.getKey())
            .get(ObjectNode.class).get();

    assertNotNull(kvMetadata);
    assertNotNull(kvObject);
    assertEquals(kvMetadata.getCollection(), kvObject.getCollection());
    assertEquals(kvMetadata.getKey(), kvObject.getKey());
    assertEquals(patched.getRef(), kvObject.getRef());
    assertEquals(name, kvObject.getValue().get("name").asText());
}

From source file:ok.MyService2.java

@Override
protected Task<BlockingQueue> createTask() {
    final Task<BlockingQueue> task;
    task = new Task<BlockingQueue>() {

        @Override/*from  w  w w . ja va  2 s . c  om*/
        protected BlockingQueue call() throws Exception {
            BlockingQueue result = new LinkedBlockingQueue<String>();

            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
            cm.setMaxTotal(100);

            CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
            try {
                ExecutorService executor = Executors.newFixedThreadPool(sites.size());
                List<Future<String>> results = new ArrayList<Future<String>>();
                for (int i = 0; i < sites.size(); i++) {
                    HttpGet httpget = new HttpGet(sites.get(i));
                    Callable worker = new MyCallable(httpclient, httpget);
                    Future<String> res = executor.submit(worker);
                    results.add(res);
                    // String url = hostList[i];
                    //   Runnable worker = new MyRunnable(url);
                    //   executor.execute(worker);
                    //   executor.submit(null);

                }
                executor.shutdown();
                // Wait until all threads are finish
                //                   while (!executor.isTerminated()) {
                //
                //                   }
                for (Future<String> element : results) {
                    result.add(element.get());
                }
                System.out.println("\nFinished all threads");

            } finally {
                httpclient.close();
            }
            return result;
        }

    };
    return task;
}