This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 5f72cef67cba815824c56c7e4160346d6aa5ca56
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Wed May 22 13:53:14 2024 +0200

    Fix unneeded code in ServicePool
    
    The service is always in a started state.  The problem was caused by the 
field being set (while holding the lock) to the newly created service before it 
was actually started, so that another thread could see the non started service.
---
 .../apache/camel/support/cache/ServicePool.java    | 41 ++--------------------
 1 file changed, 2 insertions(+), 39 deletions(-)

diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/cache/ServicePool.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/cache/ServicePool.java
index 3596bd4d8ec..de7b5b513e1 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/cache/ServicePool.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/cache/ServicePool.java
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.support.cache;
 
-import java.time.Duration;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -29,14 +28,9 @@ import java.util.function.Function;
 import org.apache.camel.Endpoint;
 import org.apache.camel.NonManagedService;
 import org.apache.camel.Service;
-import org.apache.camel.StatefulService;
 import org.apache.camel.support.LRUCache;
 import org.apache.camel.support.LRUCacheFactory;
 import org.apache.camel.support.service.ServiceSupport;
-import org.apache.camel.support.task.BlockingTask;
-import org.apache.camel.support.task.Tasks;
-import org.apache.camel.support.task.budget.Budgets;
-import org.apache.camel.support.task.budget.IterationBoundedBudget;
 import org.apache.camel.util.function.ThrowingFunction;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -136,24 +130,6 @@ abstract class ServicePool<S extends Service> extends 
ServiceSupport implements
         return s;
     }
 
-    private void waitForService(StatefulService service) {
-        BlockingTask task = 
Tasks.foregroundTask().withBudget(Budgets.iterationTimeBudget()
-                .withMaxIterations(IterationBoundedBudget.UNLIMITED_ITERATIONS)
-                .withMaxDuration(Duration.ofMillis(30000))
-                .withInterval(Duration.ofMillis(5))
-                .build())
-                .build();
-
-        if (!task.run(service::isStarting)) {
-            LOG.warn("The producer: {} did not finish starting in {} ms", 
service, 30000);
-        }
-
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Waited {} ms for producer to finish starting: {} state: 
{}", task.elapsed().toMillis(), service,
-                    service.getStatus());
-        }
-    }
-
     /**
      * Releases the producer/consumer back to the pool
      *
@@ -249,13 +225,6 @@ abstract class ServicePool<S extends Service> extends 
ServiceSupport implements
                         S tempS = creator.apply(endpoint);
                         endpoint.getCamelContext().addService(tempS, true, 
true);
                         s = tempS;
-
-                        if (s instanceof StatefulService ss) {
-                            if (ss.isStarting()) {
-                                LOG.trace("Waiting for producer to finish 
starting: {}", s);
-                                waitForService(ss);
-                            }
-                        }
                     }
                 }
             }
@@ -342,8 +311,9 @@ abstract class ServicePool<S extends Service> extends 
ServiceSupport implements
                 synchronized (lock) {
                     if (!evicts.isEmpty()) {
                         for (S evict : evicts) {
-                            doStop(evict);
                             queue.remove(evict);
+                            // stop the service after having removed it from 
queue
+                            doStop(evict);
                         }
                         evicts.clear();
                     }
@@ -361,13 +331,6 @@ abstract class ServicePool<S extends Service> extends 
ServiceSupport implements
                 if (s == null) {
                     s = creator.apply(endpoint);
                     s.start();
-
-                    if (s instanceof StatefulService ss) {
-                        if (ss.isStarting()) {
-                            LOG.trace("Waiting for producer to finish 
starting: {}", s);
-                            waitForService(ss);
-                        }
-                    }
                 }
             }
             return s;

Reply via email to