camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel] branch master updated: CAMEL-12822: camel-http4 exposes client connection stats in JMX
Date Wed, 19 Sep 2018 18:20:11 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new c052f73  CAMEL-12822: camel-http4 exposes client connection stats in JMX
c052f73 is described below

commit c052f73abd9ad51e04987f69b931268d7c033ccf
Author: Claus Ibsen <claus.ibsen@gmail.com>
AuthorDate: Wed Sep 19 20:19:35 2018 +0200

    CAMEL-12822: camel-http4 exposes client connection stats in JMX
---
 .../apache/camel/component/http4/HttpEndpoint.java |  66 +++++++++++
 .../http4/ManagedHttpProducerPoolStatsTest.java    | 130 +++++++++++++++++++++
 2 files changed, 196 insertions(+)

diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
index effcc93..c3bb606 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
@@ -26,6 +26,8 @@ import org.apache.camel.Consumer;
 import org.apache.camel.PollingConsumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.api.management.ManagedAttribute;
+import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.http.common.HttpCommonEndpoint;
 import org.apache.camel.http.common.HttpHelper;
 import org.apache.camel.http.common.cookie.CookieHandler;
@@ -42,6 +44,8 @@ import org.apache.http.conn.HttpClientConnectionManager;
 import org.apache.http.conn.ssl.DefaultHostnameVerifier;
 import org.apache.http.impl.client.BasicCookieStore;
 import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.pool.ConnPoolControl;
+import org.apache.http.pool.PoolStats;
 import org.apache.http.protocol.HttpContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -51,6 +55,7 @@ import org.slf4j.LoggerFactory;
  */
 @UriEndpoint(firstVersion = "2.3.0", scheme = "http4,https4", title = "HTTP4,HTTPS4", syntax
= "http4:httpUri",
     producerOnly = true, label = "http", lenientProperties = true)
