{"id":14,"date":"2012-03-25T22:47:46","date_gmt":"2012-03-26T02:47:46","guid":{"rendered":"http:\/\/sourcedelica.com\/wordpress\/?p=14"},"modified":"2012-04-09T08:34:02","modified_gmt":"2012-04-09T12:34:02","slug":"using-spring-jmx-managedattribute-in-scala","status":"publish","type":"post","link":"https:\/\/sourcedelica.com\/blog\/2012\/03\/using-spring-jmx-managedattribute-in-scala\/","title":{"rendered":"Targeting annotations to Javabean accessors in Scala"},"content":{"rendered":"<p>On a recent project I had written a simple scheduling service. I wanted to expose some properties and methods to JMX so I could view the configuration at runtime and so I could perform some basic operations, like starting the service.<\/p>\n<p>Spring provides a convenient way of exposing your classes to JMX using annotations, eliminating a ton of boilerplate. My initial stab at decorating the service for JMX looked like:<\/p>\n<pre class=\"brush: scala\">@ManagedResource\r\n@Component(\"schedulerService\")\r\nclass SchedulerServiceImpl extends SchedulerService {\r\n\r\n  @Value(\"${scheduler.enabled:false}\")\r\n  @ManagedAttribute @BeanProperty\r\n  val enabled: Boolean = false\r\n\r\n  @ManagedAttribute @BeanProperty\r\n  var started: Boolean = _\r\n\r\n  @ManagedOperation\r\n  def start(): String = {\r\n    \/\/...\r\n  }\r\n  \/\/...\r\n}<\/pre>\n<p><code>@ManagedAttribute<\/code> has to be put on Javabean getters and setters so I added <code>@BeanProperty<\/code> to the properties I wanted to expose. Actually I only wanted to expose the getter, but initially I wasn&#8217;t worried about that part.<\/p>\n<p>I fired up jconsole and saw that the <code>start()<\/code> method was exposed but the <code>started<\/code> and <code>enabled<\/code> properties were not. The usual tool for debugging Scala-Java interop, javap, doesn&#8217;t display information on annotations, so I used <a href=\"http:\/\/www.ej-technologies.com\/products\/jclasslib\/overview.html\">jclasslib bytecode viewer<\/a>. There I saw that the annotation had actually been set on the private field that Scala generated, not the Javabean setter.<\/p>\n<p><div id=\"attachment_21\" style=\"width: 749px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/localhost\/wordpress\/wp-content\/uploads\/2012\/03\/Bytecode-viewer.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21\" class=\"size-full wp-image-21  \" title=\"Bytecode viewer\" src=\"http:\/\/localhost\/wordpress\/wp-content\/uploads\/2012\/03\/Bytecode-viewer.jpg\" alt=\"\" width=\"739\" height=\"319\" srcset=\"https:\/\/sourcedelica.com\/blog\/wp-content\/uploads\/2012\/03\/Bytecode-viewer.jpg 924w, https:\/\/sourcedelica.com\/blog\/wp-content\/uploads\/2012\/03\/Bytecode-viewer-300x129.jpg 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/a><p id=\"caption-attachment-21\" class=\"wp-caption-text\">Annotation set on &quot;started&quot; field<\/p><\/div><br \/>\n<br \/>\nAfter some asking around, Iulian Dragos told me about the meta-annotations in the <code><a href=\"http:\/\/www.scala-lang.org\/api\/current\/index.html#scala.annotation.target.package\">scala.annotation.target<\/a><\/code> package. These let you target annotations at specific accessors. <\/p>\n<p>This allows you to create Javabean accessor-targeted annotations like this:<\/p>\n<pre class=\"brush: scala\">  \r\n@(ManagedAttribute @beanGetter) @BeanProperty\r\nval enabled: Boolean = false\r\n<\/pre>\n<p>That is cool but pretty verbose.   Fortunately you can clean it up with a type alias:<\/p>\n<pre class=\"brush: scala\">object JmxConfig {\r\n  type ManagedGetter = ManagedAttribute @beanGetter\r\n  type ManagedSetter = ManagedAttribute @beanSetter\r\n}<\/pre>\n<p>So the end result can look like:<\/p>\n<pre class=\"brush: scala\">import JmxConfig._\r\n\r\n@ManagedResource\r\n@Component(\"schedulerService\")\r\nclass SchedulerServiceImpl extends SchedulerService {\r\n\r\n  @Value(\"${scheduler.enabled:false}\")\r\n  @ManagedGetter @BeanProperty\r\n  val enabled: Boolean = false\r\n\r\n  @ManagedGetter @BeanProperty\r\n  var started: Boolean = _\r\n\r\n  \/\/....\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>On a recent project I had written a simple scheduling service. I wanted to expose some properties and methods to JMX so I could view the configuration at runtime and so I could perform some basic operations, like starting the service. Spring provides a convenient way of exposing your classes to JMX using annotations, eliminating [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-scala","entry"],"_links":{"self":[{"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":28,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}