Skip to content

Commit

Permalink
Upgrade to commons-pool2
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrille-leclerc committed May 13, 2015
1 parent f9f708e commit bfb8125
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 299 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
*/
package org.jmxtrans.embedded.output;

import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
import org.jmxtrans.embedded.EmbeddedJmxTransException;
import org.jmxtrans.embedded.QueryResult;
import org.jmxtrans.embedded.util.jmx.JmxUtils2;
import org.jmxtrans.embedded.util.net.HostAndPort;
import org.jmxtrans.embedded.util.net.SocketOutputStream;
import org.jmxtrans.embedded.util.net.SocketWriter;
import org.jmxtrans.embedded.util.pool.ManagedGenericKeyedObjectPool;
import org.jmxtrans.embedded.util.pool.SocketOutputStreamPoolFactory;
import org.python.core.*;
import org.python.modules.cPickle;
Expand Down Expand Up @@ -74,8 +74,7 @@ public class GraphitePickleWriter extends AbstractOutputWriter implements Output
*/
private String metricPathPrefix;
private HostAndPort graphiteServerHostAndPort;
private ManagedGenericKeyedObjectPool<HostAndPort, SocketOutputStream> socketOutputStreamPool;
private ObjectName socketPoolObjectName;
private GenericKeyedObjectPool<HostAndPort, SocketOutputStream> socketOutputStreamPool;

/**
* Load settings, initialize the {@link SocketWriter} pool and test the connection to the graphite server.
Expand All @@ -96,22 +95,19 @@ public void start() {
metricPathPrefix = metricPathPrefix + ".";
}

GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
config.testOnBorrow = getBooleanSetting("pool.testOnBorrow", true);
config.testWhileIdle = getBooleanSetting("pool.testWhileIdle", true);
config.maxActive = getIntSetting("pool.maxActive", -1);
config.maxIdle = getIntSetting("pool.maxIdle", -1);
config.minEvictableIdleTimeMillis = getLongSetting("pool.minEvictableIdleTimeMillis", TimeUnit.MINUTES.toMillis(5));
config.timeBetweenEvictionRunsMillis = getLongSetting("pool.timeBetweenEvictionRunsMillis", TimeUnit.MINUTES.toMillis(5));
GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
config.setTestOnBorrow(getBooleanSetting("pool.testOnBorrow", true));
config.setTestWhileIdle(getBooleanSetting("pool.testWhileIdle", true));
config.setMaxTotal(getIntSetting("pool.maxActive", -1));
config.setMaxIdlePerKey(getIntSetting("pool.maxIdle", -1));
config.setMinEvictableIdleTimeMillis(getLongSetting("pool.minEvictableIdleTimeMillis", TimeUnit.MINUTES.toMillis(5)));
config.setTimeBetweenEvictionRunsMillis(getLongSetting("pool.timeBetweenEvictionRunsMillis", TimeUnit.MINUTES.toMillis(5)));
config.setJmxNameBase("org.jmxtrans.embedded:type=GenericKeyedObjectPool,writer=GraphitePickleWriter,name=");
config.setJmxNamePrefix(graphiteServerHostAndPort.getHost() + "_" + graphiteServerHostAndPort.getPort());

int socketConnectTimeoutInMillis = getIntSetting("graphite.socketConnectTimeoutInMillis", SocketOutputStreamPoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT_IN_MILLIS);

socketOutputStreamPool = new ManagedGenericKeyedObjectPool<HostAndPort, SocketOutputStream>(new SocketOutputStreamPoolFactory(socketConnectTimeoutInMillis), config);

socketPoolObjectName = JmxUtils2.registerObject(
socketOutputStreamPool,
"org.jmxtrans.embedded:Type=SocketPool,Host=" + host + ",Port=" + port + ",Name=GraphiteSocketPool@" + System.identityHashCode(this),
ManagementFactory.getPlatformMBeanServer());
socketOutputStreamPool = new GenericKeyedObjectPool<HostAndPort, SocketOutputStream>(new SocketOutputStreamPoolFactory(socketConnectTimeoutInMillis), config);

if (isEnabled()) {
try {
Expand Down Expand Up @@ -191,6 +187,5 @@ public void stop() throws Exception {
logger.info("Stop GraphitePickleWriter connected to '{}' ...", graphiteServerHostAndPort);
super.stop();
socketOutputStreamPool.close();
JmxUtils2.unregisterObject(socketPoolObjectName, ManagementFactory.getPlatformMBeanServer());
}
}
35 changes: 15 additions & 20 deletions src/main/java/org/jmxtrans/embedded/output/GraphiteWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@
*/
package org.jmxtrans.embedded.output;

import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
import org.jmxtrans.embedded.QueryResult;
import org.jmxtrans.embedded.util.jmx.JmxUtils2;
import org.jmxtrans.embedded.util.net.HostAndPort;
import org.jmxtrans.embedded.util.net.SocketWriter;
import org.jmxtrans.embedded.util.pool.ManagedGenericKeyedObjectPool;
import org.jmxtrans.embedded.util.pool.SocketWriterPoolFactory;
import org.jmxtrans.embedded.util.pool.UDPSocketWriterPoolFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -78,9 +79,7 @@ public class GraphiteWriter extends AbstractOutputWriter implements OutputWriter

