Search in sources :

Example 1 with MessageGroupStore

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

the class AbstractCorrelatingMessageHandlerTests method testReaperReapsAnEmptyGroup.

// INT-2833
@Test
public void testReaperReapsAnEmptyGroup() 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);
    assertEquals(1, outputMessages.size());
    assertEquals(1, TestUtils.getPropertyValue(handler, "messageStore.groupIdToMessageGroup", Map.class).size());
    groupStore.expireMessageGroups(0);
    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 2 with MessageGroupStore

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

the class AbstractCorrelatingMessageHandlerTests method testReapWithChangeInSameMillisecond.

@Test
public void testReapWithChangeInSameMillisecond() 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);
    Method forceComplete = AbstractCorrelatingMessageHandler.class.getDeclaredMethod("forceComplete", MessageGroup.class);
    forceComplete.setAccessible(true);
    GenericMessage<String> secondMessage = new GenericMessage<String>("bar");
    mgs.addMessagesToGroup("foo", new GenericMessage<String>("foo"), secondMessage);
    MessageGroup group = mgs.getMessageGroup("foo");
    // remove a message
    mgs.removeMessagesFromGroup("foo", secondMessage);
    // force lastModified to be the same
    MessageGroup groupNow = mgs.getMessageGroup("foo");
    new DirectFieldAccessor(group).setPropertyValue("lastModified", groupNow.getLastModified());
    forceComplete.invoke(handler, group);
    Message<?> message = outputChannel.receive(0);
    assertNotNull(message);
    Collection<?> payload = (Collection<?>) message.getPayload();
    assertEquals(1, payload.size());
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Method(java.lang.reflect.Method) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Collection(java.util.Collection) Test(org.junit.Test)

Example 3 with MessageGroupStore

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

the class DelayHandlerTests method testReschedulePersistedMessagesOnStartup.

// INT-1132
@Test
public void testReschedulePersistedMessagesOnStartup() throws Exception {
    MessageGroupStore messageGroupStore = new SimpleMessageStore();
    this.delayHandler.setDefaultDelay(2000);
    this.delayHandler.setMessageStore(messageGroupStore);
    this.startDelayerHandler();
    Message<?> message = MessageBuilder.withPayload("test").build();
    this.input.send(message);
    Thread.sleep(100);
    // emulate restart
    this.taskScheduler.destroy();
    assertEquals(1, messageGroupStore.getMessageGroupCount());
    assertEquals(DELAYER_MESSAGE_GROUP_ID, messageGroupStore.iterator().next().getGroupId());
    assertEquals(1, messageGroupStore.messageGroupSize(DELAYER_MESSAGE_GROUP_ID));
    assertEquals(1, messageGroupStore.getMessageCountForAllMessageGroups());
    MessageGroup messageGroup = messageGroupStore.getMessageGroup(DELAYER_MESSAGE_GROUP_ID);
    Message<?> messageInStore = messageGroup.getMessages().iterator().next();
    Object payload = messageInStore.getPayload();
    assertEquals("DelayedMessageWrapper", payload.getClass().getSimpleName());
    assertEquals(message.getPayload(), TestUtils.getPropertyValue(payload, "original.payload"));
    this.taskScheduler.afterPropertiesSet();
    this.delayHandler = new DelayHandler(DELAYER_MESSAGE_GROUP_ID, this.taskScheduler);
    this.delayHandler.setOutputChannel(output);
    this.delayHandler.setDefaultDelay(200);
    this.delayHandler.setMessageStore(messageGroupStore);
    this.delayHandler.setBeanFactory(mock(BeanFactory.class));
    this.startDelayerHandler();
    waitForLatch(10000);
    assertSame(message.getPayload(), this.resultHandler.lastMessage.getPayload());
    assertNotSame(Thread.currentThread(), this.resultHandler.lastThread);
    assertEquals(1, messageGroupStore.getMessageGroupCount());
    assertEquals(0, messageGroupStore.messageGroupSize(DELAYER_MESSAGE_GROUP_ID));
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) MessageGroup(org.springframework.integration.store.MessageGroup) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 4 with MessageGroupStore

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

the class AggregatingMessageHandler method afterRelease.

/**
 * Complete the group and remove all its messages.
 * If the {@link #expireGroupsUponCompletion} is true, then remove group fully.
 * @param messageGroup the group to clean up.
 * @param completedMessages The completed messages. Ignored in this implementation.
 */
@Override
protected void afterRelease(MessageGroup messageGroup, Collection<Message<?>> completedMessages) {
    Object groupId = messageGroup.getGroupId();
    MessageGroupStore messageStore = getMessageStore();
    messageStore.completeGroup(groupId);
    if (this.expireGroupsUponCompletion) {
        remove(messageGroup);
    } else {
        if (messageStore instanceof SimpleMessageStore) {
            ((SimpleMessageStore) messageStore).clearMessageGroup(groupId);
        } else {
            messageStore.removeMessagesFromGroup(groupId, messageGroup.getMessages());
        }
    }
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore)

Example 5 with MessageGroupStore

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

the class ResequencingMessageHandler method afterRelease.

/**
 * Perform group removal if its {@code size} is equal to the {@code sequenceSize}.
 * Remove {@code completedMessages} from the group if it isn't null.
 * @param messageGroup the group to clean up.
 * @param completedMessages The completed messages.
 * @param timeout True if the release/discard was due to a timeout.
 */
@Override
protected void afterRelease(MessageGroup messageGroup, Collection<Message<?>> completedMessages, boolean timeout) {
    int size = messageGroup.size();
    int sequenceSize = messageGroup.getSequenceSize();
    // If there is no sequence then it must be incomplete or unbounded
    if (sequenceSize > 0 && sequenceSize == size) {
        remove(messageGroup);
    } else {
        Object groupId = messageGroup.getGroupId();
        MessageGroupStore messageStore = getMessageStore();
        if (completedMessages != null) {
            int lastReleasedSequenceNumber = findLastReleasedSequenceNumber(groupId, completedMessages);
            messageStore.setLastReleasedSequenceNumberForGroup(groupId, lastReleasedSequenceNumber);
            if (messageStore instanceof SimpleMessageStore && completedMessages.size() == messageGroup.size()) {
                ((SimpleMessageStore) messageStore).clearMessageGroup(groupId);
            } else {
                messageStore.removeMessagesFromGroup(groupId, completedMessages);
            }
        }
        if (timeout) {
            messageStore.completeGroup(groupId);
        }
    }
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore)

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