Skip to content

Commit

Permalink
Fix JMXFetch issue when several instances on the same host (#124)
Browse files Browse the repository at this point in the history
* Remove storage of connections in the connection manager
The connection will now be stored by the instance. No more shared connections

* Change ConnectionManager into ConnectionFactory
  • Loading branch information
zippolyte authored Feb 27, 2017
1 parent f7ebd8c commit 2ce3627
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 74 deletions.
33 changes: 33 additions & 0 deletions src/main/java/org/datadog/jmxfetch/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.datadog.jmxfetch;

import java.io.IOException;
import java.util.LinkedHashMap;

import org.apache.log4j.Logger;


/**
* Singleton used to create connections to the MBeanServer
*/
public class ConnectionFactory {
private final static Logger LOGGER = Logger.getLogger(ConnectionFactory.class.getName());
public static final String PROCESS_NAME_REGEX = "process_name_regex";
private static ConnectionFactory connectionFactory = null;

public static Connection createConnection(LinkedHashMap<String, Object> connectionParams) throws IOException {
if (connectionParams.get(PROCESS_NAME_REGEX) != null) {
try {
Class.forName( "com.sun.tools.attach.AttachNotSupportedException" );
} catch (ClassNotFoundException e) {
throw new IOException("Unable to find tools.jar. Are you using a JDK and did you set the pass to tools.jar ?");
}
LOGGER.info("Connecting using Attach API");
return new AttachApiConnection(connectionParams);

}
LOGGER.info("Connecting using JMX Remote");
return new RemoteConnection(connectionParams);

}

}
73 changes: 0 additions & 73 deletions src/main/java/org/datadog/jmxfetch/ConnectionManager.java

This file was deleted.

14 changes: 13 additions & 1 deletion src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,21 @@ public Instance(LinkedHashMap<String, Object> yamlInstance, LinkedHashMap<String
configurationList.add(new Configuration((LinkedHashMap<String, Object>) new YamlParser(this.getClass().getResourceAsStream("/jmx-2.yaml")).getParsedYaml()));
}

public Connection getConnection(LinkedHashMap<String, Object> connectionParams, boolean forceNewConnection) throws IOException {
if (connection == null || !connection.isAlive()) {
LOGGER.info("Connection closed or does not exist. Creating a new connection!");
return ConnectionFactory.createConnection(connectionParams);
} else if (forceNewConnection) {
LOGGER.info("Forcing the creation of a new connection");
connection.closeConnector();
return ConnectionFactory.createConnection(connectionParams);
}
return connection;
}

public void init(boolean forceNewConnection) throws IOException, FailedLoginException, SecurityException {
LOGGER.info("Trying to connect to JMX Server at " + this.toString());
connection = ConnectionManager.getInstance().getConnection(yaml, forceNewConnection);
connection = getConnection(yaml, forceNewConnection);
LOGGER.info("Connected to JMX Server at " + this.toString());
this.refreshBeansList();
this.getMatchingAttributes();
Expand Down

0 comments on commit 2ce3627

Please sign in to comment.