@Test
  @RedisAvailable
  @SuppressWarnings("unchecked")
  public void testInt3014ExpectMessageTrue() throws Exception {

    final String queueName = "si.test.redisQueueInboundChannelAdapterTests2";

    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    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(1000);
    endpoint.afterPropertiesSet();
    endpoint.start();

    Message<Object> receive = (Message<Object>) channel.receive(2000);
    assertNotNull(receive);

    assertEquals(message, receive);

    receive = (Message<Object>) errorChannel.receive(2000);
    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();
  }
 @SuppressWarnings("rawtypes")
 private static RedisTemplate createDefaultTemplate(RedisConnectionFactory connectionFactory) {
   Assert.notNull(connectionFactory, "connectionFactory cannot be null");
   RedisTemplate<String, ExpiringSession> template = new RedisTemplate<String, ExpiringSession>();
   template.setKeySerializer(new StringRedisSerializer());
   template.setHashKeySerializer(new StringRedisSerializer());
   template.setConnectionFactory(connectionFactory);
   template.afterPropertiesSet();
   return template;
 }
 /**
  * @param queueName Must not be an empty String
  * @param connectionFactory Must not be null
  */
 public RedisQueueMessageDrivenEndpoint(
     String queueName, RedisConnectionFactory connectionFactory) {
   Assert.hasText(queueName, "'queueName' is required");
   Assert.notNull(connectionFactory, "'connectionFactory' must not be null");
   RedisTemplate<String, byte[]> template = new RedisTemplate<String, byte[]>();
   template.setConnectionFactory(connectionFactory);
   template.setEnableDefaultSerializer(false);
   template.setKeySerializer(new StringRedisSerializer());
   template.afterPropertiesSet();
   this.boundListOperations = template.boundListOps(queueName);
 }
 /** @param redisConnectionFactory */
 public RedisAggregateCounterRepository(RedisConnectionFactory redisConnectionFactory) {
   super("aggregatecounters", redisConnectionFactory);
   RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
   redisTemplate.setConnectionFactory(redisConnectionFactory);
   redisTemplate.setKeySerializer(new StringRedisSerializer());
   redisTemplate.setValueSerializer(new StringRedisSerializer());
   redisTemplate.setHashKeySerializer(new StringRedisSerializer());
   redisTemplate.setHashValueSerializer(new GenericToStringSerializer<Long>(Long.class));
   redisTemplate.afterPropertiesSet();
   hashOperations = redisTemplate.opsForHash();
   setOperations = redisTemplate.opsForSet();
 }
  @Test
  @RedisAvailable
  public void testFileSystemWithRedisMetadataStore() throws Exception {
    RedisTemplate<String, ?> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(this.getConnectionFactoryForTest());
    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    template.delete("persistentAcceptOnceFileListFilterRedisTests");

    try {
      this.testFileSystem(
          new RedisMetadataStore(
              this.getConnectionFactoryForTest(), "persistentAcceptOnceFileListFilterRedisTests"));
    } finally {
      template.delete("persistentAcceptOnceFileListFilterRedisTests");
    }
  }
  @Test
  @RedisAvailable
  public void testInt3017IntegrationInbound() throws Exception {

    String payload = new Date().toString();

    RedisTemplate<String, String> redisTemplate = new StringRedisTemplate();
    redisTemplate.setConnectionFactory(this.connectionFactory);
    redisTemplate.afterPropertiesSet();

    redisTemplate
        .boundListOps("si.test.Int3017IntegrationInbound")
        .leftPush("{\"payload\":\"" + payload + "\",\"headers\":{}}");

    Message<?> receive = this.fromChannel.receive(2000);
    assertNotNull(receive);
    assertEquals(payload, receive.getPayload());
  }
  @Test
  @RedisAvailable
  @SuppressWarnings("unchecked")
  public void testInt3014Default() throws Exception {

    String queueName = "si.test.redisQueueInboundChannelAdapterTests";

    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    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(1000);
    endpoint.afterPropertiesSet();
    endpoint.start();

    Message<Object> receive = (Message<Object>) channel.receive(2000);
    assertNotNull(receive);
    assertEquals(payload, receive.getPayload());

    receive = (Message<Object>) channel.receive(2000);
    assertNotNull(receive);
    assertEquals(payload2, receive.getPayload());

    endpoint.stop();
  }
  @Test
  @RedisAvailable
  @SuppressWarnings("unchecked")
  public void testInt3442ProperlyStop() throws Exception {
    final String queueName = "si.test.testInt3442ProperlyStopTest";

    final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(this.connectionFactory);
    redisTemplate.setEnableDefaultSerializer(false);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.afterPropertiesSet();

    RedisQueueMessageDrivenEndpoint endpoint =
        new RedisQueueMessageDrivenEndpoint(queueName, this.connectionFactory);
    BoundListOperations<String, byte[]> boundListOperations =
        TestUtils.getPropertyValue(endpoint, "boundListOperations", BoundListOperations.class);
    boundListOperations = Mockito.spy(boundListOperations);
    new DirectFieldAccessor(endpoint).setPropertyValue("boundListOperations", boundListOperations);
    endpoint.setBeanFactory(Mockito.mock(BeanFactory.class));
    endpoint.setOutputChannel(new DirectChannel());
    endpoint.setReceiveTimeout(1000);
    endpoint.setStopTimeout(100);

    ExecutorService executorService = Executors.newCachedThreadPool();
    endpoint.setTaskExecutor(executorService);

    endpoint.afterPropertiesSet();
    endpoint.start();

    redisTemplate.boundListOps(queueName).leftPush("foo");
    endpoint.stop();

    executorService.shutdown();
    assertTrue(executorService.awaitTermination(1, TimeUnit.SECONDS));

    Mockito.verify(boundListOperations).rightPush(Mockito.any(byte[].class));
  }
  @Test
  @RedisAvailable
  @SuppressWarnings("unchecked")
  @Ignore
  // JedisConnectionFactory doesn't support proper 'destroy()' and allows to create new fresh Redis
  // connection
  public void testInt3196Recovery() throws Exception {
    String queueName = "test.si.Int3196Recovery";
    QueueChannel channel = new QueueChannel();

    final List<ApplicationEvent> exceptionEvents = new ArrayList<ApplicationEvent>();

    final CountDownLatch exceptionsLatch = new CountDownLatch(2);

    RedisQueueMessageDrivenEndpoint endpoint =
        new RedisQueueMessageDrivenEndpoint(queueName, this.connectionFactory);
    endpoint.setBeanFactory(Mockito.mock(BeanFactory.class));
    endpoint.setApplicationEventPublisher(
        new ApplicationEventPublisher() {

          @Override
          public void publishEvent(ApplicationEvent event) {
            exceptionEvents.add(event);
            exceptionsLatch.countDown();
          }
        });
    endpoint.setOutputChannel(channel);
    endpoint.setReceiveTimeout(100);
    endpoint.setRecoveryInterval(200);
    endpoint.afterPropertiesSet();
    endpoint.start();

    int n = 0;
    do {
      n++;
      if (n == 100) {
        break;
      }
      Thread.sleep(100);
    } while (!endpoint.isListening());

    assertTrue(n < 100);

    ((DisposableBean) this.connectionFactory).destroy();

    assertTrue(exceptionsLatch.await(10, TimeUnit.SECONDS));

    for (ApplicationEvent exceptionEvent : exceptionEvents) {
      assertThat(exceptionEvent, Matchers.instanceOf(RedisExceptionEvent.class));
      assertSame(endpoint, exceptionEvent.getSource());
      assertThat(
          ((IntegrationEvent) exceptionEvent).getCause().getClass(),
          Matchers.isIn(
              Arrays.<Class<? extends Throwable>>asList(
                  RedisSystemException.class, RedisConnectionFailureException.class)));
    }

    ((InitializingBean) this.connectionFactory).afterPropertiesSet();

    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(this.getConnectionFactoryForTest());
    redisTemplate.setEnableDefaultSerializer(false);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.afterPropertiesSet();

    String payload = "testing";

    redisTemplate.boundListOps(queueName).leftPush(payload);

    Message<?> receive = channel.receive(1000);
    assertNotNull(receive);
    assertEquals(payload, receive.getPayload());

    endpoint.stop();
  }
  @Parameters
  public static Collection<Object[]> testParams() {
    // XStream serializer
    XStreamMarshaller xstream = new XStreamMarshaller();
    try {
      xstream.afterPropertiesSet();
    } catch (Exception ex) {
      throw new RuntimeException("Cannot init XStream", ex);
    }
    OxmSerializer serializer = new OxmSerializer(xstream, xstream);
    JacksonJsonRedisSerializer<Person> jsonSerializer =
        new JacksonJsonRedisSerializer<Person>(Person.class);
    JacksonJsonRedisSerializer<String> jsonStringSerializer =
        new JacksonJsonRedisSerializer<String>(String.class);

    // create Jedis Factory
    ObjectFactory<String> stringFactory = new StringObjectFactory();

    JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
    jedisConnFactory.setUsePool(true);

    jedisConnFactory.setPort(SettingsUtils.getPort());
    jedisConnFactory.setHostName(SettingsUtils.getHost());

    jedisConnFactory.afterPropertiesSet();

    RedisTemplate<String, String> genericTemplate = new StringRedisTemplate(jedisConnFactory);

    RedisTemplate<String, String> xstreamGenericTemplate = new RedisTemplate<String, String>();
    xstreamGenericTemplate.setConnectionFactory(jedisConnFactory);
    xstreamGenericTemplate.setDefaultSerializer(serializer);
    xstreamGenericTemplate.afterPropertiesSet();

    RedisTemplate<String, Person> jsonPersonTemplate = new RedisTemplate<String, Person>();
    jsonPersonTemplate.setConnectionFactory(jedisConnFactory);
    jsonPersonTemplate.setDefaultSerializer(jsonSerializer);
    jsonPersonTemplate.setHashKeySerializer(jsonSerializer);
    jsonPersonTemplate.setHashValueSerializer(jsonStringSerializer);
    jsonPersonTemplate.afterPropertiesSet();

    // JRedis
    JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
    jredisConnFactory.setUsePool(true);
    jredisConnFactory.setPort(SettingsUtils.getPort());
    jredisConnFactory.setHostName(SettingsUtils.getHost());
    jredisConnFactory.afterPropertiesSet();

    RedisTemplate<String, String> genericTemplateJR = new StringRedisTemplate(jredisConnFactory);
    RedisTemplate<String, Person> xGenericTemplateJR = new RedisTemplate<String, Person>();
    xGenericTemplateJR.setConnectionFactory(jredisConnFactory);
    xGenericTemplateJR.setDefaultSerializer(serializer);
    xGenericTemplateJR.afterPropertiesSet();

    RedisTemplate<String, Person> jsonPersonTemplateJR = new RedisTemplate<String, Person>();
    jsonPersonTemplateJR.setConnectionFactory(jredisConnFactory);
    jsonPersonTemplateJR.setDefaultSerializer(jsonSerializer);
    jsonPersonTemplateJR.setHashKeySerializer(jsonSerializer);
    jsonPersonTemplateJR.setHashValueSerializer(jsonStringSerializer);
    jsonPersonTemplateJR.afterPropertiesSet();

    // RJC

    // rjc
    RjcConnectionFactory rjcConnFactory = new RjcConnectionFactory();
    rjcConnFactory.setUsePool(true);
    rjcConnFactory.setPort(SettingsUtils.getPort());
    rjcConnFactory.setHostName(SettingsUtils.getHost());
    rjcConnFactory.afterPropertiesSet();

    RedisTemplate<String, String> genericTemplateRJC = new StringRedisTemplate(jredisConnFactory);
    RedisTemplate<String, Person> xGenericTemplateRJC = new RedisTemplate<String, Person>();
    xGenericTemplateRJC.setConnectionFactory(rjcConnFactory);
    xGenericTemplateRJC.setDefaultSerializer(serializer);
    xGenericTemplateRJC.afterPropertiesSet();

    RedisTemplate<String, Person> jsonPersonTemplateRJC = new RedisTemplate<String, Person>();
    jsonPersonTemplateRJC.setConnectionFactory(rjcConnFactory);
    jsonPersonTemplateRJC.setDefaultSerializer(jsonSerializer);
    jsonPersonTemplateRJC.setHashKeySerializer(jsonSerializer);
    jsonPersonTemplateRJC.setHashValueSerializer(jsonStringSerializer);
    jsonPersonTemplateRJC.afterPropertiesSet();

    // Lettuce
    LettuceConnectionFactory lettuceConnFactory = new LettuceConnectionFactory();
    lettuceConnFactory.setPort(SettingsUtils.getPort());
    lettuceConnFactory.setHostName(SettingsUtils.getHost());
    lettuceConnFactory.afterPropertiesSet();

    RedisTemplate<String, String> genericTemplateLtc = new StringRedisTemplate(lettuceConnFactory);
    RedisTemplate<String, Person> xGenericTemplateLtc = new RedisTemplate<String, Person>();
    xGenericTemplateLtc.setConnectionFactory(lettuceConnFactory);
    xGenericTemplateLtc.setDefaultSerializer(serializer);
    xGenericTemplateLtc.afterPropertiesSet();

    RedisTemplate<String, Person> jsonPersonTemplateLtc = new RedisTemplate<String, Person>();
    jsonPersonTemplateLtc.setConnectionFactory(lettuceConnFactory);
    jsonPersonTemplateLtc.setDefaultSerializer(jsonSerializer);
    jsonPersonTemplateLtc.setHashKeySerializer(jsonSerializer);
    jsonPersonTemplateLtc.setHashValueSerializer(jsonStringSerializer);
    jsonPersonTemplateLtc.afterPropertiesSet();

    return Arrays.asList(
        new Object[][] {
          {stringFactory, stringFactory, genericTemplate},
          {stringFactory, stringFactory, genericTemplate},
          {stringFactory, stringFactory, genericTemplate},
          {stringFactory, stringFactory, genericTemplate},
          {stringFactory, stringFactory, xstreamGenericTemplate},
          {stringFactory, stringFactory, genericTemplateJR},
          {stringFactory, stringFactory, genericTemplateJR},
          {stringFactory, stringFactory, genericTemplateJR},
          {stringFactory, stringFactory, genericTemplateJR},
          {stringFactory, stringFactory, xGenericTemplateJR},
          {stringFactory, stringFactory, jsonPersonTemplate},
          {stringFactory, stringFactory, jsonPersonTemplateJR},
          {stringFactory, stringFactory, genericTemplateRJC},
          {stringFactory, stringFactory, genericTemplateRJC},
          {stringFactory, stringFactory, genericTemplateRJC},
          {stringFactory, stringFactory, genericTemplateRJC},
          {stringFactory, stringFactory, xGenericTemplateRJC},
          {stringFactory, stringFactory, jsonPersonTemplateRJC},
          {stringFactory, stringFactory, genericTemplateLtc},
          {stringFactory, stringFactory, genericTemplateLtc},
          {stringFactory, stringFactory, genericTemplateLtc},
          {stringFactory, stringFactory, genericTemplateLtc},
          {stringFactory, stringFactory, xGenericTemplateLtc},
          {stringFactory, stringFactory, jsonPersonTemplateLtc}
        });
  }