hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ey...@apache.org
Subject svn commit: r1179287 - in /hadoop/common/trunk/hadoop-common-project/hadoop-common: CHANGES.txt src/main/java/org/apache/hadoop/jmx/JMXJsonServlet.java
Date Wed, 05 Oct 2011 16:09:28 GMT
Author: eyang
Date: Wed Oct  5 16:09:27 2011
New Revision: 1179287

URL: http://svn.apache.org/viewvc?rev=1179287&view=rev
Log:
HADOOP-7704. Reduce number of object created by JMXJsonServlet.
(Devaraj K via Eric Yang)

Modified:
    hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt
    hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/jmx/JMXJsonServlet.java

Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt?rev=1179287&r1=1179286&r2=1179287&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/CHANGES.txt Wed Oct  5 16:09:27
2011
@@ -48,6 +48,9 @@ Trunk (unreleased changes)
     HADOOP-7703. Improved excpetion handling of shutting down web server.
     (Devaraj K via Eric Yang)
 
+    HADOOP-7704. Reduce number of object created by JMXJsonServlet.
+    (Devaraj K via Eric Yang)
+
 Release 0.23.0 - Unreleased
 
   INCOMPATIBLE CHANGES

Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/jmx/JMXJsonServlet.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/jmx/JMXJsonServlet.java?rev=1179287&r1=1179286&r2=1179287&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/jmx/JMXJsonServlet.java
(original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/jmx/JMXJsonServlet.java
Wed Oct  5 16:09:27 2011
@@ -117,13 +117,15 @@ public class JMXJsonServlet extends Http
 
   private static final long serialVersionUID = 1L;
 
-  // ----------------------------------------------------- Instance Variables
   /**
    * MBean server.
    */
-  protected transient MBeanServer mBeanServer = null;
+  protected transient MBeanServer mBeanServer;
 
-  // --------------------------------------------------------- Public Methods
+  /**
+   * Json Factory to create Json generators for write objects in json format
+   */
+  protected transient JsonFactory jsonFactory;
   /**
    * Initialize this servlet.
    */
@@ -131,6 +133,7 @@ public class JMXJsonServlet extends Http
   public void init() throws ServletException {
     // Retrieve the MBean server
     mBeanServer = ManagementFactory.getPlatformMBeanServer();
+    jsonFactory = new JsonFactory();
   }
 
   /**
@@ -149,53 +152,45 @@ public class JMXJsonServlet extends Http
           response)) {
         return;
       }
+      JsonGenerator jg = null;
+      try {
+        response.setContentType("application/json; charset=utf8");
 
-      response.setContentType("application/json; charset=utf8");
-
-      PrintWriter writer = response.getWriter();
+        PrintWriter writer = response.getWriter();
+        jg = jsonFactory.createJsonGenerator(writer);
+        jg.useDefaultPrettyPrinter();
+        jg.writeStartObject();
 
-      JsonFactory jsonFactory = new JsonFactory();
-      JsonGenerator jg = jsonFactory.createJsonGenerator(writer);
-      jg.useDefaultPrettyPrinter();
-      jg.writeStartObject();
-      if (mBeanServer == null) {
-        jg.writeStringField("result", "ERROR");
-        jg.writeStringField("message", "No MBeanServer could be found");
-        jg.close();
-        LOG.error("No MBeanServer could be found.");
-        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
-        return;
-      }
-      
-      // query per mbean attribute
-      String getmethod = request.getParameter("get");
-      if (getmethod != null) {
-        String[] splitStrings = getmethod.split("\\:\\:");
-        if (splitStrings.length != 2) {
-          jg.writeStringField("result", "ERROR");
-          jg.writeStringField("message", "query format is not as expected.");
-          jg.close();
-          response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+        // query per mbean attribute
+        String getmethod = request.getParameter("get");
+        if (getmethod != null) {
+          String[] splitStrings = getmethod.split("\\:\\:");
+          if (splitStrings.length != 2) {
+            jg.writeStringField("result", "ERROR");
+            jg.writeStringField("message", "query format is not as expected.");
+            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+            return;
+          }
+          listBeans(jg, new ObjectName(splitStrings[0]), splitStrings[1],
+              response);
           return;
         }
-        listBeans(jg, new ObjectName(splitStrings[0]), splitStrings[1],
-            response);
-        jg.close();
-        return;
-      }
 
-      // query per mbean
-      String qry = request.getParameter("qry");
-      if (qry == null) {
-        qry = "*:*";
+        // query per mbean
+        String qry = request.getParameter("qry");
+        if (qry == null) {
+          qry = "*:*";
+        }
+        listBeans(jg, new ObjectName(qry), null, response);
+      } finally {
+        if (jg != null) {
+          jg.close();
+        }
       }
-      listBeans(jg, new ObjectName(qry), null, response);
-      jg.close();
-
-    } catch ( IOException e ) {
+    } catch (IOException e) {
       LOG.error("Caught an exception while processing JMX request", e);
       response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
-    } catch ( MalformedObjectNameException e ) {
+    } catch (MalformedObjectNameException e) {
       LOG.error("Caught an exception while processing JMX request", e);
       response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
     }



Mime
View raw message