Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 1.48 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * REST interface to Akka's JMX service.
  3.  * <p/>
  4.  * Here is an example that retreives the current number of Actors.
  5.  * <pre>
  6.  * http://localhost:9998/management
  7.  *   ?service=service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
  8.  *   &component=se.scalablesolutions.akka:type=Stats
  9.  *   &attribute=counter_NrOfActors
  10.  * </pre>
  11.  */
  12. @Path("/management")
  13. class RestfulJMX extends Actor with Logging {
  14.   private case class Request(service: String, component: String, attribute: String)
  15.   private val connectors = new ConcurrentHashMap[String, JMXConnector]
  16.  
  17.   @GET
  18.   @Produces(Array("text/html"))
  19.   def queryJMX(
  20.     @QueryParam("service") service: String,
  21.     @QueryParam("component") component: String,
  22.     @QueryParam("attribute") attribute: String) =
  23.     (this !! Request(service, component, attribute)).getOrElse(<error>Error in REST JMX management service</error>)
  24.  
  25.   override def receive: PartialFunction[Any, Unit] = {
  26.     case Request(service, component, attribute) => reply(retrieveAttribute(service, component, attribute))
  27.   }
  28.  
  29.   private def retrieveAttribute(service: String, component: String, attribute: String): scala.xml.Elem = {
  30.     try {
  31.       var connector = connectors.putIfAbsent(service, JMXConnectorFactory.connect(new JMXServiceURL(service)))
  32.       <div>{connector.getMBeanServerConnection.getAttribute(new ObjectName(component), attribute).toString}</div>
  33.     } catch {
  34.       case e: Exception =>
  35.         if (connectors.contains(service)) connectors.remove(service)
  36.         throw e
  37.     }
  38.   }
  39. }