Search in sources :

Example 21 with MessageGroupStore

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

the class AbstractCorrelatingMessageHandlerTests method testDontReapIfAlreadyCompleteAfterRefetch.

/*
	 * INT-3216 - Verifies the complete early exit is taken after a refresh.
	 */
@Test
public void testDontReapIfAlreadyCompleteAfterRefetch() throws Exception {
    MessageGroupProcessor mgp = new DefaultAggregatingMessageGroupProcessor();
    AggregatingMessageHandler handler = new AggregatingMessageHandler(mgp);
    handler.setReleaseStrategy(group -> true);
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    MessageGroupStore mgs = TestUtils.getPropertyValue(handler, "messageStore", MessageGroupStore.class);
    mgs.addMessagesToGroup("foo", new GenericMessage<String>("foo"));
    MessageGroup group = new SimpleMessageGroup(mgs.getMessageGroup("foo"));
    mgs.completeGroup("foo");
    mgs = spy(mgs);
    new DirectFieldAccessor(handler).setPropertyValue("messageStore", mgs);
    Method forceComplete = AbstractCorrelatingMessageHandler.class.getDeclaredMethod("forceComplete", MessageGroup.class);
    forceComplete.setAccessible(true);
    MessageGroup groupInStore = (MessageGroup) TestUtils.getPropertyValue(mgs, "groupIdToMessageGroup", Map.class).get("foo");
    assertTrue(groupInStore.isComplete());
    assertFalse(group.isComplete());
    new DirectFieldAccessor(group).setPropertyValue("lastModified", groupInStore.getLastModified());
    forceComplete.invoke(handler, group);
    verify(mgs).getMessageGroup("foo");
    assertNull(outputChannel.receive(0));
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Method(java.lang.reflect.Method) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Test(org.junit.Test)

Example 22 with MessageGroupStore

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

the class AbstractCorrelatingMessageHandlerTests method testDontReapIfNewGroupFoundDuringRefetch.

/*
	 * INT-3216 - Verifies we don't complete if it's a completely new group (different timestamp).
	 */
@Test
public void testDontReapIfNewGroupFoundDuringRefetch() throws Exception {
    MessageGroupProcessor mgp = new DefaultAggregatingMessageGroupProcessor();
    AggregatingMessageHandler handler = new AggregatingMessageHandler(mgp);
    handler.setReleaseStrategy(group -> true);
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    MessageGroupStore mgs = TestUtils.getPropertyValue(handler, "messageStore", MessageGroupStore.class);
    mgs.addMessagesToGroup("foo", new GenericMessage<String>("foo"));
    MessageGroup group = new SimpleMessageGroup(mgs.getMessageGroup("foo"));
    mgs = spy(mgs);
    new DirectFieldAccessor(handler).setPropertyValue("messageStore", mgs);
    Method forceComplete = AbstractCorrelatingMessageHandler.class.getDeclaredMethod("forceComplete", MessageGroup.class);
    forceComplete.setAccessible(true);
    MessageGroup groupInStore = (MessageGroup) TestUtils.getPropertyValue(mgs, "groupIdToMessageGroup", Map.class).get("foo");
    assertFalse(groupInStore.isComplete());
    assertFalse(group.isComplete());
    DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(group);
    directFieldAccessor.setPropertyValue("lastModified", groupInStore.getLastModified());
    directFieldAccessor.setPropertyValue("timestamp", groupInStore.getTimestamp() - 1);
    forceComplete.invoke(handler, group);
    verify(mgs).getMessageGroup("foo");
    assertNull(outputChannel.receive(0));
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Method(java.lang.reflect.Method) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Test(org.junit.Test)

Example 23 with MessageGroupStore

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

the class AbstractCorrelatingMessageHandlerTests method testReaperReapsAnEmptyGroupAfterConfiguredDelay.

// INT-2833
@Test
public void testReaperReapsAnEmptyGroupAfterConfiguredDelay() throws Exception {
    final MessageGroupStore groupStore = new SimpleMessageStore();
    AggregatingMessageHandler handler = new AggregatingMessageHandler(group -> group, groupStore);
    final List<Message<?>> outputMessages = new ArrayList<Message<?>>();
    handler.setOutputChannel((message, timeout) -> {
        /*
			 * Executes when group 'bar' completes normally
			 */
        outputMessages.add(message);
        return true;
    });
    handler.setReleaseStrategy(group -> group.size() == 1);
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId("bar").build();
    handler.handleMessage(message);
    handler.setMinimumTimeoutForEmptyGroups(10_000);
    assertEquals(1, outputMessages.size());
    assertEquals(1, TestUtils.getPropertyValue(handler, "messageStore.groupIdToMessageGroup", Map.class).size());
    groupStore.expireMessageGroups(0);
    assertEquals(1, TestUtils.getPropertyValue(handler, "messageStore.groupIdToMessageGroup", Map.class).size());
    handler.setMinimumTimeoutForEmptyGroups(10);
    int n = 0;
    while (n++ < 200) {
        groupStore.expireMessageGroups(0);
        if (TestUtils.getPropertyValue(handler, "messageStore.groupIdToMessageGroup", Map.class).size() > 0) {
            Thread.sleep(50);
        } else {
            break;
        }
    }
    assertTrue(n < 200);
    assertEquals(0, TestUtils.getPropertyValue(handler, "messageStore.groupIdToMessageGroup", Map.class).size());
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 24 with MessageGroupStore

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

the class AbstractCorrelatingMessageHandlerTests method testDontReapIfAlreadyComplete.

@Test
public /* INT-3216 */
void testDontReapIfAlreadyComplete() throws Exception {
    MessageGroupProcessor mgp = new DefaultAggregatingMessageGroupProcessor();
    AggregatingMessageHandler handler = new AggregatingMessageHandler(mgp);
    handler.setReleaseStrategy(group -> true);
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    MessageGroupStore mgs = TestUtils.getPropertyValue(handler, "messageStore", MessageGroupStore.class);
    mgs.addMessagesToGroup("foo", new GenericMessage<String>("foo"));
    mgs.completeGroup("foo");
    mgs = spy(mgs);
    new DirectFieldAccessor(handler).setPropertyValue("messageStore", mgs);
    Method forceComplete = AbstractCorrelatingMessageHandler.class.getDeclaredMethod("forceComplete", MessageGroup.class);
    forceComplete.setAccessible(true);
    MessageGroup group = (MessageGroup) TestUtils.getPropertyValue(mgs, "groupIdToMessageGroup", Map.class).get("foo");
    assertTrue(group.isComplete());
    forceComplete.invoke(handler, group);
    verify(mgs, never()).getMessageGroup("foo");
    assertNull(outputChannel.receive(0));
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 25 with MessageGroupStore

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

the class AbstractCorrelatingMessageHandlerTests method testScheduleRemoveAnEmptyGroupAfterConfiguredDelay.

@Test
public void testScheduleRemoveAnEmptyGroupAfterConfiguredDelay() throws Exception {
    final MessageGroupStore groupStore = new SimpleMessageStore();
    AggregatingMessageHandler handler = new AggregatingMessageHandler(group -> group, groupStore);
    final List<Message<?>> outputMessages = new ArrayList<Message<?>>();
    handler.setOutputChannel((message, timeout) -> {
        /*
			 * Executes when group 'bar' completes normally
			 */
        outputMessages.add(message);
        return true;
    });
    handler.setReleaseStrategy(group -> group.size() == 1);
    handler.setMinimumTimeoutForEmptyGroups(100);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.afterPropertiesSet();
    handler.setTaskScheduler(taskScheduler);
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId("bar").build();
    handler.handleMessage(message);
    assertEquals(1, outputMessages.size());
    assertEquals(1, TestUtils.getPropertyValue(handler, "messageStore.groupIdToMessageGroup", Map.class).size());
    Thread.sleep(100);
    int n = 0;
    while (TestUtils.getPropertyValue(handler, "messageStore.groupIdToMessageGroup", Map.class).size() > 0 && n++ < 200) {
        Thread.sleep(50);
    }
    assertTrue(n < 200);
    assertEquals(0, TestUtils.getPropertyValue(handler, "messageStore.groupIdToMessageGroup", Map.class).size());
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArrayList(java.util.ArrayList) Map(java.util.Map) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) 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