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

davsclaus pushed a commit to branch camel-3.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.18.x by this push:
     new b0e97f6c3ae CAMEL-18816: camel-ahc component crashes when traffic 
starts too early (#8907)
b0e97f6c3ae is described below

commit b0e97f6c3aed1f527c52f732a1178052c8706df5
Author: krzysztof-mackowiak 
<[email protected]>
AuthorDate: Thu Dec 15 18:25:59 2022 +0100

    CAMEL-18816: camel-ahc component crashes when traffic starts too early 
(#8907)
    
    * CAMEL-18816: camel-ahc component crashes when traffic starts too early - 
bugfix
    
    * CAMEL-18816: checkstyle fixes
    
    Co-authored-by: Krzysztof Mackowiak <[email protected]>
---
 .../apache/camel/component/ahc/AhcEndpoint.java    | 84 +++++++++++++---------
 .../apache/camel/component/ahc/AhcProducer.java    |  2 +-
 .../AhcRecipientListInOnlyWithTryBlockTest.java    | 52 ++++++++++++++
 .../ahc/AhcRecipientListInOutWithTryBlockTest.java | 57 +++++++++++++++
 .../camel/component/ahc/AhcRecipientListTest.java  | 45 ++++++++++++
 5 files changed, 206 insertions(+), 34 deletions(-)

diff --git 
a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcEndpoint.java
 
b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcEndpoint.java
index bf1a4f828f8..b1d24fa3152 100644
--- 
a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcEndpoint.java
+++ 
b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcEndpoint.java
@@ -16,7 +16,9 @@
  */
 package org.apache.camel.component.ahc;
 
+import java.io.IOException;
 import java.net.URI;
+import java.security.GeneralSecurityException;
 import java.util.Map;
 
 import javax.net.ssl.SSLContext;
@@ -91,7 +93,6 @@ public class AhcEndpoint extends DefaultEndpoint implements 
AsyncEndpoint, Heade
 
     @Override
     public Producer createProducer() throws Exception {
-        ObjectHelper.notNull(client, "AsyncHttpClient", this);
         ObjectHelper.notNull(httpUri, "HttpUri", this);
         ObjectHelper.notNull(binding, "AhcBinding", this);
         return new AhcProducer(this);
@@ -108,14 +109,21 @@ public class AhcEndpoint extends DefaultEndpoint 
implements AsyncEndpoint, Heade
         return true;
     }
 
-    public AsyncHttpClient getClient() {
+    public synchronized AsyncHttpClient getClient() {
+        return client;
+    }
+
+    protected synchronized AsyncHttpClient getOrCreateClient() {
+        if (client == null) {
+            client = createClient();
+        }
         return client;
     }
 
     /**
      * To use a custom {@link AsyncHttpClient}
      */
-    public void setClient(AsyncHttpClient client) {
+    public synchronized void setClient(AsyncHttpClient client) {
         this.client = client;
     }
 
@@ -278,35 +286,37 @@ public class AhcEndpoint extends DefaultEndpoint 
implements AsyncEndpoint, Heade
     @Override
     protected void doStart() throws Exception {
         super.doStart();
-        if (client == null) {
+    }
+
+    private AsyncHttpClient createClient() {
+        AsyncHttpClientConfig config;
 
-            AsyncHttpClientConfig config;
-
-            if (clientConfig != null) {
-                DefaultAsyncHttpClientConfig.Builder builder = 
AhcComponent.cloneConfig(clientConfig);
-
-                if (sslContextParameters != null) {
-                    SSLContext sslContext = 
sslContextParameters.createSSLContext(getCamelContext());
-                    JdkSslContext ssl = new JdkSslContext(sslContext, true, 
ClientAuth.REQUIRE);
-                    builder.setSslContext(ssl);
-                }
-
-                config = builder.build();
-            } else {
-                DefaultAsyncHttpClientConfig.Builder builder = new 
DefaultAsyncHttpClientConfig.Builder();
-                /*
-                 * Not doing this will always create a cookie handler per 
endpoint, which is incompatible
-                 * to prior versions and interferes with the cookie handling 
in camel
-                 */
-                builder.setCookieStore(null);
-                if (sslContextParameters != null) {
-                    SSLContext sslContext = 
sslContextParameters.createSSLContext(getCamelContext());
-                    JdkSslContext ssl = new JdkSslContext(sslContext, true, 
ClientAuth.REQUIRE);
-                    builder.setSslContext(ssl);
-                }
-                config = builder.build();
+        if (clientConfig != null) {
+            DefaultAsyncHttpClientConfig.Builder builder = 
AhcComponent.cloneConfig(clientConfig);
+            configureSslContext(builder);
+            config = builder.build();
+        } else {
+            DefaultAsyncHttpClientConfig.Builder builder = new 
DefaultAsyncHttpClientConfig.Builder();
+            /*
+             * Not doing this will always create a cookie handler per 
endpoint, which is incompatible
+             * to prior versions and interferes with the cookie handling in 
camel
+             */
+            builder.setCookieStore(null);
+            configureSslContext(builder);
+            config = builder.build();
+        }
+        return createClient(config);
+    }
+
+    private void configureSslContext(DefaultAsyncHttpClientConfig.Builder 
builder) {
+        try {
+            if (sslContextParameters != null) {
+                SSLContext sslContext = 
sslContextParameters.createSSLContext(getCamelContext());
+                JdkSslContext ssl = new JdkSslContext(sslContext, true, 
ClientAuth.REQUIRE);
+                builder.setSslContext(ssl);
             }
-            client = createClient(config);
+        } catch (GeneralSecurityException | IOException e) {
+            throw new IllegalArgumentException("Invalid SSL context 
parameters", e);
         }
     }
 
@@ -322,10 +332,18 @@ public class AhcEndpoint extends DefaultEndpoint 
implements AsyncEndpoint, Heade
     protected void doStop() throws Exception {
         super.doStop();
         // ensure client is closed when stopping
-        if (client != null && !client.isClosed()) {
-            client.close();
+        AsyncHttpClient clientToClose;
+        synchronized (this) {
+            clientToClose = this.client;
+            this.client = null;
+        }
+        closeClient(clientToClose);
+    }
+
+    private void closeClient(AsyncHttpClient clientToClose) throws IOException 
{
+        if (clientToClose != null && !clientToClose.isClosed()) {
+            clientToClose.close();
         }
-        client = null;
     }
 
 }
diff --git 
a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcProducer.java
 
b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcProducer.java
index 6bfe833e8e6..ed4b1d27398 100644
--- 
a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcProducer.java
+++ 
b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcProducer.java
@@ -43,7 +43,7 @@ public class AhcProducer extends DefaultAsyncProducer {
 
     public AhcProducer(AhcEndpoint endpoint) {
         super(endpoint);
-        this.client = endpoint.getClient();
+        this.client = endpoint.getOrCreateClient();
     }
 
     @Override
diff --git 
a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcRecipientListInOnlyWithTryBlockTest.java
 
b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcRecipientListInOnlyWithTryBlockTest.java
new file mode 100644
index 00000000000..96c40661e06
--- /dev/null
+++ 
b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcRecipientListInOnlyWithTryBlockTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.ahc;
+
+import org.apache.camel.LoggingLevel;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+public class AhcRecipientListInOnlyWithTryBlockTest extends BaseAhcTest {
+
+    @Test
+    public void testRecipientListCalledBeforeComponentStarted() throws 
Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("timer://foo?delay=-1&?repeatCount=1")
+                    .doTry()
+                        .recipientList(constant(getAhcEndpointUri()))
+                        .to("mock:result")
+                    .endDoTry()
+                    .doCatch(Exception.class)
+                        .log(LoggingLevel.ERROR, "Exception occured 
${exception}")
+                        .transform(constant("Exception occured"))
+                    .end();
+
+                from(getTestServerEndpointUri())
+                        .transform(constant("Hello"));
+            }
+        };
+    }
+}
diff --git 
a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcRecipientListInOutWithTryBlockTest.java
 
b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcRecipientListInOutWithTryBlockTest.java
new file mode 100644
index 00000000000..a789ae7cbdf
--- /dev/null
+++ 
b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcRecipientListInOutWithTryBlockTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.ahc;
+
+import org.apache.camel.LoggingLevel;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class AhcRecipientListInOutWithTryBlockTest extends BaseAhcTest {
+
+    @Test
+    public void testRecipientListCalledBeforeComponentStarted() throws 
Exception {
+        String response = template.requestBody("direct:start", "Hello", 
String.class);
+
+        assertEquals("Bye", response);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("timer://foo?delay=-1&?repeatCount=1")
+                        .to("direct:start");
+
+                from("direct:start")
+                    .doTry()
+                        .recipientList(constant(getAhcEndpointUri()))
+                    .endDoTry()
+                    .doCatch(Exception.class)
+                        .log(LoggingLevel.ERROR, "Exception occured 
${exception}")
+                        .transform(constant("Exception occured"))
+                    .end();
+
+                from(getTestServerEndpointUri())
+                        .transform(constant("Bye"));
+            }
+        };
+    }
+
+}
diff --git 
a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcRecipientListTest.java
 
b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcRecipientListTest.java
new file mode 100644
index 00000000000..8508a8760d1
--- /dev/null
+++ 
b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcRecipientListTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.ahc;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+public class AhcRecipientListTest extends BaseAhcTest {
+
+    @Test
+    public void testRecipientListCalledBeforeComponentStarted() throws 
Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("timer://foo?delay=-1&?repeatCount=1")
+                        .recipientList(constant(getAhcEndpointUri()))
+                        .to("mock:result");
+
+                from(getTestServerEndpointUri())
+                        .transform(constant("Hello"));
+            }
+        };
+    }
+}

Reply via email to