{"id":2179,"date":"2012-08-01T21:15:06","date_gmt":"2012-08-01T21:15:06","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=2179"},"modified":"2012-08-01T21:15:43","modified_gmt":"2012-08-01T21:15:43","slug":"using-jmx-to-monitor-uima-running-in-a-servlet","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=2179","title":{"rendered":"Using JMX to monitor UIMA running in a servlet"},"content":{"rendered":"<p><strong>Overview<\/strong><\/p>\n<p>A quick howto for if you&#8217;re running UIMA in a servlet, and want to be able to monitor your AE performance using JMX<\/p>\n<p><strong>Background<\/strong><\/p>\n<p>I&#8217;ve <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=2052\">mentioned <strong>JMX<\/strong><\/a> before. Basically, a Java app can expose information and methods through <a href=\"http:\/\/en.wikipedia.org\/wiki\/Java_Management_Extensions\">a standard interface<\/a>. Tools like <a href=\"http:\/\/java.sun.com\/developer\/technicalArticles\/J2SE\/jconsole.html\">jconsole<\/a>, which come with Java, can then be used to monitor and administer the Java app. <\/p>\n<p><a href=\"http:\/\/uima.apache.org\/\"><strong>UIMA<\/strong> (Unstructured Information Management Architecture)<\/a> is an Apache project, providing a standards-based way to perform analytics on unstructured text. It <a href=\"http:\/\/uima.apache.org\/downloads\/releaseDocs\/2.2.2-incubating\/docs\/html\/overview_and_setup\/overview_and_setup.html#ugr.ovv.conceptual.analysis_basics\">hosts a pipeline of annotators<\/a>: individual components each performing a specific text analytics task. As a document moves down the pipeline UIMA runs each of the annotators on the document. Each annotator adds it&#8217;s own annotations for the things it looks for in the text. <\/p>\n<p><strong>UIMA and JMX<\/strong><\/p>\n<p>UIMA supports JMX. <a href=\"http:\/\/uima.apache.org\/downloads\/releaseDocs\/2.2.2-incubating\/docs\/html\/tutorials_and_users_guides\/tutorials_and_users_guides.html#ugr.tug.application.jmx\">UIMA registers an <code>MBean<\/code> for each annotator<\/a>, letting you see the performance info for each annotator. In a pipeline of several annotators, it lets you see (amongst other things) how much time your document is spending in each annotator. <\/p>\n<p><img decoding=\"async\" src=\"http:\/\/uima.apache.org\/downloads\/releaseDocs\/2.2.2-incubating\/docs\/html\/images\/tutorials_and_users_guides\/tug.application\/image006.jpg\" width=\"450\" alt=\"jconsole\"\/><\/p>\n<p>In a stand-alone UIMA application, you basically <a href=\"http:\/\/uima.apache.org\/downloads\/releaseDocs\/2.2.2-incubating\/docs\/html\/tutorials_and_users_guides\/tutorials_and_users_guides.html#ugr.tug.application.jmx\">get this for free<\/a>. Start the application with the standard Java <code>-D<\/code> property for enabling JMX:<\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em; font-size: 1em; overflow: auto;\">-Dcom.sun.management.jmxremote<\/pre>\n<p>It is ready to let jconsole connect to it. <\/p>\n<p><!--more--><strong>UIMA in a servlet<\/strong><\/p>\n<p>For a current project, I&#8217;m running a UIMA pipeline in a Java servlet on Tomcat. I need to run the server with the JMX property, but there is slightly more to it than that. <\/p>\n<p>Basically, you need to manually register the analysis engine pipeline with JMX. From there, UIMA will handle registering all of the annotators and other components in the pipeline. <\/p>\n<p>Because the servlet can be started and stopped separate from the server as a whole, the easiest way to do this is from a <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/servlet\/ServletContextListener.html\"><code>ServletContextListener<\/code><\/a>. <\/p>\n<p><strong>Step 1:<\/strong> Register the context listener <\/p>\n<p>Add something like this to your web.xml<\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em; font-size: 1em; overflow: auto;\">&lt;listener&gt;\r\n    &lt;listener-class&gt;com.dalelane.uima.myapp.PipelineJMXMonitor&lt;\/listener-class&gt;\r\n&lt;\/listener&gt;<\/pre>\n<p><strong>Step 2:<\/strong> Create the context listener<\/p>\n<p>If you get a handle to your UIMA <a href=\"http:\/\/uima.apache.org\/downloads\/releaseDocs\/2.3.0-incubating\/docs\/api\/org\/apache\/uima\/analysis_engine\/AnalysisEngine.html\">AnalysisEngine<\/a>, it has a method <a href=\"http:\/\/uima.apache.org\/downloads\/releaseDocs\/2.3.0-incubating\/docs\/api\/org\/apache\/uima\/analysis_engine\/AnalysisEngine.html#getManagementInterface()\"> <code>getManagementInterface()<\/code><\/a> for getting the JMX management interface. <\/p>\n<p>In your context listener&#8217;s <code>contextInitialised<\/code> method, you can register this interface with the MBean server. In the context listener&#8217;s <code>contextDestroyed<\/code> method, you need to unregister it. <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em; font-size: 1em; overflow: auto;\">package com.dalelane.uima.myapp;\r\n\r\nimport java.util.List;\r\n\r\nimport javax.management.MBeanServer;\r\nimport javax.management.MBeanServerFactory;\r\nimport javax.management.ObjectName;\r\nimport javax.servlet.ServletContextEvent;\r\nimport javax.servlet.ServletContextListener;\r\n\r\nimport com.dalelane.uima.MyPipelineService;\r\n\r\npublic class PipelineJMXMonitor implements ServletContextListener {\r\n\r\n  @Override\r\n  public void contextInitialized(ServletContextEvent sce) {\r\n    try {\r\n      getMBeanServer().registerMBean(\r\n          MyPipelineService.getInstance().analysisEngine.getManagementInterface(),\r\n          new ObjectName(\"Application:Name=MyUIMAApp,Type=Server\"));\r\n    }\r\n    catch (Exception e){\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n  \r\n  @Override\r\n  public void contextDestroyed(ServletContextEvent sce) {\r\n    try {\r\n      getMBeanServer().unregisterMBean(\r\n          new ObjectName(\"Application:Name=MyUIMAApp,Type=Server\"));\r\n    } \r\n    catch (Exception e) {\r\n      e.printStackTrace();\r\n    } \r\n  }\r\n\r\n  private MBeanServer getMBeanServer(){\r\n    List&lt;MBeanServer&gt; servers = MBeanServerFactory.findMBeanServer(null);\r\n    return servers.get(0);\r\n  }\r\n}<\/pre>\n<p><strong>Step 3:<\/strong> Start the server<\/p>\n<p>Start the server with the <code>-Dcom.sun.management.jmxremote<\/code> option, and it&#8217;ll be ready for using jconsole to monitor it. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview A quick howto for if you&#8217;re running UIMA in a servlet, and want to be able to monitor your AE performance using JMX Background I&#8217;ve mentioned JMX before. Basically, a Java app can expose information and methods through a standard interface. Tools like jconsole, which come with Java, can then be used to monitor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[219,517,534,535,533],"class_list":["post-2179","post","type-post","status-publish","format-standard","hentry","category-code","tag-java","tag-jmx","tag-servlet","tag-tomcat","tag-uima"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2179"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2179\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}