Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JMXFetch issue when several instances on the same host #124

Merged
merged 4 commits into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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