+@ManagedResource(description = "Managed HttpEndpoint")
 public class HttpEndpoint extends HttpCommonEndpoint {
 
     private static final Logger LOG = LoggerFactory.getLogger(HttpEndpoint.class);
@@ -455,4 +460,65 @@ public class HttpEndpoint extends HttpCommonEndpoint {
         this.socketTimeout = socketTimeout;
     }
 
+    @ManagedAttribute(description = "Maximum number of allowed persistent connections")
+    public int getClientConnectionsPoolStatsMax() {
+        ConnPoolControl pool = null;
+        if (clientConnectionManager instanceof ConnPoolControl) {
+            pool = (ConnPoolControl) clientConnectionManager;
+        }
+        if (pool != null) {
+            PoolStats stats = pool.getTotalStats();
+            if (stats != null) {
+                return stats.getMax();
+            }
+        }
+        return -1;
+    }
+
+    @ManagedAttribute(description = "Number of available idle persistent connections")
+    public int getClientConnectionsPoolStatsAvailable() {
+        ConnPoolControl pool = null;
+        if (clientConnectionManager instanceof ConnPoolControl) {
+            pool = (ConnPoolControl) clientConnectionManager;
+        }
+        if (pool != null) {
+            PoolStats stats = pool.getTotalStats();
+            if (stats != null) {
+                return stats.getAvailable();
+            }
+        }
+        return -1;
+    }
+
+    @ManagedAttribute(description = "Number of persistent connections tracked by the connection
manager currently being used to execute requests")
+    public int getClientConnectionsPoolStatsLeased() {
+        ConnPoolControl pool = null;
+        if (clientConnectionManager instanceof ConnPoolControl) {
+            pool = (ConnPoolControl) clientConnectionManager;
+        }
+        if (pool != null) {
+            PoolStats stats = pool.getTotalStats();
+            if (stats != null) {
+                return stats.getLeased();
+            }
+        }
+        return -1;
+    }
+
+    @ManagedAttribute(description = "Number of connection requests being blocked awaiting
a free connection."
+        + " This can happen only if there are more worker threads contending for fewer connections.")
+    public int getClientConnectionsPoolStatsPending() {
+        ConnPoolControl pool = null;
+        if (clientConnectionManager instanceof ConnPoolControl) {
+            pool = (ConnPoolControl) clientConnectionManager;
+        }
+        if (pool != null) {
+            PoolStats stats = pool.getTotalStats();
+            if (stats != null) {
+                return stats.getPending();
+            }
+        }
+        return -1;
+    }
+
 }
diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/ManagedHttpProducerPoolStatsTest.java
b/components/camel-http4/src/test/java/org/apache/camel/component/http4/ManagedHttpProducerPoolStatsTest.java
new file mode 100644
index 0000000..5dc5171
--- /dev/null
+++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/ManagedHttpProducerPoolStatsTest.java
@@ -0,0 +1,130 @@
+/**
+ * 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.http4;
+
+import java.io.IOException;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.bootstrap.HttpServer;
+import org.apache.http.impl.bootstrap.ServerBootstrap;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ManagedHttpProducerPoolStatsTest extends BaseHttpTest {
+
+    private HttpServer localServer;
+
+    @Override
+    protected boolean useJmx() {
+        return true;
+    }
+
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        localServer = ServerBootstrap.bootstrap().
+                setHttpProcessor(getBasicHttpProcessor()).
+                setConnectionReuseStrategy(getConnectionReuseStrategy()).
+                setResponseFactory(getHttpResponseFactory()).
+                setExpectationVerifier(getHttpExpectationVerifier()).
+                setSslContext(getSSLContext()).
+                registerHandler("/myapp", new HttpRequestHandler() {
+                    @Override
+                    public void handle(HttpRequest request, HttpResponse response, HttpContext
context) throws HttpException, IOException {
+                        response.setEntity(new StringEntity("OK", "ASCII"));
+                        response.setStatusCode(HttpStatus.SC_OK);
+                    }
+                }).create();
+        localServer.start();
+
+        super.setUp();
+    }
+
+    @After
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+
+        if (localServer != null) {
+            localServer.stop();
+        }
+    }
+    
+    @Test
+    public void testPoolStats() throws Exception {
+        // turn on registering jmx always so the producer is also registered
+        context.getManagementStrategy().getManagementAgent().setRegisterAlways(true);
+
+        String uri = "http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort()
+ "/myapp";
+
+        Exchange out = template.request(uri, new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody("Hello World");
+            }
+        });
+
+        assertNotNull(out);
+        assertEquals("OK", out.getOut().getBody(String.class));
+
+        // look up stats
+        HttpEndpoint http = context.getEndpoint(uri, HttpEndpoint.class);
+        assertNotNull(http);
+
+        int max = http.getClientConnectionsPoolStatsMax();
+        int avail = http.getClientConnectionsPoolStatsAvailable();
+        int leased = http.getClientConnectionsPoolStatsLeased();
+        int pending = http.getClientConnectionsPoolStatsPending();
+
+        assertEquals(200, max);
+        assertEquals(1, avail);
+        assertEquals(0, leased);
+        assertEquals(0, pending);
+
+        // should be in JMX too
+        MBeanServer mbeanServer = getMBeanServer();
+        String id = context.getManagementName();
+        ObjectName on = ObjectName.getInstance("org.apache.camel:context=" + id + ",type=endpoints,name=\""
+ uri + "\"");
+        assertTrue(mbeanServer.isRegistered(on));
+
+        max = (int) mbeanServer.getAttribute(on, "ClientConnectionsPoolStatsMax");
+        assertEquals(200, max);
+        avail = (int) mbeanServer.getAttribute(on, "ClientConnectionsPoolStatsAvailable");
+        assertEquals(1, avail);
+        leased = (int) mbeanServer.getAttribute(on, "ClientConnectionsPoolStatsLeased");
+        assertEquals(0, leased);
+        pending = (int) mbeanServer.getAttribute(on, "ClientConnectionsPoolStatsPending");
+        assertEquals(0, pending);
+    }
+
+    protected MBeanServer getMBeanServer() {
+        return context.getManagementStrategy().getManagementAgent().getMBeanServer();
+    }
+
+
+}


Mime
View raw message