Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.appbasics.security.audit;
 
 import java.util.List;
 
 
 
The class handles all audit items

Author(s):
Philip Helger
 
 public class AsynchronousAuditor extends AbstractAuditor
 {
   private static final Logger s_aLogger = LoggerFactory.getLogger (AsynchronousAuditor.class);
 
   private final ReadWriteLock m_aRWLock = new ReentrantReadWriteLock ();
   // Just to have custom named threads....
   private static final ThreadFactory s_aThreadFactory = new ExtendedDefaultThreadFactory ("AsyncAuditor");
   private final ExecutorService m_aSenderThreadPool;
 
   public AsynchronousAuditor (@Nonnull final ICurrentUserIDProvider aUserIDProvider,
                               @Nonnull final IThrowingRunnableWithParameter <List <IAuditItem>> aPerformer)
   {
     super (aUserIDProvider);
     ValueEnforcer.notNull (aPerformer"Performer");
 
      = new ConcurrentCollectorMultiple <IAuditItem> (aPerformer);
   }
 
   @Override
   protected void handleAuditItem (@Nonnull final IAuditItem aAuditItem)
   {
     ValueEnforcer.notNull (aAuditItem"AuditItem");
 
     .writeLock ().lock ();
     try
     {
       .queueObject (aAuditItem);
     }
     finally
     {
       .writeLock ().unlock ();
     }
   }
 
   public int getQueueLength ()
   {
     .readLock ().lock ();
     try
     {
       return .getQueueLength ();
     }
     finally
     {
       .readLock ().unlock ();
     }
   }

  
When using this auditor, it is important to call this stop method before shutdown. It avoids further queuing of objects and waits until all items are handled. This method blocks until all remaining objects are handled.

Returns:
com.helger.commons.state.EChange.CHANGED if the shutdown was performed, com.helger.commons.state.EChange.UNCHANGED if the auditor was already shut down.
  public EChange stop ()
  {
    .writeLock ().lock ();
    try
    {
      // Check if the thread pool is already shut down
      if (.isShutdown ())
        return .;
      // don't take any more actions
      // stop all specific queues
      final int nQueueLength = .getQueueLength ();
      if (nQueueLength > 0)
        .info ("Stopping auditor queues with " + nQueueLength + " items");
    }
    finally
    {
      .writeLock ().unlock ();
    }
    // Don't wait in a writeLock!
    try
    {
      {
        // wait until we're done
      }
    }
    catch (final InterruptedException ex)
    {
      .error ("Error stopping auditor queue"ex);
    }
    return .;
  }
New to GrepCode? Check out our FAQ X