Search in sources :

Example 36 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testMessageGroupWithAddedMessagePrimitiveGroupId.

@Test
@MongoDbAvailable
public void testMessageGroupWithAddedMessagePrimitiveGroupId() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    MessageStore messageStore = this.getMessageStore();
    MessageGroup messageGroup = store.getMessageGroup(1);
    Message<?> messageA = new GenericMessage<String>("A");
    Message<?> messageB = new GenericMessage<String>("B");
    store.addMessagesToGroup(1, messageA);
    messageGroup = store.addMessageToGroup(1, 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"));
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) 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) MessageGroup(org.springframework.integration.store.MessageGroup) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 37 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testMessageGroupMarkingMessage.

@Test
@MongoDbAvailable
public void testMessageGroupMarkingMessage() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    MessageGroup messageGroup = store.getMessageGroup(1);
    Message<?> messageA = new GenericMessage<String>("A");
    Message<?> messageB = new GenericMessage<String>("B");
    store.addMessagesToGroup(1, messageA);
    messageGroup = store.addMessageToGroup(1, messageB);
    assertNotNull(messageGroup);
    assertEquals(2, messageGroup.size());
    store.removeMessagesFromGroup(1, messageA);
    messageGroup = store.getMessageGroup(1);
    assertEquals(1, messageGroup.size());
    // validate that the updates were propagated to Mongo as well
    store = this.getMessageGroupStore();
    messageGroup = store.getMessageGroup(1);
    assertEquals(1, messageGroup.size());
}
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) MessageGroup(org.springframework.integration.store.MessageGroup) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 38 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testSameMessageMultipleGroupsRemove.

@Test
@MongoDbAvailable
public void testSameMessageMultipleGroupsRemove() 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.removeMessagesFromGroup(3, messageA);
    assertEquals(1, store.messageGroupSize(1));
    assertEquals(1, store.messageGroupSize(2));
    assertEquals(0, store.messageGroupSize(3));
    assertEquals(1, store.messageGroupSize(4));
    store.removeMessagesFromGroup(4, messageA);
    assertEquals(1, store.messageGroupSize(1));
    assertEquals(1, store.messageGroupSize(2));
    assertEquals(0, store.messageGroupSize(3));
    assertEquals(0, store.messageGroupSize(4));
    store.removeMessagesFromGroup(2, messageA);
    assertEquals(1, store.messageGroupSize(1));
    assertEquals(0, store.messageGroupSize(2));
    assertEquals(0, store.messageGroupSize(3));
    assertEquals(0, store.messageGroupSize(4));
    store.removeMessagesFromGroup(1, messageA);
    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)

Example 39 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testPollMessages.

@Test
@MongoDbAvailable
public void testPollMessages() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    Message<?> messageA = new GenericMessage<String>("A");
    Message<?> messageB = new GenericMessage<String>("B");
    store.addMessagesToGroup(1, messageA);
    Thread.sleep(10);
    store.addMessagesToGroup(1, messageB);
    assertEquals(2, store.messageGroupSize(1));
    Message<?> out = store.pollMessageFromGroup(1);
    assertNotNull(out);
    assertEquals("A", out.getPayload());
    assertEquals(1, store.messageGroupSize(1));
    out = store.pollMessageFromGroup(1);
    assertEquals("B", out.getPayload());
    assertEquals(0, store.messageGroupSize(1));
}
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)

Example 40 with MessageGroupStore

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

the class DelayerHandlerRescheduleIntegrationTests method testDelayerHandlerRescheduleWithRedisMessageStore.

@Test
@RedisAvailable
public void testDelayerHandlerRescheduleWithRedisMessageStore() throws Exception {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("DelayerHandlerRescheduleIntegrationTests-context.xml", this.getClass());
    MessageChannel input = context.getBean("input", MessageChannel.class);
    MessageGroupStore messageStore = context.getBean("messageStore", MessageGroupStore.class);
    String delayerMessageGroupId = DELAYER_ID + ".messageGroupId";
    messageStore.removeMessageGroup(delayerMessageGroupId);
    Message<String> message1 = MessageBuilder.withPayload("test1").build();
    input.send(message1);
    Thread.sleep(10);
    input.send(MessageBuilder.withPayload("test2").build());
    // Emulate restart and check DB state before next start
    // Interrupt taskScheduler as quickly as possible
    ThreadPoolTaskScheduler taskScheduler = (ThreadPoolTaskScheduler) IntegrationContextUtils.getTaskScheduler(context);
    taskScheduler.shutdown();
    taskScheduler.getScheduledExecutor().awaitTermination(10, TimeUnit.SECONDS);
    context.close();
    try {
        context.getBean("input", MessageChannel.class);
        fail("IllegalStateException expected");
    } catch (Exception e) {
        assertTrue(e instanceof IllegalStateException);
        assertTrue(e.getMessage().contains("BeanFactory not initialized or already closed - call 'refresh'"));
    }
    assertEquals(1, messageStore.getMessageGroupCount());
    assertEquals(delayerMessageGroupId, messageStore.iterator().next().getGroupId());
    assertEquals(2, messageStore.messageGroupSize(delayerMessageGroupId));
    assertEquals(2, messageStore.getMessageCountForAllMessageGroups());
    MessageGroup messageGroup = messageStore.getMessageGroup(delayerMessageGroupId);
    Message<?> messageInStore = messageGroup.getMessages().iterator().next();
    Object payload = messageInStore.getPayload();
    // INT-3049
    assertTrue(payload instanceof DelayHandler.DelayedMessageWrapper);
    assertEquals(message1, ((DelayHandler.DelayedMessageWrapper) payload).getOriginal());
    context.refresh();
    PollableChannel output = context.getBean("output", PollableChannel.class);
    Message<?> message = output.receive(20000);
    assertNotNull(message);
    Object payload1 = message.getPayload();
    message = output.receive(20000);
    assertNotNull(message);
    Object payload2 = message.getPayload();
    assertNotSame(payload1, payload2);
    assertEquals(1, messageStore.getMessageGroupCount());
    int n = 0;
    while (n++ < 200 && messageStore.messageGroupSize(delayerMessageGroupId) > 0) {
        Thread.sleep(100);
    }
    assertEquals(0, messageStore.messageGroupSize(delayerMessageGroupId));
    messageStore.removeMessageGroup(delayerMessageGroupId);
    context.close();
}
Also used : DelayHandler(org.springframework.integration.handler.DelayHandler) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) MessageGroup(org.springframework.integration.store.MessageGroup) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

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