Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. jboner revised this gist Aug 11, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Actor providing JMX over REST (fault-tolerant)
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ class RestfulJMX extends Actor with Logging {
    case Request(service, component, attribute) => reply(retrieveAttribute(service, component, attribute))
    }

    private def retrieveAttribute(service: String, component: String, attribute: String): scala.xml.Elem = synchronized {
    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>
  2. jboner created this gist Aug 11, 2009.
    39 changes: 39 additions & 0 deletions Actor providing JMX over REST (fault-tolerant)
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    /**
    * 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 = synchronized {
    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
    }
    }
    }