Search in sources :

Example 11 with JdkSerializationRedisSerializer

use of org.springframework.data.redis.serializer.JdkSerializationRedisSerializer in project dq-easy-cloud by dq-open-cloud.

the class DqRedisConfig method redisTemlateValueSerializer.

/**
 * <p>
 * 实例化 RedisTemplate 对象--保存的值为对象经过jdk序列化后的对象
 * </p>
 *
 * @return
 * @author daiqi
 * @date 2017年12月7日 下午5:19:51
 */
@Bean(value = DqRedisConstant.REDIS_TEMPLATE_VALUE_SERIALIZER_NAME)
public RedisTemplate<String, Object> redisTemlateValueSerializer() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    return redisTemplate;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) Bean(org.springframework.context.annotation.Bean)

Example 12 with JdkSerializationRedisSerializer

use of org.springframework.data.redis.serializer.JdkSerializationRedisSerializer 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 13 with JdkSerializationRedisSerializer

use of org.springframework.data.redis.serializer.JdkSerializationRedisSerializer 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 14 with JdkSerializationRedisSerializer

use of org.springframework.data.redis.serializer.JdkSerializationRedisSerializer 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)

Example 15 with JdkSerializationRedisSerializer

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

the class RedisQueueOutboundChannelAdapterTests method testInt3015Default.

@Test
@RedisAvailable
public void testInt3015Default() throws Exception {
    final String queueName = "si.test.testRedisQueueOutboundChannelAdapter";
    final RedisQueueOutboundChannelAdapter handler = new RedisQueueOutboundChannelAdapter(queueName, this.connectionFactory);
    String payload = "testing";
    handler.handleMessage(MessageBuilder.withPayload(payload).build());
    RedisTemplate<String, ?> redisTemplate = new StringRedisTemplate();
    redisTemplate.setConnectionFactory(this.connectionFactory);
    redisTemplate.afterPropertiesSet();
    Object result = redisTemplate.boundListOps(queueName).rightPop(5000, TimeUnit.MILLISECONDS);
    assertNotNull(result);
    assertEquals(payload, result);
    Date payload2 = new Date();
    handler.handleMessage(MessageBuilder.withPayload(payload2).build());
    RedisTemplate<String, ?> redisTemplate2 = new RedisTemplate<String, Object>();
    redisTemplate2.setConnectionFactory(this.connectionFactory);
    redisTemplate2.setEnableDefaultSerializer(false);
    redisTemplate2.setKeySerializer(new StringRedisSerializer());
    redisTemplate2.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate2.afterPropertiesSet();
    Object result2 = redisTemplate2.boundListOps(queueName).rightPop(5000, TimeUnit.MILLISECONDS);
    assertNotNull(result2);
    assertEquals(payload2, result2);
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) Date(java.util.Date) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

JdkSerializationRedisSerializer (org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)18 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)14 Test (org.junit.Test)12 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)11 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)11 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)9 BeanFactory (org.springframework.beans.factory.BeanFactory)5 Date (java.util.Date)4 Bean (org.springframework.context.annotation.Bean)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 Message (org.springframework.messaging.Message)3 PollableChannel (org.springframework.messaging.PollableChannel)3 ErrorMessage (org.springframework.messaging.support.ErrorMessage)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ReactiveRedisTemplate (org.springframework.data.redis.core.ReactiveRedisTemplate)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 lombok.val (lombok.val)1 Ignore (org.junit.Ignore)1