gnodet commented on code in PR #22158:
URL: https://github.com/apache/camel/pull/22158#discussion_r3756433396


##########
components/camel-state-store/camel-state-store-infinispan/src/main/java/org/apache/camel/component/statestore/infinispan/InfinispanStateStoreBackend.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.statestore.infinispan;
+
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.component.statestore.StateStoreBackend;
+import org.infinispan.client.hotrod.RemoteCache;
+import org.infinispan.client.hotrod.RemoteCacheManager;
+import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A {@link StateStoreBackend} implementation backed by Infinispan remote (Hot 
Rod). Supports per-entry TTL via
+ * Infinispan's lifespan parameter.
+ */
+public class InfinispanStateStoreBackend implements StateStoreBackend {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(InfinispanStateStoreBackend.class);
+
+    private RemoteCacheManager cacheManager;
+    private RemoteCache<String, Object> cache;
+    private String hosts = "localhost:11222";
+    private String cacheName = "camel-state-store";
+    private boolean managedCacheManager;
+
+    @Override
+    public Object put(String key, Object value, long ttlMillis) {
+        if (ttlMillis > 0) {
+            return cache.put(key, value, ttlMillis, TimeUnit.MILLISECONDS);
+        }
+        return cache.put(key, value);
+    }
+
+    @Override
+    public Object putIfAbsent(String key, Object value, long ttlMillis) {
+        if (ttlMillis > 0) {
+            return cache.putIfAbsent(key, value, ttlMillis, 
TimeUnit.MILLISECONDS);
+        }
+        return cache.putIfAbsent(key, value);
+    }
+
+    @Override
+    public Object get(String key) {
+        return cache.get(key);
+    }
+
+    @Override
+    public Object delete(String key) {
+        return cache.remove(key);
+    }
+
+    @Override
+    public boolean contains(String key) {
+        return cache.containsKey(key);
+    }
+
+    @Override
+    public Set<String> keys() {
+        return Set.copyOf(cache.keySet());
+    }
+
+    @Override
+    public int size() {
+        return cache.size();
+    }
+
+    @Override
+    public void clear() {
+        cache.clear();
+    }
+
+    @Override
+    public void start() {
+        if (cache != null) {
+            return;
+        }
+        if (cacheManager == null) {
+            ConfigurationBuilder builder = new ConfigurationBuilder();
+            builder.addServers(hosts);
+            builder.forceReturnValues(true);
+            cacheManager = new RemoteCacheManager(builder.build(), true);
+            managedCacheManager = true;
+        }
+        // Retry cache creation to handle server startup race conditions
+        Exception lastException = null;
+        for (int i = 0; i < 10; i++) {
+            try {
+                cache = 
cacheManager.administration().getOrCreateCache(cacheName, (String) null);
+                return;
+            } catch (Exception e) {
+                lastException = e;
+                LOG.warn("Failed to access cache '{}' (attempt {}/10): {}", 
cacheName, i + 1, e.getMessage());
+                try {
+                    Thread.sleep(1000);

Review Comment:
   Good suggestion! I considered it but decided against it to keep the backend 
module dependency-minimal. resilience4j would add a transitive dependency just 
for a startup retry loop that only runs once during backend initialization. The 
exponential backoff loop (now updated from fixed 1s to 200ms–5s backoff) is 
straightforward and self-contained — it handles the narrow case of Infinispan 
server startup race conditions.
   
   If the retry logic were more complex (circuit-breaking on operations, 
bulkhead patterns, etc.), resilience4j would be the right tool, but for a 
one-time startup sequence I think plain retry-with-backoff is the pragmatic 
choice.
   
   _Claude Code on behalf of Guillaume Nodet_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to