INT-4258: Redis: Make ZADD INCR False by Default

JIRA: https://jira.spring.io/browse/INT-4258

Since Redis `ZADD` command doesn't perform incrementation by default,
it should be consistent in the `RedisStoreWritingMessageHandler` as well

* Make a default behavior of the `RedisStoreWritingMessageHandler`
regarding `INCR` option as `false`
* Provide some performance refactoring for the
`RedisStoreWritingMessageHandler`  moving the code to Java 8 style
and using `FunctionExpression`
* Fix tests according to the changed default behavior

* Add `setZsetIncrementExpression` and String-based setters for other
expressions on the `RedisStoreWritingMessageHandler`
* Add `zset-increment-expression` option for XML configuration
* Add `What's New` note on the matter

Polishing
This commit is contained in:
Artem Bilan
2017-05-03 16:26:15 -04:00
committed by Gary Russell
parent 2dfb0808b3
commit 47433754b8
8 changed files with 151 additions and 88 deletions

View File

@@ -22,6 +22,7 @@
<int-redis:store-outbound-channel-adapter id="withStringTemplate"
collection-type="${collection.type}"
key="pepboys"
zset-increment-expression="true"
auto-startup="false">
<int-redis:request-handler-advice-chain>
<int:retry-advice/>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2007-2016 the original author or authors.
* Copyright 2007-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.expression.Expression;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.handler.advice.RequestHandlerRetryAdvice;
import org.springframework.integration.redis.outbound.RedisStoreWritingMessageHandler;
@@ -41,6 +42,7 @@ import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* @author Oleg Zhurakousky
@@ -61,7 +63,7 @@ public class RedisStoreOutboundChannelAdapterParserTests {
@Test
public void validateWithStringTemplate() throws Exception {
RedisStoreWritingMessageHandler withStringTemplate = context.getBean("withStringTemplate.handler",
RedisStoreWritingMessageHandler.class);
RedisStoreWritingMessageHandler.class);
assertEquals("pepboys", ((LiteralExpression) TestUtils.getPropertyValue(withStringTemplate,
"keyExpression")).getExpressionString());
assertEquals("PROPERTIES", (TestUtils.getPropertyValue(withStringTemplate, "collectionType")).toString());
@@ -75,6 +77,9 @@ public class RedisStoreOutboundChannelAdapterParserTests {
assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors.first.item.advice"),
Matchers.instanceOf(RequestHandlerRetryAdvice.class));
assertEquals("true", TestUtils.getPropertyValue(withStringTemplate, "zsetIncrementScoreExpression",
Expression.class).getExpressionString());
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2007-2016 the original author or authors.
* Copyright 2007-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -66,6 +66,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.2
*/
@ContextConfiguration
@@ -190,7 +191,10 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
RedisZSet<String> redisZSet = new DefaultRedisZSet<String>("foo", this.redisTemplate);
assertEquals(0, redisZSet.size());
Message<String> message = MessageBuilder.withPayload("bar").setHeader(RedisHeaders.KEY, "foo").build();
Message<String> message = MessageBuilder.withPayload("bar")
.setHeader(RedisHeaders.KEY, "foo")
.setHeader(RedisHeaders.ZSET_INCREMENT_SCORE, true)
.build();
this.zsetChannel.send(message);
assertEquals(1, redisZSet.size());
@@ -210,7 +214,6 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
Message<String> message = MessageBuilder.withPayload("bar")
.setHeader(RedisHeaders.KEY, "foo")
.setHeader(RedisHeaders.ZSET_INCREMENT_SCORE, false)
.build();
this.zsetChannel.send(message);
@@ -232,6 +235,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
Message<String> message = MessageBuilder.withPayload("bar")
.setHeader(RedisHeaders.KEY, "foo")
.setHeader(RedisHeaders.ZSET_SCORE, 2)
.setHeader(RedisHeaders.ZSET_INCREMENT_SCORE, true)
.build();
this.zsetChannel.send(message);
@@ -280,7 +284,9 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
presidents.put("John Quincy Adams", 19);
presidents.put("Zachary Taylor", 19);
Message<Map<String, Integer>> message = MessageBuilder.withPayload(presidents).build();
Message<Map<String, Integer>> message = MessageBuilder.withPayload(presidents)
.setHeader(RedisHeaders.ZSET_INCREMENT_SCORE, true)
.build();
this.mapToZsetChannel.send(message);
@@ -293,7 +299,6 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
this.beanFactory.getBean("mapToZset.handler", RedisStoreWritingMessageHandler.class);
assertEquals("'presidents'", TestUtils.getPropertyValue(handler, "keyExpression.expression"));
// test default (increment by score) behavior
this.mapToZsetChannel.send(message);
assertEquals(5, redisZset.size());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,6 +55,7 @@ import org.springframework.messaging.support.GenericMessage;
* @author Gunnar Hillert
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
*/
public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests {
@@ -201,7 +202,9 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests {
list.add("Manny");
list.add("Moe");
list.add("Jack");
Message<List<String>> message = new GenericMessage<List<String>>(list);
Message<List<String>> message = MessageBuilder.withPayload(list)
.setHeader(RedisHeaders.ZSET_INCREMENT_SCORE, true)
.build();
handler.handleMessage(message);
assertEquals(3, redisZset.size());