Search in sources :

Example 26 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project spring-integration by spring-projects.

the class RedisPublishingMessageHandlerTests method testRedisPublishingMessageHandler.

@Test
@RedisAvailable
public void testRedisPublishingMessageHandler() throws Exception {
    int numToTest = 10;
    String topic = "si.test.channel";
    final CountDownLatch latch = new CountDownLatch(numToTest * 2);
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    MessageListenerAdapter listener = new MessageListenerAdapter();
    listener.setDelegate(new Listener(latch));
    listener.setSerializer(new StringRedisSerializer());
    listener.afterPropertiesSet();
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.afterPropertiesSet();
    container.addMessageListener(listener, Collections.<Topic>singletonList(new ChannelTopic(topic)));
    container.start();
    this.awaitContainerSubscribed(container);
    final RedisPublishingMessageHandler handler = new RedisPublishingMessageHandler(connectionFactory);
    handler.setTopicExpression(new LiteralExpression(topic));
    for (int i = 0; i < numToTest; i++) {
        handler.handleMessage(MessageBuilder.withPayload("test-" + i).build());
    }
    for (int i = 0; i < numToTest; i++) {
        handler.handleMessage(MessageBuilder.withPayload(("test-" + i).getBytes()).build());
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    container.stop();
}
Also used : MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) LiteralExpression(org.springframework.expression.common.LiteralExpression) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) CountDownLatch(java.util.concurrent.CountDownLatch) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 27 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project spring-integration by spring-projects.

the class RedisStoreOutboundChannelAdapterIntegrationTests method testMapToMapAsSingleEntryWithKeyAsHeader.

@Test
@RedisAvailable
public void testMapToMapAsSingleEntryWithKeyAsHeader() {
    RedisTemplate<String, Map<String, Map<String, String>>> redisTemplate = new RedisTemplate<String, Map<String, Map<String, String>>>();
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setConnectionFactory(getConnectionFactoryForTest());
    redisTemplate.afterPropertiesSet();
    RedisMap<String, Map<String, String>> redisMap = new DefaultRedisMap<String, Map<String, String>>("pepboys", redisTemplate);
    assertEquals(0, redisMap.size());
    Map<String, String> pepboys = new HashMap<String, String>();
    pepboys.put("1", "Manny");
    pepboys.put("2", "Moe");
    pepboys.put("3", "Jack");
    Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).setHeader(RedisHeaders.KEY, "pepboys").setHeader(RedisHeaders.MAP_KEY, "foo").build();
    this.mapToMapBChannel.send(message);
    Map<String, String> pepboyz = redisMap.get("foo");
    assertEquals("Manny", pepboyz.get("1"));
    assertEquals("Moe", pepboyz.get("2"));
    assertEquals("Jack", pepboyz.get("3"));
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) HashMap(java.util.HashMap) DefaultRedisMap(org.springframework.data.redis.support.collections.DefaultRedisMap) HashMap(java.util.HashMap) Map(java.util.Map) RedisMap(org.springframework.data.redis.support.collections.RedisMap) DefaultRedisMap(org.springframework.data.redis.support.collections.DefaultRedisMap) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 28 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project spring-integration by spring-projects.

the class RedisQueueMessageDrivenEndpointTests method testInt3442ProperlyStop.

@Test
@RedisAvailable
@SuppressWarnings("unchecked")
public void testInt3442ProperlyStop() throws Exception {
    final String queueName = "si.test.testInt3442ProperlyStopTest";
    final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(this.connectionFactory);
    redisTemplate.setEnableDefaultSerializer(false);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.afterPropertiesSet();
    while (redisTemplate.boundListOps(queueName).rightPop() != null) {
    // drain
    }
    RedisQueueMessageDrivenEndpoint endpoint = new RedisQueueMessageDrivenEndpoint(queueName, this.connectionFactory);
    BoundListOperations<String, byte[]> boundListOperations = TestUtils.getPropertyValue(endpoint, "boundListOperations", BoundListOperations.class);
    boundListOperations = Mockito.spy(boundListOperations);
    DirectFieldAccessor dfa = new DirectFieldAccessor(endpoint);
    dfa.setPropertyValue("boundListOperations", boundListOperations);
    endpoint.setBeanFactory(Mockito.mock(BeanFactory.class));
    endpoint.setOutputChannel(new DirectChannel());
    endpoint.setReceiveTimeout(10);
    ExecutorService executorService = Executors.newCachedThreadPool();
    endpoint.setTaskExecutor(executorService);
    endpoint.afterPropertiesSet();
    endpoint.start();
    waitListening(endpoint);
    dfa.setPropertyValue("listening", false);
    redisTemplate.boundListOps(queueName).leftPush("foo");
    final CountDownLatch stopLatch = new CountDownLatch(1);
    endpoint.stop(() -> stopLatch.countDown());
    executorService.shutdown();
    assertTrue(executorService.awaitTermination(20, TimeUnit.SECONDS));
    assertTrue(stopLatch.await(21, TimeUnit.SECONDS));
    verify(boundListOperations, atLeastOnce()).rightPush(any(byte[].class));
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) DirectChannel(org.springframework.integration.channel.DirectChannel) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) CountDownLatch(java.util.concurrent.CountDownLatch) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 29 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project spring-integration by spring-projects.

