Last active
December 12, 2015 10:48
-
-
Save ricston-git/4761408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="queueSizeExporter" class="org.springframework.jmx.export.MBeanExporter"> | |
<property name="beans"> | |
<map> | |
<entry key="Mule.${app.name}:category=custom,name=queueSize" value-ref="mbeanQueueSizeAttributeQuery" /> | |
</map> | |
</property> | |
</bean> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<task:scheduler id="mbeanQueueSizeUpdaterScheduler" pool-size="2" /> | |
<task:scheduled-tasks scheduler="mbeanQueueSizeUpdaterScheduler"> | |
<task:scheduled ref="mbeanQueueSizeAttributeQuery" method="retrieveMbeanAttribute" fixed-delay="10000"/> | |
</task:scheduled-tasks> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="mbeanQueueSizeAttributeQuery" class="com.mulesoft.monitor.jmx.MBeanAttributeQuery" init-method="initialise" destroy-method="destroy"> | |
<property name="jmxUrl" value="service:jmx:rmi:///jndi/rmi://${jms.ip}:${jms.jmxPort}/jmxrmi" /> | |
<property name="objectName" value="org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=${jms.queueName}" /> | |
<property name="attribute" value="QueueSize" /> | |
</bean> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MBeanAttributeQuery implements NotificationListener | |
{ | |
private String jmxUrl; | |
private String objectName; | |
private String attribute; | |
protected JMXConnector jmxConnector; | |
protected MBeanServerConnection mbeanServer; | |
protected ObjectName objectNameExpression; | |
protected boolean connectionClosed = true; | |
private Object value; | |
private String stringValue; | |
protected Log logger = LogFactory.getLog(getClass()); | |
protected void checkConnection() throws IOException | |
{ | |
//only connect if the jmxUrl is set and the connection is closed | |
if ((jmxUrl != null) && connectionClosed) | |
{ | |
try | |
{ | |
//create a JMX connection and retrieve an mbean server connection | |
JMXServiceURL url = new JMXServiceURL(jmxUrl); | |
jmxConnector = JMXConnectorFactory.connect(url, null); | |
mbeanServer = jmxConnector.getMBeanServerConnection(); | |
try | |
{ | |
//try to remove the listener, this is necessary in case a connection was already established | |
//and a listener already added. Avoiding adding listener twice | |
jmxConnector.removeConnectionNotificationListener(this, null, null); | |
} | |
catch (ListenerNotFoundException e) | |
{ | |
// ignore exception | |
} | |
//add this class as a connection notification listener | |
jmxConnector.addConnectionNotificationListener(this, null, null); | |
//since we managed to connect, set connectionClosed to false | |
connectionClosed = false; | |
} | |
catch (IOException exception) | |
{ | |
logger.error("Could not connect to JMX server " + jmxUrl + ", will retry on the next poll.", exception); | |
} | |
} | |
} | |
public void initialise() throws IOException, MalformedObjectNameException, NullPointerException | |
{ | |
// if jmxUrl is not null, use it to connect | |
checkConnection(); | |
// if jmxUrl is null, connect to the local mbean server | |
if (jmxUrl == null) | |
{ | |
mbeanServer = ManagementFactory.getPlatformMBeanServer(); | |
} | |
objectNameExpression = new ObjectName(objectName); | |
} | |
public void destroy() throws IOException | |
{ | |
if (jmxConnector != null) | |
{ | |
jmxConnector.close(); | |
} | |
} | |
public void retrieveMbeanAttribute() | |
throws MalformedObjectNameException, NullPointerException, InstanceNotFoundException, | |
IntrospectionException, ReflectionException, AttributeNotFoundException, MBeanException, IOException | |
{ | |
checkConnection(); | |
value = mbeanServer.getAttribute(objectNameExpression, attribute); | |
stringValue = value.toString(); | |
logger.debug("Value collected = " + stringValue); | |
} | |
/** | |
* if we receive a notification that the connection is closed, set the | |
* connectionClosed to true in order to reconnect on the next poll | |
*/ | |
@Override | |
public void handleNotification(Notification notification, Object handback) | |
{ | |
if ("jmx.remote.connection.closed".equals(notification.getType())) | |
{ | |
connectionClosed = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment