[jboss-svn-commits] JBL Code SVN: r33888 - labs/jbossrules/trunk/drools-core/src/main/java/org/drools/audit.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Jul 14 18:48:31 EDT 2010


Author: KrisVerlaenen
Date: 2010-07-14 18:48:30 -0400 (Wed, 14 Jul 2010)
New Revision: 33888

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/audit/WorkingMemoryFileLogger.java
Log:
 - making the file logger thread safe

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/audit/WorkingMemoryFileLogger.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/audit/WorkingMemoryFileLogger.java	2010-07-14 21:08:35 UTC (rev 33887)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/audit/WorkingMemoryFileLogger.java	2010-07-14 22:48:30 UTC (rev 33888)
@@ -118,14 +118,18 @@
         try {
             fileWriter = new FileWriter(this.fileName + (this.nbOfFile == 0 ? ".log" : this.nbOfFile + ".log"), true );
             final XStream xstream = new XStream();
-            for (LogEvent event : this.events) {
+            List<LogEvent> eventsToWrite = null; 
+            synchronized (this.events) {
+            	eventsToWrite = new ArrayList<LogEvent>(this.events);
+                clear();
+            }
+            for (LogEvent event : eventsToWrite) {
                 fileWriter.write(xstream.toXML(event) + "\n");
             }
             if (split) {
                 this.nbOfFile++;
                 initialized = false;
             }
-            clear();
             fileWriter.write("</object-stream>");
         } catch ( final FileNotFoundException exc ) {
             throw new RuntimeException( "Could not create the log file.  Please make sure that directory that the log file should be placed in does exist." );
@@ -153,7 +157,9 @@
      * Clears all the events in the log.
      */
     private void clear() {
-        this.events.clear();
+        synchronized (this.events) {
+        	this.events.clear();
+        }
     }
 
     /**
@@ -171,9 +177,11 @@
      * @see org.drools.audit.WorkingMemoryLogger
      */
     public void logEventCreated(final LogEvent logEvent) {
-        this.events.add( logEvent );
-        if ( this.events.size() > this.maxEventsInMemory ) {
-            writeToDisk();
+        synchronized (this.events) {
+        	this.events.add( logEvent );
+	        if ( this.events.size() > this.maxEventsInMemory ) {
+	            writeToDisk();
+	        }
         }
     }
     



More information about the jboss-svn-commits mailing list