the class RedisQueueMessageDrivenEndpointTests method testInt3014Default.

@Test
@RedisAvailable
@SuppressWarnings("unchecked")
public void testInt3014Default() {
    String queueName = "si.test.redisQueueInboundChannelAdapterTests";
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(this.connectionFactory);
    redisTemplate.setEnableDefaultSerializer(false);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.afterPropertiesSet();
    String payload = "testing";
    redisTemplate.boundListOps(queueName).leftPush(payload);
    Date payload2 = new Date();
    redisTemplate.boundListOps(queueName).leftPush(payload2);
    PollableChannel channel = new QueueChannel();
    RedisQueueMessageDrivenEndpoint endpoint = new RedisQueueMessageDrivenEndpoint(queueName, this.connectionFactory);
    endpoint.setBeanFactory(Mockito.mock(BeanFactory.class));
    endpoint.setOutputChannel(channel);
    endpoint.setReceiveTimeout(10);
    endpoint.afterPropertiesSet();
    endpoint.start();
    Message<Object> receive = (Message<Object>) channel.receive(10000);
    assertNotNull(receive);
    assertEquals(payload, receive.getPayload());
    receive = (Message<Object>) channel.receive(10000);
    assertNotNull(receive);
    assertEquals(payload2, receive.getPayload());
    endpoint.stop();
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) Date(java.util.Date) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 30 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project spring-integration by spring-projects.

the class RedisQueueMessageDrivenEndpointTests method testInt3014ExpectMessageTrue.

@Test
@RedisAvailable
@SuppressWarnings("unchecked")
public void testInt3014ExpectMessageTrue() {
    final String queueName = "si.test.redisQueueInboundChannelAdapterTests2";
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(this.connectionFactory);
    redisTemplate.setEnableDefaultSerializer(false);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.afterPropertiesSet();
    Message<?> message = MessageBuilder.withPayload("testing").build();
    redisTemplate.boundListOps(queueName).leftPush(message);
    redisTemplate.boundListOps(queueName).leftPush("test");
    PollableChannel channel = new QueueChannel();
    PollableChannel errorChannel = new QueueChannel();
    RedisQueueMessageDrivenEndpoint endpoint = new RedisQueueMessageDrivenEndpoint(queueName, this.connectionFactory);
    endpoint.setBeanFactory(Mockito.mock(BeanFactory.class));
    endpoint.setExpectMessage(true);
    endpoint.setOutputChannel(channel);
    endpoint.setErrorChannel(errorChannel);
    endpoint.setReceiveTimeout(10);
    endpoint.afterPropertiesSet();
    endpoint.start();
    Message<Object> receive = (Message<Object>) channel.receive(10000);
    assertNotNull(receive);
    assertEquals(message, receive);
    receive = (Message<Object>) errorChannel.receive(10000);
    assertNotNull(receive);
    assertThat(receive, Matchers.instanceOf(ErrorMessage.class));
    assertThat(receive.getPayload(), Matchers.instanceOf(MessagingException.class));
    assertThat(((Exception) receive.getPayload()).getMessage(), Matchers.containsString("Deserialization of Message failed."));
    assertThat(((Exception) receive.getPayload()).getCause(), Matchers.instanceOf(ClassCastException.class));
    assertThat(((Exception) receive.getPayload()).getCause().getMessage(), Matchers.containsString("java.lang.String cannot be cast to org.springframework.messaging.Message"));
    endpoint.stop();
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) MessagingException(org.springframework.messaging.MessagingException) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) RedisSystemException(org.springframework.data.redis.RedisSystemException) RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) MessagingException(org.springframework.messaging.MessagingException) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) ErrorMessage(org.springframework.messaging.support.ErrorMessage) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)46 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)30 Bean (org.springframework.context.annotation.Bean)17 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)16 Test (org.junit.Test)14 JdkSerializationRedisSerializer (org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)14 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)13 DataAccessException (org.springframework.dao.DataAccessException)7 RedisConnection (org.springframework.data.redis.connection.RedisConnection)7 RedisCallback (org.springframework.data.redis.core.RedisCallback)7 BeanFactory (org.springframework.beans.factory.BeanFactory)5 Date (java.util.Date)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Jackson2JsonRedisSerializer (org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer)3 Message (org.springframework.messaging.Message)3 PollableChannel (org.springframework.messaging.PollableChannel)3 ErrorMessage (org.springframework.messaging.support.ErrorMessage)3 HashMap (java.util.HashMap)2