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));
}
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());
}
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));
}
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());
}
}
}
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);
}
}
}
Aggregations