Skip to content

Commit

Permalink
improve err handling (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianciutea authored Nov 14, 2022
1 parent 1dd960d commit 46c0523
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/main/java/org/newrelic/nrjmx/v2/JMXFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ private Set<ObjectInstance> queryMBeans(ObjectName objectName) throws JMXConnect
}

try {
MBeanServerConnection conn = getConnection();

result = withConnectionExceptionHandler(() ->
getConnection().queryMBeans(objectName, null)
conn.queryMBeans(objectName, null)
);

if (internalStat != null) {
Expand Down Expand Up @@ -300,8 +302,10 @@ private List<String> getMBeanAttributeNames(ObjectName objectName) throws JMXCon
}

try {
MBeanServerConnection conn = getConnection();

info = withConnectionExceptionHandler(() ->
getConnection().getMBeanInfo(objectName)
conn.getMBeanInfo(objectName)
);

if (internalStat != null) {
Expand Down Expand Up @@ -397,11 +401,12 @@ private void getMBeanAttributes(ObjectName objectName, List<String> attributes,
List<Attribute> attrValues = new ArrayList<>();
AttributeList attributeList;
try {
MBeanServerConnection conn = getConnection();

List<String> finalAttributes = attributes;

attributeList = withConnectionExceptionHandler(() ->
getConnection().getAttributes(objectName, finalAttributes.toArray(new String[0]))
conn.getAttributes(objectName, finalAttributes.toArray(new String[0]))
);

if (internalStat != null) {
Expand Down Expand Up @@ -509,13 +514,17 @@ private void getMBeanAttribute(ObjectName objectName, String attribute, List<Att

Object value;
try {
MBeanServerConnection conn = getConnection();

value = withConnectionExceptionHandler(() ->
getConnection().getAttribute(objectName, attribute)
conn.getAttribute(objectName, attribute)
);
if (value instanceof Attribute) {
Attribute jmxAttr = (Attribute) value;
value = jmxAttr.getValue();
}
} catch (JMXConnectionError je) {
throw je;
} catch (ConnectException ce) {
String message = String.format("can't connect to JMX server, error: '%s'", ce.getMessage());
throw new JMXConnectionError(message);
Expand Down Expand Up @@ -652,6 +661,16 @@ private <T> T withTimeout(Future<T> future, long timeoutMs) throws JMXError, JMX
private <T> T withConnectionExceptionHandler(Callable<T> task) throws Exception {
try {
return task.call();
} catch (
// Not connection errors.
MBeanException
| AttributeNotFoundException
| InstanceNotFoundException
| ReflectionException
| RuntimeOperationsException
| IllegalArgumentException
| IntrospectionException e) {
throw e;
} catch (Exception e) {
if (e instanceof ConnectException) {
disconnect();
Expand Down

0 comments on commit 46c0523

Please sign in to comment.