use of org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper in project opennms by OpenNMS.
the class DefaultConnectionManager method connect.
@Override
public JmxServerConnectionWrapper connect(JmxConnectors connectorName, InetAddress ipAddress, Map<String, String> properties, RetryCallback retryCallback) throws JmxServerConnectionException {
// if null, use dummy implementation
if (retryCallback == null) {
retryCallback = NULL_CALLBACK;
}
JmxServerConnectionException lastException = null;
final JmxServerConnector connector = getConnector(connectorName);
for (int i = 0; i < retries; i++) {
LOG.debug("{}/{}: Try connecting to {}", (i + 1), retries, ipAddress);
retryCallback.onRetry();
try {
JmxServerConnectionWrapper connectionWrapper = connector.createConnection(ipAddress, properties);
if (connectionWrapper == null) {
throw new JmxServerConnectionException("Received null connection");
}
return connectionWrapper;
} catch (JmxServerConnectionException ex) {
LOG.debug("Connection could not be established", ex);
lastException = ex;
}
}
if (lastException != null) {
throw lastException;
}
throw new JmxServerConnectionException("Connection could not be established. Reason: No retries left.");
}
use of org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper in project opennms by OpenNMS.
the class DefaultJmxConnector method createConnection.
public JmxServerConnectionWrapper createConnection(JmxConnectionConfig config) throws JmxServerConnectionException {
try {
// If we're trying to create a connection to a localhost address...
if (config.isLocalConnection()) {
// this JVM's MBeanServer directly.
return new PlatformMBeanServerConnector().createConnection();
}
// Create URL
final String urlString = config.getUrl();
final JMXServiceURL url = new JMXServiceURL(urlString);
LOG.debug("JMX: {} - {}", config.getFactory(), url);
// Apply password strategy
final Map<String, Object> env = new HashMap<>();
config.getPasswordStategy().apply(env, config);
// Create connection and connect
final JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, env);
try {
connector.connect(env);
} catch (SecurityException x) {
throw new JmxServerConnectionException("Security exception: bad credentials", x);
}
// Wrap Connection
MBeanServerConnection connection = connector.getMBeanServerConnection();
JmxServerConnectionWrapper connectionWrapper = new DefaultConnectionWrapper(connector, connection);
return connectionWrapper;
} catch (MalformedURLException e) {
throw new JmxServerConnectionException(e);
} catch (IOException e) {
throw new JmxServerConnectionException(e);
}
}
use of org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper in project opennms by OpenNMS.
the class JMXDetector method isServiceDetected.
public final boolean isServiceDetected(final InetAddress address, Map<String, String> runtimeAttributes) {
final String ipAddr = InetAddressUtils.str(address);
final int port = getPort();
final int retries = getRetries();
final int timeout = getTimeout();
LOG.info("isServiceDetected: {}: Checking address: {} for capability on port {}", getServiceName(), ipAddr, port);
for (int attempts = 0; attempts < retries; attempts++) {
try (final JmxServerConnectionWrapper client = this.connect(address, port, timeout, runtimeAttributes)) {
LOG.info("isServiceDetected: {}: Attempting to connect to address: {}, port: {}, attempt: #{}", getServiceName(), ipAddr, port, attempts);
if (client.getMBeanServerConnection().getMBeanCount() <= 0) {
return false;
}
if (m_object != null) {
client.getMBeanServerConnection().getObjectInstance(new ObjectName(m_object));
}
return true;
} catch (ConnectException e) {
// Connection refused!! Continue to retry.
LOG.info("isServiceDetected: {}: Unable to connect to address: {} port {}, attempt #{}", getServiceName(), ipAddr, port, attempts, e);
} catch (NoRouteToHostException e) {
// No Route to host!!!
LOG.info("isServiceDetected: {}: No route to address {} was available", getServiceName(), ipAddr, e);
} catch (final PortUnreachableException e) {
// Port unreachable
LOG.info("isServiceDetected: {}: Port unreachable while connecting to address {} port {} within timeout: {} attempt: {}", getServiceName(), ipAddr, port, timeout, attempts, e);
} catch (InterruptedIOException e) {
// Expected exception
LOG.info("isServiceDetected: {}: Did not connect to address {} port {} within timeout: {} attempt: {}", getServiceName(), ipAddr, port, timeout, attempts, e);
} catch (MalformedObjectNameException e) {
LOG.info("isServiceDetected: {}: Object instance {} is not valid on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
} catch (InstanceNotFoundException e) {
LOG.info("isServiceDetected: {}: Object instance {} does not exists on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
} catch (IOException e) {
// NMS-8096: Because the JMX connections wrap lower-level exceptions in an IOException,
// we need to unwrap the exceptions to provide INFO log messages about failures
boolean loggedIt = false;
// Unwrap exception
Throwable cause = e.getCause();
while (cause != null && loggedIt == false) {
if (cause instanceof ConnectException) {
// Connection refused!! Continue to retry.
LOG.info("isServiceDetected: {}: Unable to connect to address: {} port {}, attempt #{}", getServiceName(), ipAddr, port, attempts, e);
loggedIt = true;
} else if (cause instanceof NoRouteToHostException) {
// No Route to host!!!
LOG.info("isServiceDetected: {}: No route to address {} was available", getServiceName(), ipAddr, e);
loggedIt = true;
} else if (cause instanceof PortUnreachableException) {
// Port unreachable
LOG.info("isServiceDetected: {}: Port unreachable while connecting to address {} port {} within timeout: {} attempt: {}", getServiceName(), ipAddr, port, timeout, attempts, e);
loggedIt = true;
} else if (cause instanceof InterruptedIOException) {
// Expected exception
LOG.info("isServiceDetected: {}: Did not connect to address {} port {} within timeout: {} attempt: {}", getServiceName(), ipAddr, port, timeout, attempts, e);
loggedIt = true;
} else if (cause instanceof NameNotFoundException) {
LOG.info("isServiceDetected: {}: Name {} not found on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
loggedIt = true;
} else if (cause instanceof MalformedObjectNameException) {
LOG.info("isServiceDetected: {}: Object instance {} is not valid on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
loggedIt = true;
} else if (cause instanceof InstanceNotFoundException) {
LOG.info("isServiceDetected: {}: Object instance {} does not exists on address {} port {} within timeout: {} attempt: {}", getServiceName(), m_object, ipAddr, port, timeout, attempts, e);
loggedIt = true;
}
cause = cause.getCause();
}
if (!loggedIt) {
// If none of the causes are an expected type, log an error
LOG.error("isServiceDetected: {}: An unexpected I/O exception occured contacting address {} port {}", getServiceName(), ipAddr, port, e);
}
} catch (Throwable t) {
LOG.error("isServiceDetected: {}: Unexpected error trying to detect {} on address {} port {}", getServiceName(), getServiceName(), ipAddr, port, t);
}
}
return false;
}
Aggregations