Repository: camel Updated Branches: refs/heads/master e84b46d75 -> 357653a6e
Fixed CS. fixes #619. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/357653a6 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/357653a6 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/357653a6 Branch: refs/heads/master Commit: 357653a6e246fe518723043b7819644b4a427932 Parents: cadb1bd Author: Claus Ibsen <[email protected]> Authored: Fri Sep 25 11:21:37 2015 +0200 Committer: Claus Ibsen <[email protected]> Committed: Fri Sep 25 11:21:49 2015 +0200 ---------------------------------------------------------------------- .../RedisStringIdempotentRepository.java | 145 ++++++++++--------- 1 file changed, 79 insertions(+), 66 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/357653a6/components/camel-spring-redis/src/main/java/org/apache/camel/component/redis/processor/idempotent/RedisStringIdempotentRepository.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-redis/src/main/java/org/apache/camel/component/redis/processor/idempotent/RedisStringIdempotentRepository.java b/components/camel-spring-redis/src/main/java/org/apache/camel/component/redis/processor/idempotent/RedisStringIdempotentRepository.java index 04e585a..43b6623 100644 --- a/components/camel-spring-redis/src/main/java/org/apache/camel/component/redis/processor/idempotent/RedisStringIdempotentRepository.java +++ b/components/camel-spring-redis/src/main/java/org/apache/camel/component/redis/processor/idempotent/RedisStringIdempotentRepository.java @@ -1,5 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.camel.component.redis.processor.idempotent; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.api.management.ManagedOperation; +import org.apache.camel.api.management.ManagedResource; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.Cursor; @@ -8,84 +30,75 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ScanOptions; import org.springframework.data.redis.core.ValueOperations; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - +@ManagedResource(description = "Spring Redis based message id repository") public class RedisStringIdempotentRepository extends RedisIdempotentRepository { - private final ValueOperations<String, String> valueOperations; + private final ValueOperations<String, String> valueOperations; - /* - The expiry time frame for the item in seconds - */ - private Long expiry; + private long expiry; - public RedisStringIdempotentRepository( - RedisTemplate<String, String> redisTemplate, - String processorName) { - super(redisTemplate, processorName); - this.valueOperations = redisTemplate.opsForValue(); - } + public RedisStringIdempotentRepository(RedisTemplate<String, String> redisTemplate, String processorName) { + super(redisTemplate, processorName); + this.valueOperations = redisTemplate.opsForValue(); + } - @Override - public boolean contains(String key) { - String value = valueOperations.get(createRedisKey(key)); - if (value != null) { - return true; - } else { - return false; + @ManagedOperation(description = "Does the store contain the given key") + @Override + public boolean contains(String key) { + String value = valueOperations.get(createRedisKey(key)); + return value != null; } - } - @Override - public boolean add(String key) { - boolean added = valueOperations.setIfAbsent(createRedisKey(key), key); - if (expiry != null) { - valueOperations.getOperations().expire(createRedisKey(key), expiry, TimeUnit.SECONDS); + @ManagedOperation(description = "Adds the key to the store") + @Override + public boolean add(String key) { + boolean added = valueOperations.setIfAbsent(createRedisKey(key), key); + if (expiry > 0) { + valueOperations.getOperations().expire(createRedisKey(key), expiry, TimeUnit.SECONDS); + } + return added; } - return added; - } - @Override - public boolean remove(String key) { - valueOperations.getOperations().delete(createRedisKey(key)); - return true; - } + @ManagedOperation(description = "Remove the key from the store") + @Override + public boolean remove(String key) { + valueOperations.getOperations().delete(createRedisKey(key)); + return true; + } - public void clear() { - valueOperations.getOperations().execute(new RedisCallback<List<byte[]>>() { - @Override - public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException { - List<byte[]> binaryKeys = new ArrayList<>(); - Cursor<byte[]> - cursor = - connection.scan(ScanOptions.scanOptions().match("*" + createRedisKey("*")).build()); + @ManagedOperation(description = "Clear the store") + @Override + public void clear() { + valueOperations.getOperations().execute(new RedisCallback<List<byte[]>>() { + @Override + public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException { + List<byte[]> binaryKeys = new ArrayList<>(); + Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().match("*" + createRedisKey("*")).build()); - while (cursor.hasNext()) { - byte[] key = cursor.next(); - binaryKeys.add(key); - } - if (binaryKeys.size() > 0) { - connection.del(binaryKeys.toArray(new byte[][]{})); - } - return binaryKeys; - } - }); - } + while (cursor.hasNext()) { + byte[] key = cursor.next(); + binaryKeys.add(key); + } + if (binaryKeys.size() > 0) { + connection.del(binaryKeys.toArray(new byte[][]{})); + } + return binaryKeys; + } + }); + } - public String createRedisKey(String key) { - return new StringBuilder(getProcessorName()).append(":").append(key).toString(); - } + protected String createRedisKey(String key) { + return getProcessorName() + ":" + key; + } - public Long getExpiry() { - return expiry; - } + public long getExpiry() { + return expiry; + } - /** - * Exire all newly added items after the given number of seconds - */ - public void setExpiry(Long expiry) { - this.expiry = expiry; - } + /** + * Expire all newly added items after the given number of seconds (0 means never expire) + */ + public void setExpiry(long expiry) { + this.expiry = expiry; + } }
