@Bean RedisTemplate<String, Object> redisTemplate() { final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(jedisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class)); template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class)); return template; }
@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(); }
@Bean(name = "redisTemplate") RedisOperations<String, Object> genericRedisTemplate(RedisConnectionFactory rcf) { RedisTemplate<String, Object> rt = new RedisTemplate<String, Object>(); rt.setConnectionFactory(rcf); rt.setKeySerializer(new StringRedisSerializer()); // Fixed to Vehicle type - no type information carried to redis rt.setValueSerializer(new Jackson2JsonRedisSerializer<>(Vehicle.class)); // Type is store in redis // rt.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return rt; }
@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"); } }
@RequestMapping(value = "/xdanalytics/orders", method = RequestMethod.GET) public @ResponseBody List<OrderAnalytics> orderAnalyticsFromXd() { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); OrderAnalytics oaAll = new OrderAnalytics( "richgauges.order_gauge", (String) redisTemplate.opsForValue().get("richgauges.order_gauge")); OrderAnalytics oaFraud = new OrderAnalytics( "richgauges.fraud_order_gauge", (String) redisTemplate.opsForValue().get("richgauges.fraud_order_gauge")); ArrayList<OrderAnalytics> oaList = new ArrayList<OrderAnalytics>(); oaList.add(oaAll); oaList.add(oaFraud); return oaList; }
@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(); }
@Override protected void onInit() throws Exception { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory()); Assert.state( !this.mapKeyExpressionExplicitlySet || (this.collectionType == CollectionType.MAP || this.collectionType == CollectionType.PROPERTIES), "'mapKeyExpression' can only be set for CollectionType.MAP or CollectionType.PROPERTIES"); if (!this.redisTemplateExplicitlySet) { if (!this.extractPayloadElements) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); StringRedisSerializer serializer = new StringRedisSerializer(); template.setKeySerializer(serializer); template.setHashKeySerializer(serializer); this.redisTemplate = template; } this.redisTemplate.setConnectionFactory(this.connectionFactory); this.redisTemplate.afterPropertiesSet(); } this.initialized = true; }
@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(); }