Fixes #2020 - Introduce a name for `HttpClient` instances.

Added HttpClientMBean, and overridden getObjectContextBasis() so that
the HttpClient name is inherited by children components such as the
HttpClientTransport, the ThreadPool, etc.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2017-12-12 17:12:40 +01:00 committed by Jan Bartel
parent 18cd1fb8df
commit f2bf3d63bc
4 changed files with 109 additions and 2 deletions

View File

@ -99,6 +99,12 @@
<artifactId>jetty-io</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>

View File

@ -146,6 +146,7 @@ public class HttpClient extends ContainerLifeCycle
private HttpField encodingField;
private boolean removeIdleDestinations = false;
private boolean connectBlocking = false;
private String name = getClass().getSimpleName() + "@" + Integer.toHexString(hashCode());
/**
* Creates a {@link HttpClient} instance that can perform requests to non-TLS destinations only
@ -196,8 +197,6 @@ public class HttpClient extends ContainerLifeCycle
if (sslContextFactory != null)
addBean(sslContextFactory);
String name = HttpClient.class.getSimpleName() + "@" + hashCode();
if (executor == null)
{
QueuedThreadPool threadPool = new QueuedThreadPool();
@ -639,6 +638,17 @@ public class HttpClient extends ContainerLifeCycle
this.byteBufferPool = byteBufferPool;
}
@ManagedAttribute("The name of this HttpClient")
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
/**
* @return the max time, in milliseconds, a connection can take to connect to destinations
*/

View File

@ -0,0 +1,40 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.client.jmx;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.jmx.ObjectMBean;
public class HttpClientMBean extends ObjectMBean
{
public HttpClientMBean(Object managedObject)
{
super(managedObject);
}
@Override
public String getObjectContextBasis()
{
// Returning the HttpClient name as the "context" property
// because it is inherited by the ObjectNames of the components
// of HttpClient such as the transport, the threadpool, etc.
HttpClient httpClient = (HttpClient)getManagedObject();
return httpClient.getName();
}
}

View File

@ -0,0 +1,51 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.client.jmx;
import java.lang.management.ManagementFactory;
import java.util.Locale;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.junit.Assert;
import org.junit.Test;
public class HttpClientJMXTest
{
@Test
public void testHttpClientName() throws Exception
{
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer);
HttpClient httpClient = new HttpClient();
httpClient.addBean(mbeanContainer);
httpClient.start();
String domain = HttpClient.class.getPackage().getName();
ObjectName pattern = new ObjectName(domain + ":type=" + HttpClient.class.getSimpleName().toLowerCase(Locale.ENGLISH) + ",*");
Set<ObjectName> objectNames = mbeanServer.queryNames(pattern, null);
Assert.assertEquals(1, objectNames.size());
ObjectName objectName = objectNames.iterator().next();
Assert.assertEquals(httpClient.getName(), objectName.getKeyProperty("context"));
}
}