This is an automated email from the ASF dual-hosted git repository.

mxsm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new 16409850a [ISSUE #3009]change MetricsHandler#get to try-resources 
(#4800)
16409850a is described below

commit 16409850afb935feb8c6895788a348b87bb81a6c
Author: Qin Qianjiu <[email protected]>
AuthorDate: Thu Mar 21 21:59:08 2024 +0800

    [ISSUE #3009]change MetricsHandler#get to try-resources (#4800)
---
 .../runtime/admin/handler/MetricsHandler.java      | 129 ++++++++++-----------
 1 file changed, 62 insertions(+), 67 deletions(-)

diff --git 
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java
 
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java
index 8eef2691a..f1065c2fe 100755
--- 
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java
+++ 
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java
@@ -97,76 +97,71 @@ public class MetricsHandler extends AbstractHttpHandler {
      * @throws IOException if an I/O error occurs while handling the request
      */
     void get(HttpExchange httpExchange) throws IOException {
-        OutputStream out = httpExchange.getResponseBody();
         httpExchange.getResponseHeaders().add(EventMeshConstants.CONTENT_TYPE, 
EventMeshConstants.APPLICATION_JSON);
         
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_ORIGIN, "*");
 
-        try {
-            GetMetricsResponse getMetricsResponse = new GetMetricsResponse(
-                httpSummaryMetrics.maxHTTPTPS(),
-                httpSummaryMetrics.avgHTTPTPS(),
-                httpSummaryMetrics.maxHTTPCost(),
-                httpSummaryMetrics.avgHTTPCost(),
-                httpSummaryMetrics.avgHTTPBodyDecodeCost(),
-                httpSummaryMetrics.getHttpDiscard(),
-                httpSummaryMetrics.maxSendBatchMsgTPS(),
-                httpSummaryMetrics.avgSendBatchMsgTPS(),
-                httpSummaryMetrics.getSendBatchMsgNumSum(),
-                httpSummaryMetrics.getSendBatchMsgFailNumSum(),
-                httpSummaryMetrics.getSendBatchMsgFailRate(),
-                httpSummaryMetrics.getSendBatchMsgDiscardNumSum(),
-                httpSummaryMetrics.maxSendMsgTPS(),
-                httpSummaryMetrics.avgSendMsgTPS(),
-                httpSummaryMetrics.getSendMsgNumSum(),
-                httpSummaryMetrics.getSendMsgFailNumSum(),
-                httpSummaryMetrics.getSendMsgFailRate(),
-                httpSummaryMetrics.getReplyMsgNumSum(),
-                httpSummaryMetrics.getReplyMsgFailNumSum(),
-                httpSummaryMetrics.maxPushMsgTPS(),
-                httpSummaryMetrics.avgPushMsgTPS(),
-                httpSummaryMetrics.getHttpPushMsgNumSum(),
-                httpSummaryMetrics.getHttpPushFailNumSum(),
-                httpSummaryMetrics.getHttpPushMsgFailRate(),
-                httpSummaryMetrics.maxHTTPPushLatency(),
-                httpSummaryMetrics.avgHTTPPushLatency(),
-                httpSummaryMetrics.getBatchMsgQueueSize(),
-                httpSummaryMetrics.getSendMsgQueueSize(),
-                httpSummaryMetrics.getPushMsgQueueSize(),
-                httpSummaryMetrics.getHttpRetryQueueSize(),
-                httpSummaryMetrics.avgBatchSendMsgCost(),
-                httpSummaryMetrics.avgSendMsgCost(),
-                httpSummaryMetrics.avgReplyMsgCost(),
-
-                tcpSummaryMetrics.getRetrySize(),
-                tcpSummaryMetrics.getClient2eventMeshTPS(),
-                tcpSummaryMetrics.getEventMesh2mqTPS(),
-                tcpSummaryMetrics.getMq2eventMeshTPS(),
-                tcpSummaryMetrics.getEventMesh2clientTPS(),
-                tcpSummaryMetrics.getAllTPS(),
-                tcpSummaryMetrics.getAllConnections(),
-                tcpSummaryMetrics.getSubTopicNum());
-            String result = JsonUtils.toJSONString(getMetricsResponse);
-            byte[] bytes = 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET);
-            httpExchange.sendResponseHeaders(200, bytes.length);
-            out.write(bytes);
-        } catch (Exception e) {
-            StringWriter writer = new StringWriter();
-            PrintWriter printWriter = new PrintWriter(writer);
-            e.printStackTrace(printWriter);
-            printWriter.flush();
-            String stackTrace = writer.toString();
-
-            Error error = new Error(e.toString(), stackTrace);
-            String result = JsonUtils.toJSONString(error);
-            byte[] bytes = 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET);
-            httpExchange.sendResponseHeaders(500, bytes.length);
-            out.write(bytes);
-        } finally {
-            if (out != null) {
-                try {
-                    out.close();
-                } catch (IOException e) {
-                    log.warn("out close failed...", e);
+        try (OutputStream out = httpExchange.getResponseBody()) {
+            try {
+                GetMetricsResponse getMetricsResponse = new GetMetricsResponse(
+                    httpSummaryMetrics.maxHTTPTPS(),
+                    httpSummaryMetrics.avgHTTPTPS(),
+                    httpSummaryMetrics.maxHTTPCost(),
+                    httpSummaryMetrics.avgHTTPCost(),
+                    httpSummaryMetrics.avgHTTPBodyDecodeCost(),
+                    httpSummaryMetrics.getHttpDiscard(),
+                    httpSummaryMetrics.maxSendBatchMsgTPS(),
+                    httpSummaryMetrics.avgSendBatchMsgTPS(),
+                    httpSummaryMetrics.getSendBatchMsgNumSum(),
+                    httpSummaryMetrics.getSendBatchMsgFailNumSum(),
+                    httpSummaryMetrics.getSendBatchMsgFailRate(),
+                    httpSummaryMetrics.getSendBatchMsgDiscardNumSum(),
+                    httpSummaryMetrics.maxSendMsgTPS(),
+                    httpSummaryMetrics.avgSendMsgTPS(),
+                    httpSummaryMetrics.getSendMsgNumSum(),
+                    httpSummaryMetrics.getSendMsgFailNumSum(),
+                    httpSummaryMetrics.getSendMsgFailRate(),
+                    httpSummaryMetrics.getReplyMsgNumSum(),
+                    httpSummaryMetrics.getReplyMsgFailNumSum(),
+                    httpSummaryMetrics.maxPushMsgTPS(),
+                    httpSummaryMetrics.avgPushMsgTPS(),
+                    httpSummaryMetrics.getHttpPushMsgNumSum(),
+                    httpSummaryMetrics.getHttpPushFailNumSum(),
+                    httpSummaryMetrics.getHttpPushMsgFailRate(),
+                    httpSummaryMetrics.maxHTTPPushLatency(),
+                    httpSummaryMetrics.avgHTTPPushLatency(),
+                    httpSummaryMetrics.getBatchMsgQueueSize(),
+                    httpSummaryMetrics.getSendMsgQueueSize(),
+                    httpSummaryMetrics.getPushMsgQueueSize(),
+                    httpSummaryMetrics.getHttpRetryQueueSize(),
+                    httpSummaryMetrics.avgBatchSendMsgCost(),
+                    httpSummaryMetrics.avgSendMsgCost(),
+                    httpSummaryMetrics.avgReplyMsgCost(),
+
+                    tcpSummaryMetrics.getRetrySize(),
+                    tcpSummaryMetrics.getClient2eventMeshTPS(),
+                    tcpSummaryMetrics.getEventMesh2mqTPS(),
+                    tcpSummaryMetrics.getMq2eventMeshTPS(),
+                    tcpSummaryMetrics.getEventMesh2clientTPS(),
+                    tcpSummaryMetrics.getAllTPS(),
+                    tcpSummaryMetrics.getAllConnections(),
+                    tcpSummaryMetrics.getSubTopicNum());
+                String result = JsonUtils.toJSONString(getMetricsResponse);
+                byte[] bytes = 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET);
+                httpExchange.sendResponseHeaders(200, bytes.length);
+                out.write(bytes);
+            } catch (Exception e) {
+                try (StringWriter writer = new StringWriter()) {
+                    try (PrintWriter printWriter = new PrintWriter(writer)) {
+                        e.printStackTrace(printWriter);
+                    }
+
+                    String stackTrace = writer.toString();
+
+                    Error error = new Error(e.toString(), stackTrace);
+                    String result = JsonUtils.toJSONString(error);
+                    byte[] bytes = 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET);
+                    httpExchange.sendResponseHeaders(500, bytes.length);
+                    out.write(bytes);
                 }
             }
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to