Java Code Examples for java.util.concurrent.LinkedBlockingQueue#toArray()

The following examples show how to use java.util.concurrent.LinkedBlockingQueue#toArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * toArray contains all elements in FIFO order
 */
public void testToArray() {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    Object[] o = q.toArray();
    for (int i = 0; i < o.length; i++)
        assertSame(o[i], q.poll());
}
 
Example 2
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * toArray(a) contains all elements in FIFO order
 */
public void testToArray2() throws InterruptedException {
    LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
    Integer[] ints = new Integer[SIZE];
    Integer[] array = q.toArray(ints);
    assertSame(ints, array);
    for (int i = 0; i < ints.length; i++)
        assertSame(ints[i], q.poll());
}
 
Example 3
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * toArray(incompatible array type) throws ArrayStoreException
 */
public void testToArray1_BadArg() {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    try {
        q.toArray(new String[10]);
        shouldThrow();
    } catch (ArrayStoreException success) {}
}
 
Example 4
Source File: CollectionStuffTest.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueue() throws Exception {
    LinkedBlockingQueue<Byte> lbq = new LinkedBlockingQueue<>();
    lbq.add(Byte.valueOf("1"));
    lbq.add(Byte.valueOf("2"));
    lbq.add(Byte.valueOf("3"));
    assertEquals(1, (byte) lbq.peek());
    assertEquals(1, (byte) lbq.peek());
    assertEquals(1, (byte) lbq.peek());

    Byte[] bufferList = new Byte[lbq.size()];
    Byte[] lbqList = lbq.toArray(bufferList);
    assertArrayEquals(bufferList, lbqList);
    assertSame(bufferList, lbqList);

    File file = new File("queue.txt");
    try (FileOutputStream fileChannel = new FileOutputStream(file)) {
        byte[] bytes = new byte[3];
        bytes[0] = Byte.parseByte("1");
        bytes[1] = Byte.parseByte("2");
        bytes[2] = Byte.parseByte("3");
        fileChannel.write(bytes);
        fileChannel.flush();
        fileChannel.close();

        try (FileReader fr = new FileReader(file)) {
            char[] chars = new char[3];
            assertEquals(3, fr.read(chars));
            assertEquals(1, chars[0]);
            assertEquals(2, chars[1]);
            assertEquals(3, chars[2]);

            assertEquals(1, (byte) lbq.remove());
            assertEquals(2, (byte) lbq.remove());
            assertEquals(3, (byte) lbq.remove());
        }
    } finally {
        retryDelete(file, 3);
    }
}
 
Example 5
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * toArray contains all elements in FIFO order
 */
public void testToArray() {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    Object[] o = q.toArray();
    for (int i = 0; i < o.length; i++)
        assertSame(o[i], q.poll());
}
 
Example 6
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * toArray(a) contains all elements in FIFO order
 */
public void testToArray2() throws InterruptedException {
    LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
    Integer[] ints = new Integer[SIZE];
    Integer[] array = q.toArray(ints);
    assertSame(ints, array);
    for (int i = 0; i < ints.length; i++)
        assertSame(ints[i], q.poll());
}
 
Example 7
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * toArray(incompatible array type) throws ArrayStoreException
 */
public void testToArray1_BadArg() {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    try {
        q.toArray(new String[10]);
        shouldThrow();
    } catch (ArrayStoreException success) {}
}
 
Example 8
Source File: FailedConnectionListener.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public void handleFailedConnection(PushManager<? extends SimpleApnsPushNotification> pushManager, Throwable cause) {
    List<SimpleApnsPushNotification> notifications = new ArrayList<SimpleApnsPushNotification>();
    if (cause instanceof SSLException || cause instanceof SSLHandshakeException || cause instanceof ClosedChannelException) { //cert is probably bad so shut it down.
        if (!pushManager.isShutDown()) {
            pushManager.unregisterFailedConnectionListener(this);

            try {
                BlockingQueue notificationQueue =  pushManager.getQueue();
                if(notificationQueue !=null){
                    LinkedBlockingQueue<SimpleApnsPushNotification>  queue =  ( LinkedBlockingQueue<SimpleApnsPushNotification> )notificationQueue;
                    Object[] objectMess = queue.toArray(); //get messages still in queue
                    for(Object o : objectMess){
                        if(o instanceof SimpleApnsPushNotification) {
                            notifications.add((SimpleApnsPushNotification) o);
                        }
                    }
                }
                pushManager.shutdown();
            } catch (InterruptedException ie) {
                logger.error("Failed to stop push services", ie);
            }
        } else {
            return;
        }
    }
    //mark all unsent notifications failed
    if (notifications != null) {
            notifications.forEach(notification -> {
                if (notification instanceof APNsNotification) {
                    try {
                        ((APNsNotification) notification).messageSendFailed(cause);//mark failed with bad token
                    } catch (Exception e) {
                        logger.error("failed to track notification in failed connection listener", e);
                    }
                }
                //if test this is a problem because you can't connect
                if (notification instanceof TestAPNsNotification) {
                    TestAPNsNotification testAPNsNotification = ((TestAPNsNotification) notification);
                    testAPNsNotification.setReason(cause);
                    testAPNsNotification.countdown();
                }

            });
        pushManager.getQueue().clear();
    }
    logger.error("Failed to register push connection", cause);
}