Search in sources :

Example 6 with MessageGroupQueue

use of org.springframework.integration.store.MessageGroupQueue in project spring-integration by spring-projects.

the class MessageGroupQueueTests method testConcurrentReadersWithTimeout.

@Test
public void testConcurrentReadersWithTimeout() throws Exception {
    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1);
    final AtomicReference<Message<?>> messageHolder1 = new AtomicReference<Message<?>>();
    final AtomicReference<Message<?>> messageHolder2 = new AtomicReference<Message<?>>();
    final AtomicReference<Message<?>> messageHolder3 = new AtomicReference<Message<?>>();
    Thread t1 = new Thread(() -> {
        try {
            messageHolder1.set(queue.poll(10, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            messageHolder2.set(queue.poll(10, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });
    Thread t3 = new Thread(() -> {
        try {
            messageHolder3.set(queue.poll(10, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });
    Thread t4 = new Thread(() -> {
        try {
            queue.offer(new GenericMessage<String>("Hi"), 10, TimeUnit.SECONDS);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    t1.start();
    Thread.sleep(1000);
    t2.start();
    Thread.sleep(1000);
    t3.start();
    Thread.sleep(1000);
    t4.start();
    Thread.sleep(1000);
    assertNotNull(messageHolder1.get());
    assertEquals("Hi", messageHolder1.get().getPayload());
    Thread.sleep(4000);
    assertTrue(messageHolder2.get() == null);
}
Also used : SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroupQueue(org.springframework.integration.store.MessageGroupQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 7 with MessageGroupQueue

use of org.springframework.integration.store.MessageGroupQueue in project spring-integration by spring-projects.

the class MessageGroupQueueTests method testConcurrentWritersWithTimeout.

@Test
public void testConcurrentWritersWithTimeout() throws Exception {
    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1);
    final AtomicReference<Boolean> booleanHolder1 = new AtomicReference<Boolean>(true);
    final AtomicReference<Boolean> booleanHolder2 = new AtomicReference<Boolean>(true);
    final AtomicReference<Boolean> booleanHolder3 = new AtomicReference<Boolean>(true);
    Thread t1 = new Thread(() -> {
        try {
            booleanHolder1.set(queue.offer(new GenericMessage<String>("Hi-1"), 2, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            boolean offered = queue.offer(new GenericMessage<String>("Hi-2"), 2, TimeUnit.SECONDS);
            booleanHolder2.set(offered);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    Thread t3 = new Thread(() -> {
        try {
            boolean offered = queue.offer(new GenericMessage<String>("Hi-3"), 2, TimeUnit.SECONDS);
            booleanHolder3.set(offered);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    t1.start();
    Thread.sleep(1000);
    t2.start();
    Thread.sleep(100);
    t3.start();
    Thread.sleep(4000);
    assertTrue(booleanHolder1.get());
    assertFalse(booleanHolder2.get());
    assertFalse(booleanHolder3.get());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) MessageGroupQueue(org.springframework.integration.store.MessageGroupQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 8 with MessageGroupQueue

use of org.springframework.integration.store.MessageGroupQueue in project spring-integration by spring-projects.

the class MessageGroupQueueTests method testConcurrentWriteReadMulti.

@Test
public void testConcurrentWriteReadMulti() throws Exception {
    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 4);
    final AtomicReference<Message<?>> messageHolder = new AtomicReference<Message<?>>();
    queue.offer(new GenericMessage<String>("hello"), 1000, TimeUnit.SECONDS);
    Thread t1 = new Thread(() -> {
        try {
            queue.offer(new GenericMessage<String>("Hi"), 1000, TimeUnit.SECONDS);
            queue.offer(new GenericMessage<String>("Hi"), 1000, TimeUnit.SECONDS);
            queue.offer(new GenericMessage<String>("Hi"), 1000, TimeUnit.SECONDS);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            queue.poll(1000, TimeUnit.SECONDS);
            messageHolder.set(queue.poll(1000, TimeUnit.SECONDS));
            queue.poll(1000, TimeUnit.SECONDS);
            queue.poll(1000, TimeUnit.SECONDS);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });
    t1.start();
    Thread.sleep(1000);
    t2.start();
    Thread.sleep(1000);
    assertTrue(messageHolder.get().getPayload().equals("Hi"));
    assertNull(queue.poll(5, TimeUnit.SECONDS));
}
Also used : SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroupQueue(org.springframework.integration.store.MessageGroupQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Aggregations

MessageGroupQueue (org.springframework.integration.store.MessageGroupQueue)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Test (org.junit.Test)7 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)7 SimpleMessageStore (org.springframework.integration.store.SimpleMessageStore)6 GenericMessage (org.springframework.messaging.support.GenericMessage)6 Message (org.springframework.messaging.Message)5 MessageGroup (org.springframework.integration.store.MessageGroup)1 MessageGroupStore (org.springframework.integration.store.MessageGroupStore)1