Search in sources :

Example 1 with SimpleMessageStore

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

the class DelayHandler method releaseMessageAfterDelay.

private void releaseMessageAfterDelay(final Message<?> message, long delay) {
    Message<?> delayedMessage = message;
    DelayedMessageWrapper messageWrapper = null;
    if (message.getPayload() instanceof DelayedMessageWrapper) {
        messageWrapper = (DelayedMessageWrapper) message.getPayload();
    } else {
        messageWrapper = new DelayedMessageWrapper(message, System.currentTimeMillis());
        delayedMessage = getMessageBuilderFactory().withPayload(messageWrapper).copyHeaders(message.getHeaders()).build();
        this.messageStore.addMessageToGroup(this.messageGroupId, delayedMessage);
    }
    Runnable releaseTask;
    if (this.messageStore instanceof SimpleMessageStore) {
        final Message<?> messageToSchedule = delayedMessage;
        releaseTask = () -> releaseMessage(messageToSchedule);
    } else {
        final UUID messageId = delayedMessage.getHeaders().getId();
        releaseTask = () -> {
            Message<?> messageToRelease = getMessageById(messageId);
            if (messageToRelease != null) {
                releaseMessage(messageToRelease);
            }
        };
    }
    getTaskScheduler().schedule(releaseTask, new Date(messageWrapper.getRequestDate() + delay));
}
Also used : SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) UUID(java.util.UUID) Date(java.util.Date)

Example 2 with SimpleMessageStore

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

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

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

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

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