Optimize number of MsoLogger instances 47/31847/1
authorUsaraswat <saraswat.urwashi@amdocs.com>
Thu, 15 Feb 2018 15:51:41 +0000 (10:51 -0500)
committerUsaraswat <saraswat.urwashi@amdocs.com>
Thu, 15 Feb 2018 16:53:14 +0000 (11:53 -0500)
Each call to getMsoLogger creates a new instance of MsoLogger
which is a potential memory leak

Change-Id: Ib0dda3abbf45d8633b4c8a2d49aa9d5c87e12931
Issue-ID: LOG-174
Signed-off-by: Usaraswat <saraswat.urwashi@amdocs.com>
common/src/main/java/org/openecomp/mso/logger/MsoLogger.java

index 86aedc1..b8c4aed 100644 (file)
@@ -144,15 +144,16 @@ public class MsoLogger {
     // For internal logging of the initialization of MSO logs
     private static final Logger LOGGER      = Logger.getLogger(MsoLogger.class.getName());
 
-    private MsoLogger(MsoLogger.Catalog cat) {
-        this.logger = EELFManager.getInstance().getErrorLogger();
-        this.auditLogger = EELFManager.getInstance().getAuditLogger();
-        this.metricsLogger = EELFManager.getInstance().getMetricsLogger();
-        MsoLogger.initialization();
-        setDefaultLogCatalog(cat);
-    }
 
-    private static synchronized void initialization() {
+    // Since four adaptors are using the instance of  MsoLogger which will be referenced everywhere
+    // hence limiting the number of MsoLogger instances to five.
+    private static final MsoLogger generalMsoLogger = new MsoLogger(Catalog.GENERAL);
+    private static final MsoLogger apihLogger = new MsoLogger(Catalog.APIH);
+    private static final MsoLogger asdcLogger = new MsoLogger(Catalog.ASDC);
+    private static final MsoLogger raLogger = new MsoLogger(Catalog.RA);
+    private static final MsoLogger bpelLogger = new MsoLogger(Catalog.BPEL);
+
+    static {
         if (instanceUUID == null || ("").equals(instanceUUID)) {
             instanceUUID = getInstanceUUID();
         }
@@ -170,15 +171,40 @@ public class MsoLogger {
         }
     }
 
+    // Singleton instances of the EELFLogger of all types are referenced by MsoLogger
+    private MsoLogger(Catalog cat) {
+        this.logger = EELFManager.getInstance().getErrorLogger();
+        this.auditLogger = EELFManager.getInstance().getAuditLogger();
+        this.metricsLogger = EELFManager.getInstance().getMetricsLogger();
+        this.setDefaultLogCatalog(cat);
+    }
+
+
+
     /**
      * Get the MsoLogger based on the catalog
-     * 
+     * This method is fixed now to resolve the total number of objects that are getting created
+     * everytime this function gets called. Its supposed to have fixed number of instance per java process.
+     *
      * @param cat
      *            Catalog of the logger
      * @return the MsoLogger
      */
     public static synchronized MsoLogger getMsoLogger(MsoLogger.Catalog cat) {
-        return new MsoLogger(cat);
+        switch (cat) {
+            case GENERAL:
+                return generalMsoLogger;
+            case APIH:
+                return apihLogger;
+            case RA:
+                return raLogger;
+            case BPEL:
+                return bpelLogger;
+            case ASDC:
+                return asdcLogger;
+            default:
+                return generalMsoLogger;
+        }
     }
 
     /**