Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jboner/165706 to your computer and use it in GitHub Desktop.
Save jboner/165706 to your computer and use it in GitHub Desktop.
Actor providing JMX over REST (fault-tolerant)
/**
* REST interface to Akka's JMX service.
* <p/>
* Here is an example that retreives the current number of Actors.
* <pre>
* http://localhost:9998/management
* ?service=service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
* &component=se.scalablesolutions.akka:type=Stats
* &attribute=counter_NrOfActors
* </pre>
*/
@Path("/management")
class RestfulJMX extends Actor with Logging {
private case class Request(service: String, component: String, attribute: String)
private val connectors = new ConcurrentHashMap[String, JMXConnector]
@GET
@Produces(Array("text/html"))
def queryJMX(
@QueryParam("service") service: String,
@QueryParam("component") component: String,
@QueryParam("attribute") attribute: String) =
(this !! Request(service, component, attribute)).getOrElse(<error>Error in REST JMX management service</error>)
override def receive: PartialFunction[Any, Unit] = {
case Request(service, component, attribute) => reply(retrieveAttribute(service, component, attribute))
}
private def retrieveAttribute(service: String, component: String, attribute: String): scala.xml.Elem = {
try {
var connector = connectors.putIfAbsent(service, JMXConnectorFactory.connect(new JMXServiceURL(service)))
<div>{connector.getMBeanServerConnection.getAttribute(new ObjectName(component), attribute).toString}</div>
} catch {
case e: Exception =>
if (connectors.contains(service)) connectors.remove(service)
throw e
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment