You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/05/11 18:50:09 UTC

[camel] branch main updated: CAMEL-18056: camel-jbang - Dependency downloader - Use thread pool and report progress

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 66674d6ca23 CAMEL-18056: camel-jbang - Dependency downloader - Use thread pool and report progress
66674d6ca23 is described below

commit 66674d6ca23dfb7b7da8b5b6d6eaa1bd8b1c8c9c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed May 11 20:49:47 2022 +0200

    CAMEL-18056: camel-jbang - Dependency downloader - Use thread pool and report progress
---
 .../apache/camel/support/task/BackgroundTask.java  |  2 -
 .../org/apache/camel/main/DownloadThreadPool.java  | 64 ++++++++++++++++++++++
 .../org/apache/camel/main/DownloaderHelper.java    | 23 +++-----
 3 files changed, 71 insertions(+), 18 deletions(-)

diff --git a/core/camel-support/src/main/java/org/apache/camel/support/task/BackgroundTask.java b/core/camel-support/src/main/java/org/apache/camel/support/task/BackgroundTask.java
index 01fe497f523..a1dcb1a2378 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/task/BackgroundTask.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/task/BackgroundTask.java
@@ -48,7 +48,6 @@ public class BackgroundTask implements BlockingTask {
          * Sets a time budget for the task
          * 
          * @param  timeBudget the time budget
-         * @return
          */
         public BackgroundTaskBuilder withBudget(TimeBudget timeBudget) {
             this.budget = timeBudget;
@@ -60,7 +59,6 @@ public class BackgroundTask implements BlockingTask {
          * Sets an executor service manager for managing the threads
          *
          * @param  service an instance of an executor service to use
-         * @return
          */
         public BackgroundTaskBuilder withScheduledExecutor(ScheduledExecutorService service) {
             this.service = service;
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloadThreadPool.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloadThreadPool.java
new file mode 100644
index 00000000000..01108c6206b
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloadThreadPool.java
@@ -0,0 +1,64 @@
+/*
+ * 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.main;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.util.StopWatch;
+import org.apache.camel.util.TimeUtils;
+import org.slf4j.Logger;
+
+class DownloadThreadPool {
+
+    private final ExecutorService executorService = Executors.newCachedThreadPool();
+
+    public void download(Logger log, Runnable task, String gav) {
+        Future<?> future = executorService.submit(task);
+        awaitCompletion(log, future, gav);
+    }
+
+    void awaitCompletion(Logger log, Future<?> future, String gav) {
+        StopWatch watch = new StopWatch();
+        boolean done = false;
+        while (!done) {
+            try {
+                future.get(5000, TimeUnit.MILLISECONDS);
+                done = true;
+            } catch (Exception e) {
+                // not complete
+            }
+            if (!done) {
+                log.info("Downloading: {} (elapsed: {})", gav, TimeUtils.printDuration(watch.taken()));
+            }
+        }
+
+        // only report at INFO if downloading took > 1s because loading from cache is faster
+        // and then it is not downloaded over the internet
+        long taken = watch.taken();
+        String msg = "Downloaded:  " + gav + " (took: "
+                     + TimeUtils.printDuration(taken) + ")";
+        if (taken < 1000) {
+            log.debug(msg);
+        } else {
+            log.info(msg);
+        }
+    }
+
+}
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloaderHelper.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloaderHelper.java
index 37d62d6055a..66be5d3bd8d 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloaderHelper.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloaderHelper.java
@@ -23,8 +23,6 @@ import java.util.Map;
 
 import groovy.grape.Grape;
 import org.apache.camel.CamelContext;
-import org.apache.camel.util.StopWatch;
-import org.apache.camel.util.TimeUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -33,6 +31,8 @@ public final class DownloaderHelper {
     private static final Logger LOG = LoggerFactory.getLogger(DownloaderHelper.class);
     private static final String CP = System.getProperty("java.class.path");
 
+    private static final DownloadThreadPool downloader = new DownloadThreadPool();
+
     private DownloaderHelper() {
     }
 
@@ -59,7 +59,6 @@ public final class DownloaderHelper {
             }
         }
 
-        StopWatch watch = new StopWatch();
         Map<String, Object> map = new HashMap<>();
         map.put("classLoader", camelContext.getApplicationContextClassLoader());
         map.put("group", groupId);
@@ -67,19 +66,11 @@ public final class DownloaderHelper {
         map.put("version", version);
         map.put("classifier", "");
 
-        LOG.debug("Downloading dependency: {}:{}:{}", groupId, artifactId, version);
-        Grape.grab(map);
-
-        // only report at INFO if downloading took > 1s because loading from cache is faster
-        // and then it is not downloaded over the internet
-        long taken = watch.taken();
-        String msg = "Downloaded dependency: " + groupId + ":" + artifactId + ":" + version + " took: "
-                     + TimeUtils.printDuration(taken);
-        if (taken < 1000) {
-            LOG.debug(msg);
-        } else {
-            LOG.info(msg);
-        }
+        String gav = groupId + ":" + artifactId + ":" + version;
+        downloader.download(LOG, () -> {
+            LOG.debug("Downloading: {}", gav);
+            Grape.grab(map);
+        }, gav);
     }
 
     public static boolean alreadyOnClasspath(CamelContext camelContext, String artifactId, String version) {