use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.
the class ConfigurableMongoDbMessageStore method iterator.
@Override
public Iterator<MessageGroup> iterator() {
List<MessageGroup> messageGroups = new ArrayList<MessageGroup>();
Query query = Query.query(Criteria.where(MessageDocumentFields.GROUP_ID).exists(true));
Iterable<String> groupIds = mongoTemplate.getCollection(collectionName).distinct(MessageDocumentFields.GROUP_ID, query.getQueryObject(), String.class);
for (Object groupId : groupIds) {
messageGroups.add(getMessageGroup(groupId));
}
return messageGroups.iterator();
}
use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.
the class DelayerHandlerRescheduleIntegrationTests method testDelayerHandlerRescheduleWithRedisMessageStore.
@Test
@RedisAvailable
public void testDelayerHandlerRescheduleWithRedisMessageStore() throws Exception {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("DelayerHandlerRescheduleIntegrationTests-context.xml", 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);
Thread.sleep(10);
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);
context.close();
try {
context.getBean("input", MessageChannel.class);
fail("IllegalStateException expected");
} catch (Exception e) {
assertTrue(e instanceof IllegalStateException);
assertTrue(e.getMessage().contains("BeanFactory not initialized or already closed - call 'refresh'"));
}
assertEquals(1, messageStore.getMessageGroupCount());
assertEquals(delayerMessageGroupId, messageStore.iterator().next().getGroupId());
assertEquals(2, messageStore.messageGroupSize(delayerMessageGroupId));
assertEquals(2, messageStore.getMessageCountForAllMessageGroups());
MessageGroup messageGroup = messageStore.getMessageGroup(delayerMessageGroupId);
Message<?> messageInStore = messageGroup.getMessages().iterator().next();
Object payload = messageInStore.getPayload();
// INT-3049
assertTrue(payload instanceof DelayHandler.DelayedMessageWrapper);
assertEquals(message1, ((DelayHandler.DelayedMessageWrapper) payload).getOriginal());
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);
assertEquals(1, messageStore.getMessageGroupCount());
int n = 0;
while (n++ < 200 && messageStore.messageGroupSize(delayerMessageGroupId) > 0) {
Thread.sleep(100);
}
assertEquals(0, messageStore.messageGroupSize(delayerMessageGroupId));
messageStore.removeMessageGroup(delayerMessageGroupId);
context.close();
}
use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.
the class RedisMessageGroupStoreTests method testRemoveMessageGroup.
@Test
@RedisAvailable
public void testRemoveMessageGroup() {
RedisConnectionFactory jcf = getConnectionFactoryForTest();
RedisMessageStore store = new RedisMessageStore(jcf);
MessageGroup messageGroup = store.getMessageGroup(this.groupId);
Message<?> message = new GenericMessage<>("Hello");
messageGroup = store.addMessageToGroup(messageGroup.getGroupId(), message);
assertEquals(1, messageGroup.size());
store.removeMessageGroup(this.groupId);
MessageGroup messageGroupA = store.getMessageGroup(this.groupId);
assertNotSame(messageGroup, messageGroupA);
// assertEquals(0, messageGroupA.getMarked().size());
assertEquals(0, messageGroupA.getMessages().size());
assertEquals(0, messageGroupA.size());
// make sure the store is properly rebuild from Redis
store = new RedisMessageStore(jcf);
messageGroup = store.getMessageGroup(this.groupId);
assertEquals(0, messageGroup.getMessages().size());
assertEquals(0, messageGroup.size());
}
use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.
the class RedisMessageGroupStoreTests method testConcurrentModifications.
@Test
@RedisAvailable
@Ignore
public void testConcurrentModifications() throws Exception {
RedisConnectionFactory jcf = getConnectionFactoryForTest();
final RedisMessageStore store1 = new RedisMessageStore(jcf);
final RedisMessageStore store2 = new RedisMessageStore(jcf);
store1.removeMessageGroup(this.groupId);
final Message<?> message = new GenericMessage<>("1");
ExecutorService executor = null;
final List<Object> failures = new ArrayList<>();
for (int i = 0; i < 100; i++) {
executor = Executors.newCachedThreadPool();
executor.execute(() -> {
MessageGroup group = store1.addMessageToGroup(this.groupId, message);
if (group.getMessages().size() != 1) {
failures.add("ADD");
throw new AssertionFailedError("Failed on ADD");
}
});
executor.execute(() -> {
store2.removeMessagesFromGroup(this.groupId, message);
MessageGroup group = store2.getMessageGroup(this.groupId);
if (group.getMessages().size() != 0) {
failures.add("REMOVE");
throw new AssertionFailedError("Failed on Remove");
}
});
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
// ensures that if ADD thread executed after REMOVE, the store is empty for the next cycle
store2.removeMessagesFromGroup(1, message);
}
assertTrue(failures.size() == 0);
}
use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.
the class RedisMessageGroupStoreTests method testLastReleasedSequenceNumber.
@Test
@RedisAvailable
public void testLastReleasedSequenceNumber() {
RedisConnectionFactory jcf = getConnectionFactoryForTest();
RedisMessageStore store = new RedisMessageStore(jcf);
MessageGroup messageGroup = store.getMessageGroup(this.groupId);
Message<?> message = new GenericMessage<>("Hello");
messageGroup = store.addMessageToGroup(messageGroup.getGroupId(), message);
store.setLastReleasedSequenceNumberForGroup(messageGroup.getGroupId(), 5);
messageGroup = store.getMessageGroup(this.groupId);
assertEquals(5, messageGroup.getLastReleasedMessageSequenceNumber());
}
Aggregations