This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #568 from sonatype/hc4provider-jmx
Register pooling conn manager into mmx
- Loading branch information
Showing
6 changed files
with
258 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...rc/main/java/org/sonatype/nexus/apachehttpclient/PoolingClientConnectionManagerMBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Sonatype Nexus (TM) Open Source Version | ||
* Copyright (c) 2007-2012 Sonatype, Inc. | ||
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. | ||
* | ||
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, | ||
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. | ||
* | ||
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks | ||
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the | ||
* Eclipse Foundation. All other trademarks are the property of their respective owners. | ||
*/ | ||
package org.sonatype.nexus.apachehttpclient; | ||
|
||
import org.apache.http.impl.conn.PoolingClientConnectionManager; | ||
|
||
/** | ||
* MBean which exposes details about a {@link PoolingClientConnectionManager}. | ||
* | ||
* @since 2.2 | ||
*/ | ||
public interface PoolingClientConnectionManagerMBean | ||
{ | ||
|
||
int getMaxTotal(); | ||
|
||
int getDefaultMaxPerRoute(); | ||
|
||
int getLeased(); | ||
|
||
int getPending(); | ||
|
||
int getAvailable(); | ||
|
||
int getMax(); | ||
|
||
void closeIdleConnections( long idleTimeoutInMillis ); | ||
|
||
void closeExpiredConnections(); | ||
|
||
void setMaxTotal( int max ); | ||
|
||
void setDefaultMaxPerRoute( int max ); | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
...ain/java/org/sonatype/nexus/apachehttpclient/PoolingClientConnectionManagerMBeanImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* Sonatype Nexus (TM) Open Source Version | ||
* Copyright (c) 2007-2012 Sonatype, Inc. | ||
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. | ||
* | ||
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, | ||
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. | ||
* | ||
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks | ||
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the | ||
* Eclipse Foundation. All other trademarks are the property of their respective owners. | ||
*/ | ||
package org.sonatype.nexus.apachehttpclient; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import javax.management.StandardMBean; | ||
|
||
import org.apache.http.impl.conn.PoolingClientConnectionManager; | ||
import org.sonatype.appcontext.internal.Preconditions; | ||
|
||
/** | ||
* Default {@link PoolingClientConnectionManagerMBean} implementation. | ||
* | ||
* @since 2.2 | ||
*/ | ||
class PoolingClientConnectionManagerMBeanImpl | ||
extends StandardMBean | ||
implements PoolingClientConnectionManagerMBean | ||
{ | ||
|
||
private final PoolingClientConnectionManager connMgr; | ||
|
||
PoolingClientConnectionManagerMBeanImpl( final PoolingClientConnectionManager connMgr ) | ||
{ | ||
super( PoolingClientConnectionManagerMBean.class, false ); | ||
|
||
this.connMgr = Preconditions.checkNotNull( connMgr ); | ||
} | ||
|
||
@Override | ||
public int getMaxTotal() | ||
{ | ||
return connMgr.getMaxTotal(); | ||
} | ||
|
||
@Override | ||
public int getDefaultMaxPerRoute() | ||
{ | ||
return connMgr.getDefaultMaxPerRoute(); | ||
} | ||
|
||
@Override | ||
public int getLeased() | ||
{ | ||
return connMgr.getTotalStats().getLeased(); | ||
} | ||
|
||
@Override | ||
public int getPending() | ||
{ | ||
return connMgr.getTotalStats().getPending(); | ||
} | ||
|
||
@Override | ||
public int getAvailable() | ||
{ | ||
return connMgr.getTotalStats().getAvailable(); | ||
} | ||
|
||
@Override | ||
public int getMax() | ||
{ | ||
return connMgr.getTotalStats().getMax(); | ||
} | ||
|
||
@Override | ||
public void closeIdleConnections( final long idleTimeoutInMillis ) | ||
{ | ||
connMgr.closeIdleConnections( idleTimeoutInMillis, TimeUnit.MILLISECONDS ); | ||
} | ||
|
||
@Override | ||
public void closeExpiredConnections() | ||
{ | ||
connMgr.closeExpiredConnections(); | ||
} | ||
|
||
@Override | ||
public void setMaxTotal( final int max ) | ||
{ | ||
connMgr.setMaxTotal( max ); | ||
} | ||
|
||
@Override | ||
public void setDefaultMaxPerRoute( final int max ) | ||
{ | ||
connMgr.setDefaultMaxPerRoute( max ); | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
...ava/org/sonatype/nexus/apachehttpclient/PoolingClientConnectionManagerMBeanInstaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Sonatype Nexus (TM) Open Source Version | ||
* Copyright (c) 2007-2012 Sonatype, Inc. | ||
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. | ||
* | ||
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, | ||
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. | ||
* | ||
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks | ||
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the | ||
* Eclipse Foundation. All other trademarks are the property of their respective owners. | ||
*/ | ||
package org.sonatype.nexus.apachehttpclient; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
import javax.management.MBeanServer; | ||
import javax.management.ObjectName; | ||
|
||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.impl.conn.PoolingClientConnectionManager; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* An wrapper {@link Hc4Provider} that automatically registers / unregisters JMX MBeans for each created | ||
* {@link HttpClient}s and {@link PoolingClientConnectionManager}. | ||
* | ||
* @since 2.2 | ||
*/ | ||
@Named() | ||
@Singleton | ||
public class PoolingClientConnectionManagerMBeanInstaller | ||
{ | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger( PoolingClientConnectionManagerMBeanInstaller.class ); | ||
|
||
private static final String JMX_DOMAIN = "org.sonatype.nexus.httpclient"; | ||
|
||
private ObjectName jmxName; | ||
|
||
public synchronized void register( final PoolingClientConnectionManager connectionManager ) | ||
{ | ||
if ( jmxName == null ) | ||
{ | ||
try | ||
{ | ||
jmxName = ObjectName.getInstance( | ||
JMX_DOMAIN, "name", PoolingClientConnectionManager.class.getSimpleName() | ||
); | ||
|
||
final MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | ||
server.registerMBean( new PoolingClientConnectionManagerMBeanImpl( connectionManager ), jmxName ); | ||
} | ||
catch ( final Exception e ) | ||
{ | ||
LOGGER.warn( | ||
"Failed to register mbean {} due to {}:{}", | ||
new Object[]{ jmxName, e.getClass(), e.getMessage() } | ||
); | ||
jmxName = null; | ||
} | ||
} | ||
else | ||
{ | ||
LOGGER.warn( "Already registered mbean {}", jmxName ); | ||
} | ||
} | ||
|
||
public synchronized void unregister() | ||
{ | ||
if ( jmxName != null ) | ||
{ | ||
try | ||
{ | ||
final MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | ||
server.unregisterMBean( jmxName ); | ||
} | ||
catch ( final Exception e ) | ||
{ | ||
LOGGER.warn( | ||
"Failed to unregister mbean {} due to {}:{}", | ||
new Object[]{ jmxName, e.getClass(), e.getMessage() } | ||
); | ||
} | ||
finally | ||
{ | ||
jmxName = null; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters