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/06/27 10:12:28 UTC

[camel] 03/04: CAMEL-18232: camel-core - Invalid ThreadName pattern using default thread pattern on some JDKs

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

commit 73f1292993b9486a170ed0c4468989b3e0b0460d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jun 27 11:53:44 2022 +0200

    CAMEL-18232: camel-core - Invalid ThreadName pattern using default thread pattern on some JDKs
---
 .../camel/impl/DefaultExecutorServiceManagerTest.java   |  2 +-
 .../org/apache/camel/util/concurrent/ThreadHelper.java  | 17 +++++++++++++----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
index 94a06de92b0..4431c923114 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
@@ -123,7 +123,7 @@ public class DefaultExecutorServiceManagerTest extends ContextTestSupport {
             context.getExecutorServiceManager().resolveThreadName("foo");
             fail("Should thrown an exception");
         } catch (IllegalArgumentException e) {
-            assertEquals("Pattern is invalid: Cool #xxx#", e.getMessage());
+            assertEquals("Pattern is invalid: [Cool #xxx#] in resolved thread name: [Cool #xxx#]", e.getMessage());
         }
 
         // reset it so we can shutdown properly
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/ThreadHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/ThreadHelper.java
index 8d402ea6b39..9d44b70ef1e 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/ThreadHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/ThreadHelper.java
@@ -56,16 +56,25 @@ public final class ThreadHelper {
         String shortName = name.contains("?") ? StringHelper.before(name, "?") : name;
 
         // replace tokens
-        String answer = pattern.replace("#counter#", Long.toString(nextThreadCounter()));
-        answer = answer.replace("#longName#", longName);
-        answer = answer.replace("#name#", shortName);
+        String answer = replaceFirst(pattern, "#longName#", longName);
+        if (shortName != null) {
+            answer = replaceFirst(answer, "#name#", shortName);
+        }
+        String next = Long.toString(nextThreadCounter());
+        answer = replaceFirst(answer, "#counter#", next);
 
         // are there any #word# combos left, if so they should be considered invalid tokens
         if (INVALID_PATTERN.matcher(answer).matches()) {
-            throw new IllegalArgumentException("Pattern is invalid: " + pattern);
+            throw new IllegalArgumentException(
+                    "Pattern is invalid: [" + pattern + "] in resolved thread name: [" + answer + "]");
         }
 
         return answer;
     }
 
+    private static String replaceFirst(String input, String from, String to) {
+        // use StringHelper.replaceFirst as replace does not work on all JDK 11s (CAMEL-18232)
+        return StringHelper.replaceFirst(input, from ,to);
+    }
+
 }