Example usage for org.springframework.integration.store MessageGroupQueue add

List of usage examples for org.springframework.integration.store MessageGroupQueue add

Introduction

In this page you can find the example usage for org.springframework.integration.store MessageGroupQueue 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:org.springframework.integration.store.MessageGroupQueueTests.java

private void doTestConcurrentAccess(int concurrency, final int maxPerTask, final Set<String> set)
        throws Exception {

    SimpleMessageStore messageGroupStore = new SimpleMessageStore();
    final MessageGroupQueue queue = new MessageGroupQueue(messageGroupStore, "FOO");
    ExecutorService executorService = Executors.newCachedThreadPool();
    CompletionService<Boolean> completionService = new ExecutorCompletionService<Boolean>(executorService);

    for (int i = 0; i < concurrency; i++) {

        final int big = i;

        completionService.submit(new Callable<Boolean>() {
            public Boolean call() throws Exception {
                boolean result = true;
                for (int j = 0; j < maxPerTask; j++) {
                    result &= queue.add(new GenericMessage<String>("count=" + big + ":" + j));
                    if (!result) {
                        logger.warn("Failed to add");
                    }//from  w w w .  j a  va2s  .c  om
                }
                return result;
            }
        });

        completionService.submit(new Callable<Boolean>() {
            public Boolean call() throws Exception {
                boolean result = true;
                for (int j = 0; j < maxPerTask; j++) {
                    @SuppressWarnings("unchecked")
                    Message<String> item = (Message<String>) queue.poll(10, TimeUnit.SECONDS);
                    result &= item != null;
                    if (!result) {
                        logger.warn("Failed to poll");
                    } else if (set != null) {
                        synchronized (set) {
                            set.add(item.getPayload());
                        }
                    }
                }
                return result;
            }
        });

        messageGroupStore.expireMessageGroups(-10000);

    }

    for (int j = 0; j < 2 * concurrency; j++) {
        assertTrue(completionService.take().get());
    }

    if (set != null) {
        // Ensure all items polled are unique
        assertEquals(concurrency * maxPerTask, set.size());
    }

    assertEquals(0, queue.size());
    messageGroupStore.expireMessageGroups(-10000);
    assertEquals(Integer.MAX_VALUE, queue.remainingCapacity());

    executorService.shutdown();

}