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 378aa7597 [ISSUE #3010] Method manually handles closing an 
auto-closeable resource [MetaHandler]  (#4755)
378aa7597 is described below

commit 378aa7597f037801b75699c45b946fcb27c14c46
Author: Oleksandr Krutko <[email protected]>
AuthorDate: Fri Jan 26 10:13:06 2024 +0200

    [ISSUE #3010] Method manually handles closing an auto-closeable resource 
[MetaHandler]  (#4755)
---
 .../runtime/admin/handler/MetaHandler.java         | 73 ++++++++++------------
 1 file changed, 34 insertions(+), 39 deletions(-)

diff --git 
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetaHandler.java
 
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetaHandler.java
index 7915857f2..d29dbd2d9 100644
--- 
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetaHandler.java
+++ 
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetaHandler.java
@@ -98,51 +98,46 @@ public class MetaHandler 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 {
-            List<GetRegistryResponse> getRegistryResponseList = new 
ArrayList<>();
-            List<EventMeshDataInfo> eventMeshDataInfos = 
eventMeshMetaStorage.findAllEventMeshInfo();
-            for (EventMeshDataInfo eventMeshDataInfo : eventMeshDataInfos) {
-                GetRegistryResponse getRegistryResponse = new 
GetRegistryResponse(
-                    eventMeshDataInfo.getEventMeshClusterName(),
-                    eventMeshDataInfo.getEventMeshName(),
-                    eventMeshDataInfo.getEndpoint(),
-                    eventMeshDataInfo.getLastUpdateTimestamp(),
-                    eventMeshDataInfo.getMetadata().toString());
-                getRegistryResponseList.add(getRegistryResponse);
-            }
-            
getRegistryResponseList.sort(Comparator.comparing(GetRegistryResponse::getEventMeshClusterName));
+        try (OutputStream out = httpExchange.getResponseBody()) {
+            try {
+                List<GetRegistryResponse> getRegistryResponseList = new 
ArrayList<>();
+                List<EventMeshDataInfo> eventMeshDataInfos = 
eventMeshMetaStorage.findAllEventMeshInfo();
+                for (EventMeshDataInfo eventMeshDataInfo : eventMeshDataInfos) 
{
+                    GetRegistryResponse getRegistryResponse = new 
GetRegistryResponse(
+                        eventMeshDataInfo.getEventMeshClusterName(),
+                        eventMeshDataInfo.getEventMeshName(),
+                        eventMeshDataInfo.getEndpoint(),
+                        eventMeshDataInfo.getLastUpdateTimestamp(),
+                        eventMeshDataInfo.getMetadata().toString());
+                    getRegistryResponseList.add(getRegistryResponse);
+                }
+                
getRegistryResponseList.sort(Comparator.comparing(GetRegistryResponse::getEventMeshClusterName));
 
-            String result = JsonUtils.toJSONString(getRegistryResponseList);
-            httpExchange.sendResponseHeaders(200, 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
-            out.write(result.getBytes(Constants.DEFAULT_CHARSET));
-        } catch (NullPointerException e) {
-            // registry not initialized, return empty list
-            String result = JsonUtils.toJSONString(new ArrayList<>());
-            httpExchange.sendResponseHeaders(200, 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
-            out.write(result.getBytes(Constants.DEFAULT_CHARSET));
-        } catch (Exception e) {
-            StringWriter writer = new StringWriter();
-            PrintWriter printWriter = new PrintWriter(writer);
-            e.printStackTrace(printWriter);
-            printWriter.flush();
-            String stackTrace = writer.toString();
+                String result = 
JsonUtils.toJSONString(getRegistryResponseList);
+                httpExchange.sendResponseHeaders(200, 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
+                out.write(result.getBytes(Constants.DEFAULT_CHARSET));
+            } catch (NullPointerException e) {
+                // registry not initialized, return empty list
+                String result = JsonUtils.toJSONString(new ArrayList<>());
+                httpExchange.sendResponseHeaders(200, 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
+                out.write(result.getBytes(Constants.DEFAULT_CHARSET));
+            } 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);
-            httpExchange.sendResponseHeaders(500, 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
-            out.write(result.getBytes(Constants.DEFAULT_CHARSET));
-        } finally {
-            if (out != null) {
-                try {
-                    out.close();
-                } catch (IOException e) {
-                    log.warn("out close failed...", e);
-                }
+                Error error = new Error(e.toString(), stackTrace);
+                String result = JsonUtils.toJSONString(error);
+                httpExchange.sendResponseHeaders(500, 
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
+                out.write(result.getBytes(Constants.DEFAULT_CHARSET));
             }
+        } catch (IOException e) {
+            log.warn("out close failed...", e);
         }
     }
 


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

Reply via email to