Search in sources :

Example 6 with MessageGroupStore

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

the class AggregatorIntegrationTests method testGroupTimeoutReschedulingOnMessageDeliveryException.

@Test
public void testGroupTimeoutReschedulingOnMessageDeliveryException() throws Exception {
    for (int i = 0; i < 5; i++) {
        this.output.send(new GenericMessage<String>("fake message"));
    }
    Map<String, Object> headers = stubHeaders(1, 2, 1);
    this.groupTimeoutAggregatorInput.send(new GenericMessage<Integer>(1, headers));
    // Wait until 'group-timeout' does its stuff.
    MessageGroupStore mgs = TestUtils.getPropertyValue(this.context.getBean("gta.handler"), "messageStore", MessageGroupStore.class);
    int n = 0;
    while (n++ < 100 && mgs.getMessageGroupCount() > 0) {
        Thread.sleep(100);
        if (n == 10) {
            TestUtils.getPropertyValue(this.output, "queue", Queue.class).clear();
        }
    }
    assertTrue("Group did not complete", n < 100);
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertEquals(Collections.singletonList(1), receive.getPayload());
    assertNull(this.discard.receive(0));
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) Matchers.containsString(org.hamcrest.Matchers.containsString) Queue(java.util.Queue) Test(org.junit.Test)

Example 7 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testNonExistingEmptyMessageGroup.

@Test
@MongoDbAvailable
public void testNonExistingEmptyMessageGroup() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = getMessageGroupStore();
    store.addMessagesToGroup(1, new GenericMessage<Object>("foo"));
    MessageGroup messageGroup = store.getMessageGroup(1);
    assertNotNull(messageGroup);
    assertThat(messageGroup.getClass().getName(), containsString("PersistentMessageGroup"));
    assertEquals(1, messageGroup.size());
}
Also used : MongoClient(com.mongodb.MongoClient) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MessageGroup(org.springframework.integration.store.MessageGroup) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 8 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testMessageGroupWithAddedMessageUUIDGroupIdAndUUIDHeader.

@Test
@MongoDbAvailable
public void testMessageGroupWithAddedMessageUUIDGroupIdAndUUIDHeader() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    MessageStore messageStore = this.getMessageStore();
    Object id = UUID.randomUUID();
    MessageGroup messageGroup = store.getMessageGroup(id);
    UUID uuidA = UUID.randomUUID();
    Message<?> messageA = MessageBuilder.withPayload("A").setHeader("foo", uuidA).build();
    UUID uuidB = UUID.randomUUID();
    Message<?> messageB = MessageBuilder.withPayload("B").setHeader("foo", uuidB).build();
    store.addMessagesToGroup(id, messageA);
    messageGroup = store.addMessageToGroup(id, messageB);
    assertNotNull(messageGroup);
    assertEquals(2, messageGroup.size());
    Message<?> retrievedMessage = messageStore.getMessage(messageA.getHeaders().getId());
    assertNotNull(retrievedMessage);
    assertEquals(retrievedMessage.getHeaders().getId(), messageA.getHeaders().getId());
    // ensure that 'message_group' header that is only used internally is not propagated
    assertNull(retrievedMessage.getHeaders().get("message_group"));
    Object fooHeader = retrievedMessage.getHeaders().get("foo");
    assertTrue(fooHeader instanceof UUID);
    assertEquals(uuidA, fooHeader);
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) MongoClient(com.mongodb.MongoClient) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MessageGroup(org.springframework.integration.store.MessageGroup) UUID(java.util.UUID) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 9 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testWithMessageHistory.

@Test
@MongoDbAvailable
public void testWithMessageHistory() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    store.getMessageGroup(1);
    Message<?> message = new GenericMessage<String>("Hello");
    DirectChannel fooChannel = new DirectChannel();
    fooChannel.setBeanName("fooChannel");
    DirectChannel barChannel = new DirectChannel();
    barChannel.setBeanName("barChannel");
    message = MessageHistory.write(message, fooChannel);
    message = MessageHistory.write(message, barChannel);
    store.addMessagesToGroup(1, message);
    MessageGroup group = store.getMessageGroup(1);
    assertNotNull(group);
    Collection<Message<?>> messages = group.getMessages();
    assertTrue(!messages.isEmpty());
    message = messages.iterator().next();
    MessageHistory messageHistory = MessageHistory.read(message);
    assertNotNull(messageHistory);
    assertEquals(2, messageHistory.size());
    Properties fooChannelHistory = messageHistory.get(0);
    assertEquals("fooChannel", fooChannelHistory.get("name"));
    assertEquals("channel", fooChannelHistory.get("type"));
}
Also used : MongoClient(com.mongodb.MongoClient) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageGroup(org.springframework.integration.store.MessageGroup) Properties(java.util.Properties) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 10 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testSameMessageMultipleGroupsPoll.

@Test
@MongoDbAvailable
public void testSameMessageMultipleGroupsPoll() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    Message<?> messageA = new GenericMessage<String>("A");
    store.addMessagesToGroup(1, messageA);
    store.addMessagesToGroup(2, messageA);
    store.addMessagesToGroup(3, messageA);
    store.addMessagesToGroup(4, messageA);
    assertEquals(1, store.messageGroupSize(1));
    assertEquals(1, store.messageGroupSize(2));
    assertEquals(1, store.messageGroupSize(3));
    assertEquals(1, store.messageGroupSize(4));
    store.pollMessageFromGroup(3);
    assertEquals(1, store.messageGroupSize(1));
    assertEquals(1, store.messageGroupSize(2));
    assertEquals(0, store.messageGroupSize(3));
    assertEquals(1, store.messageGroupSize(4));
    store.pollMessageFromGroup(4);
    assertEquals(1, store.messageGroupSize(1));
    assertEquals(1, store.messageGroupSize(2));
    assertEquals(0, store.messageGroupSize(3));
    assertEquals(0, store.messageGroupSize(4));
    store.pollMessageFromGroup(2);
    assertEquals(1, store.messageGroupSize(1));
    assertEquals(0, store.messageGroupSize(2));
    assertEquals(0, store.messageGroupSize(3));
    assertEquals(0, store.messageGroupSize(4));
    store.pollMessageFromGroup(1);
    assertEquals(0, store.messageGroupSize(1));
    assertEquals(0, store.messageGroupSize(2));
    assertEquals(0, store.messageGroupSize(3));
    assertEquals(0, store.messageGroupSize(4));
}
Also used : MongoClient(com.mongodb.MongoClient) GenericMessage(org.springframework.messaging.support.GenericMessage) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Aggregations

MessageGroupStore (org.springframework.integration.store.MessageGroupStore)40 Test (org.junit.Test)37 MessageGroup (org.springframework.integration.store.MessageGroup)25 GenericMessage (org.springframework.messaging.support.GenericMessage)21 MongoDbAvailable (org.springframework.integration.mongodb.rules.MongoDbAvailable)17 AbstractBatchingMessageGroupStore (org.springframework.integration.store.AbstractBatchingMessageGroupStore)17 MongoClient (com.mongodb.MongoClient)16 SimpleMongoDbFactory (org.springframework.data.mongodb.core.SimpleMongoDbFactory)16 QueueChannel (org.springframework.integration.channel.QueueChannel)8 SimpleMessageStore (org.springframework.integration.store.SimpleMessageStore)8 Message (org.springframework.messaging.Message)8 Matchers.containsString (org.hamcrest.Matchers.containsString)7 MessageChannel (org.springframework.messaging.MessageChannel)7 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)6 ArrayList (java.util.ArrayList)5 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)5 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)5 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)5