private HostAndPort graphiteServerHostAndPort;

private ManagedGenericKeyedObjectPool<HostAndPort, SocketWriter> socketWriterPool;

private ObjectName socketPoolObjectName;
private GenericKeyedObjectPool<HostAndPort, SocketWriter> socketWriterPool;

/**
* Load settings, initialize the {@link SocketWriter} pool and test the connection to the graphite server.
Expand All @@ -101,19 +100,21 @@ public void start() {
metricPathPrefix = metricPathPrefix + ".";
}

GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
config.testOnBorrow = getBooleanSetting("pool.testOnBorrow", true);
config.testWhileIdle = getBooleanSetting("pool.testWhileIdle", true);
config.maxActive = getIntSetting("pool.maxActive", -1);
config.maxIdle = getIntSetting("pool.maxIdle", -1);
config.minEvictableIdleTimeMillis = getLongSetting("pool.minEvictableIdleTimeMillis", TimeUnit.MINUTES.toMillis(5));
config.timeBetweenEvictionRunsMillis = getLongSetting("pool.timeBetweenEvictionRunsMillis", TimeUnit.MINUTES.toMillis(5));
GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
config.setTestOnBorrow(getBooleanSetting("pool.testOnBorrow", true));
config.setTestWhileIdle(getBooleanSetting("pool.testWhileIdle", true));
config.setMaxTotal(getIntSetting("pool.maxActive", -1));
config.setMaxIdlePerKey(getIntSetting("pool.maxIdle", -1));
config.setMinEvictableIdleTimeMillis(getLongSetting("pool.minEvictableIdleTimeMillis", TimeUnit.MINUTES.toMillis(5)));
config.setTimeBetweenEvictionRunsMillis(getLongSetting("pool.timeBetweenEvictionRunsMillis", TimeUnit.MINUTES.toMillis(5)));
config.setJmxNameBase("org.jmxtrans.embedded:type=GenericKeyedObjectPool,writer=GraphiteWriter,name=");
config.setJmxNamePrefix(graphiteServerHostAndPort.getHost() + "_" + graphiteServerHostAndPort.getPort());

int socketConnectTimeoutInMillis = getIntSetting("graphite.socketConnectTimeoutInMillis", SocketWriterPoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT_IN_MILLIS);

String protocol = getStringSetting(SETTING_PROTOCOL, null);
if (protocol != null && protocol.equalsIgnoreCase(PROTOCOL_UDP)) {
socketWriterPool = new ManagedGenericKeyedObjectPool<HostAndPort, SocketWriter>(new UDPSocketWriterPoolFactory("UTF-8"), config);
socketWriterPool = new GenericKeyedObjectPool<HostAndPort, SocketWriter>(new UDPSocketWriterPoolFactory(StandardCharsets.UTF_8), config);
} else {
if (protocol == null) {
// protocol not specified, use default one
Expand All @@ -122,14 +123,9 @@ public void start() {
// unknown or protocol, use default one
logger.warn("Unknown protocol specified '{}', default protocol '{}' will be used instead.",protocol, PROTOCOL_TCP);
}
socketWriterPool = new ManagedGenericKeyedObjectPool<HostAndPort, SocketWriter>(new SocketWriterPoolFactory("UTF-8", socketConnectTimeoutInMillis), config);
socketWriterPool = new GenericKeyedObjectPool<HostAndPort, SocketWriter>(new SocketWriterPoolFactory(StandardCharsets.UTF_8, socketConnectTimeoutInMillis), config);
}

socketPoolObjectName = JmxUtils2.registerObject(
socketWriterPool,
"org.jmxtrans.embedded:Type=SocketPool,Host=" + host + ",Port=" + port + ",Name=GraphiteSocketPool@" + System.identityHashCode(this),
ManagementFactory.getPlatformMBeanServer());

if (isEnabled()) {
try {
SocketWriter socketWriter = socketWriterPool.borrowObject(graphiteServerHostAndPort);
Expand Down Expand Up @@ -176,6 +172,5 @@ public void stop() throws Exception {
logger.info("Stop GraphiteWriter connected to '{}' ...", graphiteServerHostAndPort);
super.stop();
socketWriterPool.close();
JmxUtils2.unregisterObject(socketPoolObjectName, ManagementFactory.getPlatformMBeanServer());
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/jmxtrans/embedded/util/Preconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ public static String checkNotEmpty(String reference, @Nullable String message) t
}
return reference;
}

/**
* Check the given {@code state}.
*
* @param state the state top check
* @param message exception message, can be <code>null</code>
* @throws IllegalStateException if the given {@code state} is {@code false}
*/
public static void checkState(boolean state, @Nullable String message) throws IllegalStateException {
if (!state)
throw new IllegalStateException(message);
}
}

This file was deleted.

Loading

0 comments on commit bfb8125

Please sign in to comment.