use of org.springframework.integration.store.MessageGroupStore in project spring-integration by spring-projects.
the class AbstractMongoDbMessageGroupStoreTests method testLastReleasedSequenceNumber.
@Test
@MongoDbAvailable
public void testLastReleasedSequenceNumber() throws Exception {
this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
MessageGroupStore store = this.getMessageGroupStore();
MessageGroup messageGroup = store.getMessageGroup(1);
assertNotNull(messageGroup);
Message<?> message = new GenericMessage<String>("Hello");
store.addMessagesToGroup(messageGroup.getGroupId(), message);
store.setLastReleasedSequenceNumberForGroup(messageGroup.getGroupId(), 5);
messageGroup = store.getMessageGroup(1);
assertEquals(5, messageGroup.getLastReleasedMessageSequenceNumber());
}
use of org.springframework.integration.store.MessageGroupStore in project spring-integration by spring-projects.
the class DelayerHandlerRescheduleIntegrationTests method testDelayerHandlerRescheduleWithMongoDbMessageStore.
@SuppressWarnings("unchecked")
private void testDelayerHandlerRescheduleWithMongoDbMessageStore(String config) throws Exception {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(config, this.getClass());
MessageChannel input = context.getBean("input", MessageChannel.class);
MessageGroupStore messageStore = context.getBean("messageStore", MessageGroupStore.class);
String delayerMessageGroupId = DELAYER_ID + ".messageGroupId";
messageStore.removeMessageGroup(delayerMessageGroupId);
Message<String> message1 = MessageBuilder.withPayload("test1").build();
input.send(message1);
input.send(MessageBuilder.withPayload("test2").build());
// Emulate restart and check DB state before next start
// Interrupt taskScheduler as quickly as possible
ThreadPoolTaskScheduler taskScheduler = (ThreadPoolTaskScheduler) IntegrationContextUtils.getTaskScheduler(context);
taskScheduler.shutdown();
taskScheduler.getScheduledExecutor().awaitTermination(10, TimeUnit.SECONDS);
assertEquals(2, messageStore.messageGroupSize(delayerMessageGroupId));
MessageGroup messageGroup = messageStore.getMessageGroup(delayerMessageGroupId);
Iterator<Message<?>> iterator = messageGroup.getMessages().iterator();
Message<?> messageInStore = iterator.next();
Object payload = messageInStore.getPayload();
// INT-3049
assertTrue(payload instanceof DelayHandler.DelayedMessageWrapper);
Message<String> original1 = (Message<String>) ((DelayHandler.DelayedMessageWrapper) payload).getOriginal();
messageInStore = iterator.next();
Message<String> original2 = (Message<String>) ((DelayHandler.DelayedMessageWrapper) messageInStore.getPayload()).getOriginal();
assertThat(message1, Matchers.anyOf(Matchers.is(original1), Matchers.is(original2)));
context.close();
context.refresh();
PollableChannel output = context.getBean("output", PollableChannel.class);
Message<?> message = output.receive(20000);
assertNotNull(message);
Object payload1 = message.getPayload();
message = output.receive(20000);
assertNotNull(message);
Object payload2 = message.getPayload();
assertNotSame(payload1, payload2);
messageStore = context.getBean("messageStore", MessageGroupStore.class);
assertEquals(0, messageStore.messageGroupSize(delayerMessageGroupId));
context.close();
}
use of org.springframework.integration.store.MessageGroupStore in project spring-integration by spring-projects.
the class ResequencerIntegrationTests method validateUnboundedResequencerLight.
@Test
public void validateUnboundedResequencerLight() {
MessageChannel inputChannel = context.getBean("resequencerLightInput", MessageChannel.class);
QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
EventDrivenConsumer edc = context.getBean("resequencerLight", EventDrivenConsumer.class);
ResequencingMessageHandler handler = TestUtils.getPropertyValue(edc, "handler", ResequencingMessageHandler.class);
MessageGroupStore store = TestUtils.getPropertyValue(handler, "messageStore", MessageGroupStore.class);
Message<?> message1 = MessageBuilder.withPayload("1").setCorrelationId("A").setSequenceNumber(1).build();
Message<?> message2 = MessageBuilder.withPayload("2").setCorrelationId("A").setSequenceNumber(2).setCorrelationId("A").setSequenceNumber(2).setHeader("foo", "foo").build();
Message<?> message3 = MessageBuilder.withPayload("3").setCorrelationId("A").setSequenceNumber(3).build();
Message<?> message4 = MessageBuilder.withPayload("4").setCorrelationId("A").setSequenceNumber(4).setHeader("foo", "foo").build();
Message<?> message5 = MessageBuilder.withPayload("5").setCorrelationId("A").setSequenceNumber(5).build();
Message<?> message6 = MessageBuilder.withPayload("6").setCorrelationId("A").setSequenceNumber(6).build();
inputChannel.send(message3);
assertNull(outputChannel.receive(0));
inputChannel.send(message1);
message1 = outputChannel.receive(0);
assertNotNull(message1);
assertEquals(1, new IntegrationMessageHeaderAccessor(message1).getSequenceNumber());
assertFalse(message1.getHeaders().containsKey("foo"));
inputChannel.send(message2);
message2 = outputChannel.receive(0);
message3 = outputChannel.receive(0);
assertNotNull(message2);
assertNotNull(message3);
assertEquals(2, new IntegrationMessageHeaderAccessor(message2).getSequenceNumber());
assertTrue(message2.getHeaders().containsKey("foo"));
assertEquals(3, new IntegrationMessageHeaderAccessor(message3).getSequenceNumber());
assertFalse(message3.getHeaders().containsKey("foo"));
inputChannel.send(message5);
assertNull(outputChannel.receive(0));
inputChannel.send(message6);
assertNull(outputChannel.receive(0));
inputChannel.send(message4);
message4 = outputChannel.receive(0);
message5 = outputChannel.receive(0);
message6 = outputChannel.receive(0);
assertNotNull(message4);
assertNotNull(message5);
assertNotNull(message6);
assertEquals(4, new IntegrationMessageHeaderAccessor(message4).getSequenceNumber());
assertTrue(message4.getHeaders().containsKey("foo"));
assertEquals(5, new IntegrationMessageHeaderAccessor(message5).getSequenceNumber());
assertFalse(message5.getHeaders().containsKey("foo"));
assertEquals(6, new IntegrationMessageHeaderAccessor(message6).getSequenceNumber());
assertFalse(message6.getHeaders().containsKey("foo"));
assertEquals(0, store.getMessageGroup("A").getMessages().size());
}
use of org.springframework.integration.store.MessageGroupStore in project spring-integration by spring-projects.
the class ResequencerIntegrationTests method validateUnboundedResequencerDeep.
@Test
public void validateUnboundedResequencerDeep() {
MessageChannel inputChannel = context.getBean("resequencerDeepInput", MessageChannel.class);
QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
EventDrivenConsumer edc = context.getBean("resequencerDeep", EventDrivenConsumer.class);
ResequencingMessageHandler handler = TestUtils.getPropertyValue(edc, "handler", ResequencingMessageHandler.class);
MessageGroupStore store = TestUtils.getPropertyValue(handler, "messageStore", MessageGroupStore.class);
Message<?> message1 = MessageBuilder.withPayload("1").setCorrelationId("A").setSequenceNumber(1).build();
Message<?> message2 = MessageBuilder.withPayload("2").setCorrelationId("A").setSequenceNumber(2).build();
Message<?> message3 = MessageBuilder.withPayload("3").setCorrelationId("A").setSequenceNumber(3).build();
inputChannel.send(message3);
assertNull(outputChannel.receive(0));
inputChannel.send(message1);
assertNotNull(outputChannel.receive(0));
inputChannel.send(message2);
assertNotNull(outputChannel.receive(0));
assertNotNull(outputChannel.receive(0));
assertEquals(0, store.getMessageGroup("A").getMessages().size());
}
use of org.springframework.integration.store.MessageGroupStore in project spring-integration by spring-projects.
the class AbstractCorrelatingMessageHandlerTests method testReaperDoesntReapAProcessingGroup.
// INT-2751
@Test
public void testReaperDoesntReapAProcessingGroup() throws Exception {
final MessageGroupStore groupStore = new SimpleMessageStore();
final CountDownLatch waitForSendLatch = new CountDownLatch(1);
final CountDownLatch waitReapStartLatch = new CountDownLatch(1);
final CountDownLatch waitReapCompleteLatch = new CountDownLatch(1);
AbstractCorrelatingMessageHandler handler = new AbstractCorrelatingMessageHandler(group -> group, groupStore) {
@Override
protected void afterRelease(MessageGroup group, Collection<Message<?>> completedMessages) {
}
};
handler.setReleasePartialSequences(true);
/*
* Runs "reap" when group 'bar' is in completion
*/
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(() -> {
try {
waitReapStartLatch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
}
waitForSendLatch.countDown();
try {
Thread.sleep(100);
} catch (InterruptedException e2) {
Thread.currentThread().interrupt();
}
groupStore.expireMessageGroups(50);
waitReapCompleteLatch.countDown();
});
final List<Message<?>> outputMessages = new ArrayList<Message<?>>();
handler.setOutputChannel((message, timeout) -> {
/*
* Executes when group 'bar' completes normally
*/
outputMessages.add(message);
// wake reaper
waitReapStartLatch.countDown();
try {
waitForSendLatch.await(10, TimeUnit.SECONDS);
// wait a little longer for reaper to grab groups
Thread.sleep(2000);
// simulate tx commit
groupStore.removeMessageGroup("bar");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return true;
});
handler.setReleaseStrategy(group -> group.size() == 2);
QueueChannel discards = new QueueChannel();
handler.setDiscardChannel(discards);
handler.setSendPartialResultOnExpiry(true);
Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId("qux").build();
// partial group that will be reaped
handler.handleMessage(message);
message = MessageBuilder.withPayload("foo").setCorrelationId("bar").build();
// full group that should not be reaped
handler.handleMessage(message);
message = MessageBuilder.withPayload("baz").setCorrelationId("bar").build();
handler.handleMessage(message);
assertTrue(waitReapCompleteLatch.await(10, TimeUnit.SECONDS));
// Before INT-2751 we got bar + bar + qux
// bar + qux
assertEquals(2, outputMessages.size());
// normal release
// 'bar'
assertEquals(2, ((MessageGroup) outputMessages.get(0).getPayload()).size());
// reaper release
// 'qux'
assertEquals(1, ((MessageGroup) outputMessages.get(1).getPayload()).size());
assertNull(discards.receive(0));
exec.shutdownNow();
}
Aggregations