Search in sources :

Example 91 with MessageGroup

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();
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) ArrayList(java.util.ArrayList)

Example 92 with MessageGroup

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();
}
Also used : DelayHandler(org.springframework.integration.handler.DelayHandler) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) MessageGroup(org.springframework.integration.store.MessageGroup) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 93 with MessageGroup

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());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 94 with MessageGroup

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);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) AssertionFailedError(junit.framework.AssertionFailedError) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 95 with MessageGroup

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());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

MessageGroup (org.springframework.integration.store.MessageGroup)98 Test (org.junit.Test)79 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)54 GenericMessage (org.springframework.messaging.support.GenericMessage)36 MessageGroupStore (org.springframework.integration.store.MessageGroupStore)25 Message (org.springframework.messaging.Message)20 ArrayList (java.util.ArrayList)19 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)15 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)14 MongoDbAvailable (org.springframework.integration.mongodb.rules.MongoDbAvailable)13 AbstractBatchingMessageGroupStore (org.springframework.integration.store.AbstractBatchingMessageGroupStore)13 MongoClient (com.mongodb.MongoClient)12 SimpleMongoDbFactory (org.springframework.data.mongodb.core.SimpleMongoDbFactory)12 Transactional (org.springframework.transaction.annotation.Transactional)10 LinkedList (java.util.LinkedList)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 UUID (java.util.UUID)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 HashMap (java.util.HashMap)5