INT-2665 JMX Endpoints And Remote MBeanServer

JMX endpoints need a reference to an MBeanServer. The MBeanServer
is a sub-interface of MBeanServerConnection.

When connecting to a remote MBeanServer, the server is an
instanceof MBeanServerConnection, not MBeanServer.

Change the Endpoints to get a reference to an MBeanServerConnection.

Add IOException catch clauses where necessary.
This commit is contained in:
Gary Russell
2012-07-17 16:28:57 -04:00
committed by Oleg Zhurakousky
parent b39b0c2681
commit c63141f6e0
5 changed files with 225 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
package org.springframework.integration.jmx;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
@@ -29,8 +29,9 @@ import org.springframework.util.Assert;
/**
* A {@link MessageSource} implementation that retrieves the current
* value of a JMX attribute each time {@link #receive()} is invoked.
*
*
* @author Mark Fisher
* @author Gary Russell
* @since 2.0
*/
public class AttributePollingMessageSource extends AbstractMessageSource<Object> {
@@ -39,18 +40,18 @@ public class AttributePollingMessageSource extends AbstractMessageSource<Object>
private volatile String attributeName;
private volatile MBeanServer server;
private volatile MBeanServerConnection server;
/**
* Provide the MBeanServer where the JMX MBean has been registered.
*/
public void setServer(MBeanServer server) {
public void setServer(MBeanServerConnection server) {
this.server = server;
}
/**
* Specify the String value of the JMX MBean's {@link ObjectName}.
* Specify the String value of the JMX MBean's {@link ObjectName}.
*/
public void setObjectName(String objectName) {
try {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,11 @@
package org.springframework.integration.jmx;
import java.io.IOException;
import javax.management.InstanceNotFoundException;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
@@ -26,7 +28,6 @@ import javax.management.ObjectName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.Message;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.support.MessageBuilder;
@@ -35,15 +36,16 @@ import org.springframework.util.Assert;
/**
* A JMX {@link NotificationListener} implementation that will send Messages
* containing the JMX {@link Notification} instances as their payloads.
*
*
* @author Mark Fisher
* @author Gary Russell
* @since 2.0
*/
public class NotificationListeningMessageProducer extends MessageProducerSupport implements NotificationListener {
private final Log logger = LogFactory.getLog(this.getClass());
private volatile MBeanServer server;
private volatile MBeanServerConnection server;
private volatile ObjectName objectName;
@@ -54,9 +56,9 @@ public class NotificationListeningMessageProducer extends MessageProducerSupport
/**
* Provide a reference to the MBeanServer where the notification
* publishing MBeans are registered.
* publishing MBeans are registered.
*/
public void setServer(MBeanServer server) {
public void setServer(MBeanServerConnection server) {
this.server = server;
}
@@ -118,7 +120,10 @@ public class NotificationListeningMessageProducer extends MessageProducerSupport
this.server.addNotificationListener(this.objectName, this, this.filter, this.handback);
}
catch (InstanceNotFoundException e) {
throw new IllegalStateException("Failed to find MBean instance.", e);
throw new IllegalStateException("Failed to find MBean instance.", e);
}
catch (IOException e) {
throw new IllegalStateException("IOException on MBeanServerConnection.", e);
}
}
@@ -137,6 +142,9 @@ public class NotificationListeningMessageProducer extends MessageProducerSupport
catch (ListenerNotFoundException e) {
throw new IllegalStateException("Failed to find NotificationListener.", e);
}
catch (IOException e) {
throw new IllegalStateException("IOException on MBeanServerConnection.", e);
}
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.jmx;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -26,7 +27,7 @@ import javax.management.JMException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
@@ -50,7 +51,7 @@ import org.springframework.util.ObjectUtils;
* will fallback to the defaults, if any have been configured on this instance via
* {@link #setObjectName(String)} and {@link #setOperationName(String)},
* respectively.
*
*
* <p>The operation parameter(s), if any, must be available within the payload of the
* Message being handled. If the target operation expects multiple parameters, they
* can be provided in either a List or Map typed payload.
@@ -61,7 +62,7 @@ import org.springframework.util.ObjectUtils;
*/
public class OperationInvokingMessageHandler extends AbstractReplyProducingMessageHandler implements InitializingBean {
private volatile MBeanServer server;
private volatile MBeanServerConnection server;
private volatile ObjectName objectName;
@@ -72,7 +73,7 @@ public class OperationInvokingMessageHandler extends AbstractReplyProducingMessa
* Provide a reference to the MBeanServer within which the MBean
* target for operation invocation has been registered.
*/
public void setServer(MBeanServer server) {
public void setServer(MBeanServerConnection server) {
this.server = server;
}
@@ -94,9 +95,9 @@ public class OperationInvokingMessageHandler extends AbstractReplyProducingMessa
/**
* Specify an operation name to be invoked when no such
* header is available on the Message being handled.
*/
*/
public void setOperationName(String operationName) {
this.operationName = operationName;
this.operationName = operationName;
}
@Override
@@ -111,7 +112,7 @@ public class OperationInvokingMessageHandler extends AbstractReplyProducingMessa
Map<String, Object> paramsFromMessage = this.resolveParameters(requestMessage);
try {
MBeanInfo mbeanInfo = this.server.getMBeanInfo(objectName);
MBeanOperationInfo[] opInfoArray = mbeanInfo.getOperations();
MBeanOperationInfo[] opInfoArray = mbeanInfo.getOperations();
boolean hasNoArgOption = false;
for (MBeanOperationInfo opInfo : opInfoArray) {
if (operationName.equals(opInfo.getName())) {
@@ -146,9 +147,12 @@ public class OperationInvokingMessageHandler extends AbstractReplyProducingMessa
}
catch (JMException e) {
throw new MessageHandlingException(requestMessage, "failed to invoke JMX operation '" +
operationName + "' on MBean [" + objectName + "]" + " with " +
operationName + "' on MBean [" + objectName + "]" + " with " +
paramsFromMessage.size() + " parameters: " + paramsFromMessage.keySet(), e);
}
catch (IOException e) {
throw new MessageHandlingException(requestMessage, "IOException on MBeanServerConnection", e);
}
}
/**