private void incrementOrOverwrite(BoundZSetOperations<String, Object> ops, Object object, Double score, boolean zsetIncrementScore) { if (score != null) { doIncrementOrOverwrite(ops, object, score, zsetIncrementScore); } else { this.logger.debug("Zset Score could not be determined. Using default score of 1"); doIncrementOrOverwrite(ops, object, 1d, zsetIncrementScore); } }
/** * Set the expression used as the INCR flag for the ZADD command in case of ZSet collection. * Default is 'headers.{@link RedisHeaders#ZSET_INCREMENT_SCORE}' * @param zsetIncrementScoreExpression The ZADD INCR flag expression. * @since 5.0 */ public void setZsetIncrementExpressionString(String zsetIncrementScoreExpression) { Assert.hasText(zsetIncrementScoreExpression, "'zsetIncrementScoreExpression' must not be empty"); setZsetIncrementExpression(EXPRESSION_PARSER.parseExpression(zsetIncrementScoreExpression)); }
/** * Sets the expression used as the key for Map and Properties entries. * Default is 'headers.{@link RedisHeaders#MAP_KEY}' * @param mapKeyExpression The map key expression. * @since 5.0 */ public void setMapKeyExpressionString(String mapKeyExpression) { Assert.hasText(mapKeyExpression, "'mapKeyExpression' must not be empty"); setMapKeyExpression(EXPRESSION_PARSER.parseExpression(mapKeyExpression)); }
@Test(expected = IllegalStateException.class) @RedisAvailable public void testZsetWithMapKeyExpression() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); String key = "foo"; RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf); handler.setKey(key); handler.setCollectionType(CollectionType.ZSET); handler.setMapKeyExpression(new LiteralExpression(key)); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); }
private void writeToMap(final RedisMap<Object, Object> map, Message<?> message) { final Object payload = message.getPayload(); if (this.extractPayloadElements && payload instanceof Map<?, ?>) { processInPipeline(() -> map.putAll((Map<?, ?>) payload)); } else { Object key = this.determineMapKey(message, false); map.put(key, payload); } }
public void setTopic(String topic) { Assert.hasText(topic, "'topic' must not be an empty string."); this.setTopicExpression(new LiteralExpression(topic)); }
/** * Specifies a SpEL Expression to be used to determine the key for the Redis store. * If an expression is not needed, then a literal value may be passed to the * {@link #setKey(String)} method instead of this one (they are mutually exclusive). * If neither setter is called, the default expression will be 'headers.{@link RedisHeaders#KEY}'. * @param keyExpression The key expression. * @see #setKey(String) * @since 5.0 */ public void setKeyExpressionString(String keyExpression) { Assert.hasText(keyExpression, "'keyExpression' must not be empty"); setKeyExpression(EXPRESSION_PARSER.parseExpression(keyExpression)); }
@Override protected void onInit() { Assert.notNull(this.topicExpression, "'topicExpression' must not be null."); if (this.messageConverter instanceof BeanFactoryAware) { ((BeanFactoryAware) this.messageConverter).setBeanFactory(getBeanFactory()); } if (this.evaluationContext == null) { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory()); } }
@Override protected void onInit() { super.onInit(); if (this.evaluationContext == null) { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory()); } }
@Override protected void doInit() { super.doInit(); if (this.evaluationContext == null) { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory()); } }
@Nullable private Object createReply(byte[] reply) { Object replyMessage = this.serializer.deserialize(reply); if (replyMessage == null) { return null; } if (this.extractPayload) { return getMessageBuilderFactory() .withPayload(replyMessage); } else { return replyMessage; } }
@SuppressWarnings("unchecked") private void writeToSet(final RedisSet<Object> set, Message<?> message) { final Object payload = message.getPayload(); if (this.extractPayloadElements && payload instanceof Collection<?>) { BoundSetOperations<String, Object> ops = (BoundSetOperations<String, Object>) this.redisTemplate.boundSetOps(set.getKey()); processInPipeline(() -> { for (Object object : ((Collection<?>) payload)) { ops.add(object); } }); } else { set.add(payload); } }
private void processInPipeline(PipelineCallback callback) { RedisConnectionFactory connectionFactoryForPipeline = this.redisTemplate.getConnectionFactory(); Assert.state(connectionFactoryForPipeline != null, "RedisTemplate returned no connection factory"); RedisConnection connection = RedisConnectionUtils.bindConnection(connectionFactoryForPipeline); try { connection.openPipeline(); callback.process(); } finally { connection.closePipeline(); RedisConnectionUtils.unbindConnection(connectionFactoryForPipeline); } }
@Before @After public void setup() { RedisConnectionFactory jcf = getConnectionFactoryForTest(); this.redisTemplate.setConnectionFactory(jcf); this.redisTemplate.afterPropertiesSet(); this.redisTemplate.delete("pepboys"); this.redisTemplate.delete("foo"); this.redisTemplate.delete("bar"); this.redisTemplate.delete("presidents"); }
@Test(expected = IllegalStateException.class) @RedisAvailable public void testSetWithMapKeyExpression() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); String key = "foo"; RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf); handler.setKey(key); handler.setCollectionType(CollectionType.SET); handler.setMapKeyExpression(new LiteralExpression(key)); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); }
private void writeToProperties(final RedisProperties properties, Message<?> message) { final Object payload = message.getPayload(); if (this.extractPayloadElements && payload instanceof Properties) { processInPipeline(() -> properties.putAll((Properties) payload)); } else { Assert.isInstanceOf(String.class, payload, "For property, payload must be a String."); Object key = this.determineMapKey(message, true); properties.put(key, payload); } }
/** * Specifies the key for the Redis store. If an expression is needed, then call * {@link #setKeyExpression(Expression)} instead of this method (they are mutually exclusive). * If neither setter is called, the default expression will be 'headers.{@link RedisHeaders#KEY}'. * @param key The key. * @see #setKeyExpression */ public void setKey(String key) { Assert.hasText(key, "key must not be empty"); this.setKeyExpression(new LiteralExpression(key)); }
@Test(expected = IllegalStateException.class) @RedisAvailable public void testListWithMapKeyExpression() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); String key = "foo"; RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf); handler.setKey(key); handler.setMapKeyExpression(new LiteralExpression(key)); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); }
@Test @RedisAvailable public void testMapWithMapKeyExpression() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); this.deleteKey(jcf, "foo"); String key = "foo"; RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf); handler.setKey(key); handler.setCollectionType(CollectionType.MAP); handler.setMapKeyExpression(new LiteralExpression(key)); handler.setBeanFactory(mock(BeanFactory.class)); try { handler.afterPropertiesSet(); } catch (Exception e) { fail("No exception expected:" + e.getMessage()); } this.deleteKey(jcf, "foo"); }
@Test @RedisAvailable public void testPropertiesWithMapKeyExpression() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); this.deleteKey(jcf, "foo"); String key = "foo"; RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf); handler.setKey(key); handler.setCollectionType(CollectionType.PROPERTIES); handler.setMapKeyExpression(new LiteralExpression(key)); handler.setBeanFactory(mock(BeanFactory.class)); try { handler.afterPropertiesSet(); } catch (Exception e) { fail("No exception expected:" + e.getMessage()); } this.deleteKey(jcf, "foo"); }