diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandler.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandler.java index 7d63aa7ca0..c93f8eaefa 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandler.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandler.java @@ -72,6 +72,9 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand private final Log logger = LogFactory.getLog(this.getClass()); + private final Expression zsetIncrementScoreExpression = + new SpelExpressionParser().parseExpression("headers." + RedisHeaders.ZSET_INCREMENT_SCORE); + private volatile StandardEvaluationContext evaluationContext; private volatile Expression keyExpression = @@ -322,7 +325,7 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand final Object payload = message.getPayload(); final BoundZSetOperations ops = (BoundZSetOperations) this.redisTemplate.boundZSetOps(zset.getKey()); - final Object zsetIncrementHeader = message.getHeaders().get(RedisHeaders.ZSET_INCREMENT_SCORE); + final boolean zsetIncrementHeader = this.extractZsetIncrementHeader(message); if (this.extractPayloadElements) { @@ -358,6 +361,13 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand } } + private boolean extractZsetIncrementHeader(Message message){ + if (message.getHeaders().containsKey(RedisHeaders.ZSET_INCREMENT_SCORE)){ + return this.zsetIncrementScoreExpression.getValue(this.evaluationContext, message, Boolean.class); + } + return false; + } + @SuppressWarnings("unchecked") private void handleList(RedisList list, Message message) { Object payload = message.getPayload(); @@ -454,15 +464,15 @@ public class RedisCollectionPopulatingMessageHandler extends AbstractMessageHand } private void incrementOrOverwrite(final BoundZSetOperations ops, Object object, Double score, - Object zsetIncrementScore) { + boolean zsetIncrementScore) { - boolean increment = Boolean.TRUE.equals(zsetIncrementScore);; + //boolean increment = Boolean.TRUE.equals(zsetIncrementScore);; if (score != null) { - this.doIncrementOrOverwrite(ops, object, score, increment); + this.doIncrementOrOverwrite(ops, object, score, zsetIncrementScore); } else { logger.debug("Zset Score could not be determined. Using default score of 1"); - this.doIncrementOrOverwrite(ops, object, Double.valueOf(1), increment); + this.doIncrementOrOverwrite(ops, object, Double.valueOf(1), zsetIncrementScore); } } diff --git a/spring-integration-redis/src/main/resources/org/springframework/integration/redis/config/spring-integration-redis-2.2.xsd b/spring-integration-redis/src/main/resources/org/springframework/integration/redis/config/spring-integration-redis-2.2.xsd index fe33d3f5ce..804a3012ad 100644 --- a/spring-integration-redis/src/main/resources/org/springframework/integration/redis/config/spring-integration-redis-2.2.xsd +++ b/spring-integration-redis/src/main/resources/org/springframework/integration/redis/config/spring-integration-redis-2.2.xsd @@ -219,15 +219,6 @@ - - - - - - - - - + + + + Channel to which Messages will be sent. + + + + + + + + @@ -388,18 +391,6 @@ - - - - Channel to which Messages will be sent. - - - - - - - - diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter.xml b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter.xml index d9d320f34a..e3aacb9fe9 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter.xml +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/config/store-outbound-adapter.xml @@ -11,6 +11,7 @@ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> + @@ -18,6 +19,11 @@ collection-type="LIST" key="pepboys"/> + + diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandlerTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandlerTests.java index e90a3658a4..6ce260c205 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandlerTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisCollectionPopulatingMessageHandlerTests.java @@ -232,6 +232,49 @@ public class RedisCollectionPopulatingMessageHandlerTests extends RedisAvailable } } + @SuppressWarnings("unchecked") + @Test + @RedisAvailable + public void testZsetWithListPayloadParsedAndProvidedKeyScoreIncrementAsStringHeader() {// see INT-2775 + JedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + String key = "foo"; + RedisZSet redisZset = + new DefaultRedisZSet(key, + (RedisOperations) this.initTemplate(jcf, new RedisTemplate())); + + assertEquals(0, redisZset.size()); + + RedisCollectionPopulatingMessageHandler handler = + new RedisCollectionPopulatingMessageHandler(jcf, new LiteralExpression(key)); + + handler.setCollectionType(CollectionType.ZSET); + handler.afterPropertiesSet(); + + List list = new ArrayList(); + list.add("Manny"); + list.add("Moe"); + list.add("Jack"); + Message> message = MessageBuilder.withPayload(list) + .setHeader(RedisHeaders.ZSET_INCREMENT_SCORE, "true") + .build(); + + handler.handleMessage(message); + + assertEquals(3, redisZset.size()); + Set> pepboys = redisZset.rangeByScoreWithScores(1, 1); + for (TypedTuple pepboy : pepboys) { + assertTrue(pepboy.getScore() == 1); + } + + handler.handleMessage(message); + assertEquals(3, redisZset.size()); + pepboys = redisZset.rangeByScoreWithScores(1, 2); + // should have incremented + for (TypedTuple pepboy : pepboys) { + assertTrue(pepboy.getScore() == 2); + } + } + @SuppressWarnings("unchecked") @Test @RedisAvailable