This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new b7464a363a51 CAMEL-27982: camel-spring-redis -
SpringRedisIdempotentRepository.clear() deletes only the set of the repository
(#31222)
b7464a363a51 is described below
commit b7464a363a5115bc06fcb7b58d79db17fc0f5423
Author: Adriano Machado <[email protected]>
AuthorDate: Wed Dec 2 03:34:33 2026 -0500
CAMEL-27982: camel-spring-redis - SpringRedisIdempotentRepository.clear()
deletes only the set of the repository (#31222)
- CAMEL-27982: camel-spring-redis - SpringRedisIdempotentRepository.clear()
deletes only the set of the repository
- CAMEL-27982: camel-spring-redis - release the connection of flushOnStartup
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../SpringRedisIdempotentRepository.java | 11 ++++++++--
.../SpringRedisIdempotentRepositoryTest.java | 24 ++++++++++++++++++++--
.../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc | 7 +++++++
3 files changed, 38 insertions(+), 4 deletions(-)
diff --git
a/components/camel-spring-parent/camel-spring-redis/src/main/java/org/apache/camel/component/redis/processor/idempotent/SpringRedisIdempotentRepository.java
b/components/camel-spring-parent/camel-spring-redis/src/main/java/org/apache/camel/component/redis/processor/idempotent/SpringRedisIdempotentRepository.java
index 6e54eab39f34..b8902b5e402b 100644
---
a/components/camel-spring-parent/camel-spring-redis/src/main/java/org/apache/camel/component/redis/processor/idempotent/SpringRedisIdempotentRepository.java
+++
b/components/camel-spring-parent/camel-spring-redis/src/main/java/org/apache/camel/component/redis/processor/idempotent/SpringRedisIdempotentRepository.java
@@ -25,6 +25,7 @@ import org.apache.camel.spi.IdempotentRepository;
import org.apache.camel.spi.Metadata;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.ObjectHelper;
+import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
@@ -106,7 +107,9 @@ public class SpringRedisIdempotentRepository extends
ServiceSupport implements I
@Override
@ManagedOperation(description = "Clear the store")
public void clear() {
-
redisTemplate.getConnectionFactory().getConnection().serverCommands().flushDb();
+ // the keys of this repository are the members of a single set, so
deleting that set clears the store
+ // without touching the other keys of the database
+ redisTemplate.delete(repositoryName);
}
public void setRepositoryName(String repositoryName) {
@@ -135,7 +138,11 @@ public class SpringRedisIdempotentRepository extends
ServiceSupport implements I
ObjectHelper.notNull(this.redisTemplate, "redisTemplate", this);
this.setOperations = redisTemplate.opsForSet();
if (flushOnStartup) {
-
redisTemplate.getConnectionFactory().getConnection().serverCommands().flushDb();
+ // execute borrows the connection and returns it to the pool
afterwards
+ redisTemplate.execute((RedisCallback<Void>) connection -> {
+ connection.serverCommands().flushDb();
+ return null;
+ });
}
}
diff --git
a/components/camel-spring-parent/camel-spring-redis/src/test/java/org/apache/camel/component/redis/processor/idempotent/SpringRedisIdempotentRepositoryTest.java
b/components/camel-spring-parent/camel-spring-redis/src/test/java/org/apache/camel/component/redis/processor/idempotent/SpringRedisIdempotentRepositoryTest.java
index 3dc453e6f170..ad8d27019168 100644
---
a/components/camel-spring-parent/camel-spring-redis/src/test/java/org/apache/camel/component/redis/processor/idempotent/SpringRedisIdempotentRepositoryTest.java
+++
b/components/camel-spring-parent/camel-spring-redis/src/test/java/org/apache/camel/component/redis/processor/idempotent/SpringRedisIdempotentRepositoryTest.java
@@ -24,12 +24,15 @@ import org.mockito.quality.Strictness;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisServerCommands;
+import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -118,9 +121,26 @@ public class SpringRedisIdempotentRepositoryTest {
}
@Test
- public void shouldClearRepository() {
+ public void shouldClearOnlyTheSetOfTheRepository() {
idempotentRepository.clear();
- verify(redisConnection.serverCommands()).flushDb();
+
+ // only the set that holds the keys of this repository is deleted,
other data of the database is kept
+ verify(redisTemplate).delete(REPOSITORY);
+ verify(redisServerCommands, never()).flushDb();
+ }
+
+ @Test
+ public void shouldFlushOnStartupThroughTheTemplate() {
+ // RedisTemplate.execute runs the callback and gives the connection
back to the pool afterwards
+ when(redisTemplate.execute(any(RedisCallback.class)))
+ .thenAnswer(invocation -> invocation.<RedisCallback<?>>
getArgument(0).doInRedis(redisConnection));
+ idempotentRepository.setFlushOnStartup(true);
+
+ idempotentRepository.start();
+
+ verify(redisServerCommands).flushDb();
+ // a connection taken from the factory directly is never released, so
it must not be used
+ verify(redisConnectionFactory, never()).getConnection();
}
@Test
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
index d8e420ab8402..984392f08020 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
@@ -285,3 +285,10 @@ the Infinispan embedded query engine, which Hot Rod
clients don't need: the quer
As a result, applications that use `camel-infinispan` no longer get
`infinispan-query`, `infinispan-core`, JGroups,
Hibernate Search or Lucene on their classpath through it. If your application
also starts an embedded cache manager or
uses the embedded query engine, use `camel-infinispan-embedded`, or add these
dependencies yourself.
+
+=== camel-spring-redis - SpringRedisIdempotentRepository.clear() deletes only
the set of the repository
+
+`SpringRedisIdempotentRepository.clear()` (also the `clear` JMX operation) now
deletes only the Redis set named by
+`repositoryName`, which holds the keys of the repository. Before, it ran
`FLUSHDB`, which deleted every key of the
+selected Redis database, including data that does not belong to the
repository. The `flushOnStartup` option is
+unchanged: it still deletes all keys of the database when the repository
starts.