Search in sources :

Example 36 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testCompleteMessageGroup.

@Test
@RedisAvailable
public void testCompleteMessageGroup() {
    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.completeGroup(messageGroup.getGroupId());
    messageGroup = store.getMessageGroup(this.groupId);
    assertTrue(messageGroup.isComplete());
}
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 37 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testJsonSerialization.

@Test
@RedisAvailable
public void testJsonSerialization() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper();
    GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    Message<?> genericMessage = new GenericMessage<>(new Date());
    Message<?> mutableMessage = new MutableMessage<>(UUID.randomUUID());
    Message<?> adviceMessage = new AdviceMessage<>("foo", genericMessage);
    ErrorMessage errorMessage = new ErrorMessage(new RuntimeException("test exception"), mutableMessage);
    store.addMessagesToGroup(this.groupId, genericMessage, mutableMessage, adviceMessage, errorMessage);
    MessageGroup messageGroup = store.getMessageGroup(this.groupId);
    assertEquals(4, messageGroup.size());
    List<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
    assertEquals(genericMessage, messages.get(0));
    assertEquals(mutableMessage, messages.get(1));
    assertEquals(adviceMessage, messages.get(2));
    Message<?> errorMessageResult = messages.get(3);
    assertEquals(errorMessage.getHeaders(), errorMessageResult.getHeaders());
    assertThat(errorMessageResult, instanceOf(ErrorMessage.class));
    assertEquals(errorMessage.getOriginalMessage(), ((ErrorMessage) errorMessageResult).getOriginalMessage());
    assertEquals(errorMessage.getPayload().getMessage(), ((ErrorMessage) errorMessageResult).getPayload().getMessage());
    Message<Foo> fooMessage = new GenericMessage<>(new Foo("foo"));
    try {
        store.addMessageToGroup(this.groupId, fooMessage).getMessages().iterator().next();
        fail("SerializationException expected");
    } catch (Exception e) {
        assertThat(e.getCause().getCause(), instanceOf(IllegalArgumentException.class));
        assertThat(e.getMessage(), containsString("The class with " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo and name of " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo " + "is not in the trusted packages:"));
    }
    mapper = JacksonJsonUtils.messagingAwareMapper(getClass().getPackage().getName());
    serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    store.removeMessageGroup(this.groupId);
    messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
    assertEquals(1, messageGroup.size());
    assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
    mapper = JacksonJsonUtils.messagingAwareMapper("*");
    serializer = new GenericJackson2JsonRedisSerializer(mapper);
    store.setValueSerializer(serializer);
    store.removeMessageGroup(this.groupId);
    messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
    assertEquals(1, messageGroup.size());
    assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
}
Also used : GenericJackson2JsonRedisSerializer(org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) AdviceMessage(org.springframework.integration.message.AdviceMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) ArrayList(java.util.ArrayList) AdviceMessage(org.springframework.integration.message.AdviceMessage) Date(java.util.Date) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 38 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testWithMessageHistory.

@Test
@RedisAvailable
public void testWithMessageHistory() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    Message<?> message = new GenericMessage<>("Hello");
    DirectChannel fooChannel = new DirectChannel();
    fooChannel.setBeanName("fooChannel");
    DirectChannel barChannel = new DirectChannel();
    barChannel.setBeanName("barChannel");
    message = MessageHistory.write(message, fooChannel);
    message = MessageHistory.write(message, barChannel);
    store.addMessagesToGroup(this.groupId, message);
    message = store.getMessageGroup(this.groupId).getMessages().iterator().next();
    MessageHistory messageHistory = MessageHistory.read(message);
    assertNotNull(messageHistory);
    assertEquals(2, messageHistory.size());
    Properties fooChannelHistory = messageHistory.get(0);
    assertEquals("fooChannel", fooChannelHistory.get("name"));
    assertEquals("channel", fooChannelHistory.get("type"));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) DirectChannel(org.springframework.integration.channel.DirectChannel) Properties(java.util.Properties) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 39 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testAddAndRemoveMessagesFromMessageGroup.

@Test
@RedisAvailable
public void testAddAndRemoveMessagesFromMessageGroup() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore messageStore = new RedisMessageStore(jcf);
    List<Message<?>> messages = new ArrayList<Message<?>>();
    for (int i = 0; i < 25; i++) {
        Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId(this.groupId).build();
        messageStore.addMessagesToGroup(this.groupId, message);
        messages.add(message);
    }
    MessageGroup group = messageStore.getMessageGroup(this.groupId);
    assertEquals(25, group.size());
    messageStore.removeMessagesFromGroup(this.groupId, messages);
    group = messageStore.getMessageGroup(this.groupId);
    assertEquals(0, group.size());
    messageStore.removeMessageGroup(this.groupId);
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) AdviceMessage(org.springframework.integration.message.AdviceMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) ArrayList(java.util.ArrayList) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Matchers.containsString(org.hamcrest.Matchers.containsString) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 40 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testWithAggregatorWithShutdown.

@Test
@RedisAvailable
public void testWithAggregatorWithShutdown() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("redis-aggregator-config.xml", getClass());
    MessageChannel input = context.getBean("inputChannel", MessageChannel.class);
    QueueChannel output = context.getBean("outputChannel", QueueChannel.class);
    Message<?> m1 = MessageBuilder.withPayload("1").setSequenceNumber(1).setSequenceSize(3).setCorrelationId(this.groupId).build();
    Message<?> m2 = MessageBuilder.withPayload("2").setSequenceNumber(2).setSequenceSize(3).setCorrelationId(this.groupId).build();
    input.send(m1);
    assertNull(output.receive(10));
    input.send(m2);
    assertNull(output.receive(10));
    context.close();
    context = new ClassPathXmlApplicationContext("redis-aggregator-config.xml", getClass());
    input = context.getBean("inputChannel", MessageChannel.class);
    output = context.getBean("outputChannel", QueueChannel.class);
    Message<?> m3 = MessageBuilder.withPayload("3").setSequenceNumber(3).setSequenceSize(3).setCorrelationId(this.groupId).build();
    input.send(m3);
    assertNotNull(output.receive(10000));
    context.close();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)99 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)99 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)62 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)28 GenericMessage (org.springframework.messaging.support.GenericMessage)24 BeanFactory (org.springframework.beans.factory.BeanFactory)23 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)16 MessageGroup (org.springframework.integration.store.MessageGroup)15 ArrayList (java.util.ArrayList)14 Lock (java.util.concurrent.locks.Lock)13 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)13 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)13 Message (org.springframework.messaging.Message)13 QueueChannel (org.springframework.integration.channel.QueueChannel)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)9 JdkSerializationRedisSerializer (org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)9 List (java.util.List)8 DefaultRedisList (org.springframework.data.redis.support.collections.DefaultRedisList)8 RedisList (org.springframework.data.redis.support.collections.RedisList)8