Search in sources :

Example 11 with MessageGroup

use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.

the class CorrelatingMessageBarrier method receive.

@Override
public Message<Object> receive() {
    for (Object key : this.correlationLocks.keySet()) {
        Object lock = getLock(key);
        synchronized (lock) {
            MessageGroup group = this.store.getMessageGroup(key);
            // group might be removed by another thread
            if (group != null) {
                if (this.releaseStrategy.canRelease(group)) {
                    Message<?> nextMessage = null;
                    Iterator<Message<?>> messages = group.getMessages().iterator();
                    if (messages.hasNext()) {
                        nextMessage = messages.next();
                        this.store.removeMessagesFromGroup(key, nextMessage);
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Released message for key [%s]: %s.", key, nextMessage));
                        }
                    } else {
                        remove(key);
                    }
                    @SuppressWarnings("unchecked") Message<Object> result = (Message<Object>) nextMessage;
                    return result;
                }
            }
        }
    }
    return null;
}
Also used : Message(org.springframework.messaging.Message) MessageGroup(org.springframework.integration.store.MessageGroup)

Example 12 with MessageGroup

use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.

the class MethodInvokingReleaseStrategyTests method testAdapterWithNonParameterizedMessageListBasedMethod.

@Test
public void testAdapterWithNonParameterizedMessageListBasedMethod() {
    class TestReleaseStrategy {

        @SuppressWarnings("unused")
        public boolean checkCompletenessOnNonParameterizedListOfMessages(List<Message<?>> messages) {
            Assert.assertTrue(messages.size() > 0);
            return messages.size() > new IntegrationMessageHeaderAccessor(messages.iterator().next()).getSequenceSize();
        }
    }
    ReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "checkCompletenessOnNonParameterizedListOfMessages");
    MessageGroup messages = createListOfMessages(3);
    Assert.assertTrue(adapter.canRelease(messages));
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 13 with MessageGroup

use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.

the class MethodInvokingReleaseStrategyTests method testAdapterWithPojoBasedMethod.

@Test
public void testAdapterWithPojoBasedMethod() {
    class TestReleaseStrategy {

        @SuppressWarnings("unused")
        public // the data
        boolean checkCompletenessOnListOfStrings(List<String> messages) {
            StringBuffer buffer = new StringBuffer();
            for (String content : messages) {
                buffer.append(content);
            }
            return buffer.length() >= 9;
        }
    }
    ReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "checkCompletenessOnListOfStrings");
    MessageGroup messages = createListOfMessages(3);
    Assert.assertTrue(adapter.canRelease(messages));
}
Also used : MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 14 with MessageGroup

use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.

the class AbstractMongoDbMessageGroupStoreTests method testNonExistingEmptyMessageGroup.

@Test
@MongoDbAvailable
public void testNonExistingEmptyMessageGroup() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = getMessageGroupStore();
    store.addMessagesToGroup(1, new GenericMessage<Object>("foo"));
    MessageGroup messageGroup = store.getMessageGroup(1);
    assertNotNull(messageGroup);
    assertThat(messageGroup.getClass().getName(), containsString("PersistentMessageGroup"));
    assertEquals(1, messageGroup.size());
}
Also used : MongoClient(com.mongodb.MongoClient) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MessageGroup(org.springframework.integration.store.MessageGroup) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 15 with MessageGroup

use of org.springframework.integration.store.MessageGroup in project spring-integration by spring-projects.

the class AbstractMongoDbMessageGroupStoreTests method testMessageGroupWithAddedMessageUUIDGroupIdAndUUIDHeader.

@Test
@MongoDbAvailable
public void testMessageGroupWithAddedMessageUUIDGroupIdAndUUIDHeader() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    MessageStore messageStore = this.getMessageStore();
    Object id = UUID.randomUUID();
    MessageGroup messageGroup = store.getMessageGroup(id);
    UUID uuidA = UUID.randomUUID();
    Message<?> messageA = MessageBuilder.withPayload("A").setHeader("foo", uuidA).build();
    UUID uuidB = UUID.randomUUID();
    Message<?> messageB = MessageBuilder.withPayload("B").setHeader("foo", uuidB).build();
    store.addMessagesToGroup(id, messageA);
    messageGroup = store.addMessageToGroup(id, messageB);
    assertNotNull(messageGroup);
    assertEquals(2, messageGroup.size());
    Message<?> retrievedMessage = messageStore.getMessage(messageA.getHeaders().getId());
    assertNotNull(retrievedMessage);
    assertEquals(retrievedMessage.getHeaders().getId(), messageA.getHeaders().getId());
    // ensure that 'message_group' header that is only used internally is not propagated
    assertNull(retrievedMessage.getHeaders().get("message_group"));
    Object fooHeader = retrievedMessage.getHeaders().get("foo");
    assertTrue(fooHeader instanceof UUID);
    assertEquals(uuidA, fooHeader);
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) MongoClient(com.mongodb.MongoClient) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MessageGroup(org.springframework.integration.store.MessageGroup) UUID(java.util.UUID) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

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