Search in sources :

Example 21 with SimpleMessageStore

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

the class AbstractCorrelatingMessageHandlerTests method testDontReapMessageOfOtherHandler.

@Test
public void testDontReapMessageOfOtherHandler() throws Exception {
    MessageGroupStore groupStore = new SimpleMessageStore();
    AggregatingMessageHandler handler1 = new AggregatingMessageHandler(group -> group, groupStore);
    AggregatingMessageHandler handler2 = new AggregatingMessageHandler(group -> group, groupStore);
    QueueChannel handler1DiscardChannel = new QueueChannel();
    handler1.setDiscardChannel(handler1DiscardChannel);
    QueueChannel handler2DiscardChannel = new QueueChannel();
    handler2.setDiscardChannel(handler2DiscardChannel);
    handler1.setReleaseStrategy(group -> false);
    handler2.setReleaseStrategy(group -> false);
    handler1.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
    handler1.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
    handler2.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("bar").build());
    groupStore.expireMessageGroups(0);
    assertTrue(handler1DiscardChannel.getQueueSize() == 2);
    assertTrue(handler2DiscardChannel.getQueueSize() == 1);
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Example 22 with SimpleMessageStore

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

the class MessageGroupQueueTests method testConcurrentReadWrite.

@Test
public void testConcurrentReadWrite() throws Exception {
    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1);
    final AtomicReference<Message<?>> messageHolder = new AtomicReference<Message<?>>();
    Thread t1 = new Thread(() -> {
        try {
            messageHolder.set(queue.poll(1000, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            queue.offer(new GenericMessage<String>("hello"), 1000, TimeUnit.SECONDS);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    t1.start();
    t2.start();
    Thread.sleep(1000);
    assertTrue(messageHolder.get() instanceof Message);
}
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 23 with SimpleMessageStore

use of org.springframework.integration.store.SimpleMessageStore 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 24 with SimpleMessageStore

use of org.springframework.integration.store.SimpleMessageStore 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 25 with SimpleMessageStore

use of org.springframework.integration.store.SimpleMessageStore 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

SimpleMessageStore (org.springframework.integration.store.SimpleMessageStore)25 Test (org.junit.Test)20 GenericMessage (org.springframework.messaging.support.GenericMessage)10 MessageGroupStore (org.springframework.integration.store.MessageGroupStore)8 Message (org.springframework.messaging.Message)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 MessageGroupQueue (org.springframework.integration.store.MessageGroupQueue)6 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)6 QueueChannel (org.springframework.integration.channel.QueueChannel)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)3 MessageStore (org.springframework.integration.store.MessageStore)3 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutorService (java.util.concurrent.ExecutorService)2 DirectChannel (org.springframework.integration.channel.DirectChannel)2 MessageGroup (org.springframework.integration.store.MessageGroup)2 SimpleMessageGroupFactory (org.springframework.integration.store.SimpleMessageGroupFactory)2 StopWatch (org.springframework.util.StopWatch)2 Method (java.lang.reflect.Method)1