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