Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  jDTAUS Core Utilities
   *  Copyright (c) 2005 Christian Schulte
   *
   *  Christian Schulte, Haldener Strasse 72, 58095 Hagen, Germany
   *  <cs@jdtaus.org> (+49 2331 3543887)
   *
   *  This library is free software; you can redistribute it and/or
   *  modify it under the terms of the GNU Lesser General Public
  *  License as published by the Free Software Foundation; either
  *  version 2.1 of the License, or any later version.
  *
  *  This library is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *  Lesser General Public License for more details.
  *
  *  You should have received a copy of the GNU Lesser General Public
  *  License along with this library; if not, write to the Free Software
  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 package org.jdtaus.core.monitor.util;
 
 import java.util.Date;
 import  org.jdtaus.core.container.ContainerFactory;
 import  org.jdtaus.core.container.ContextFactory;
 import  org.jdtaus.core.container.ContextInitializer;
 import  org.jdtaus.core.container.Implementation;
 import  org.jdtaus.core.container.ModelFactory;
 import  org.jdtaus.core.container.Properties;
 import  org.jdtaus.core.container.Property;
 import  org.jdtaus.core.container.PropertyException;
 import  org.jdtaus.core.logging.spi.Logger;
 import  org.jdtaus.core.monitor.TaskEvent;
 import  org.jdtaus.core.monitor.TaskListener;

TaskListener logging the duration of an operation performed by a Task.

Author(s):
Christian Schulte
Version:
$Id: TaskDurationLogger.java 5034 2008-07-04 11:04:09Z schulte2005 $
See also:
onTaskEvent(TaskEvent)
 
 public final class TaskDurationLogger implements TaskListener
 {
     //--Implementation----------------------------------------------------------
 
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausImplementation
     // This section is managed by jdtaus-container-mojo.
 
    
Meta-data describing the implementation.
 
     private static final Implementation META =
         ModelFactory.getModel().getModules().
         getImplementation(TaskDurationLogger.class.getName());
 // </editor-fold>//GEN-END:jdtausImplementation
 
     //----------------------------------------------------------Implementation--
     //--Constructors------------------------------------------------------------
 
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausConstructors
     // This section is managed by jdtaus-container-mojo.
 
    
Initializes the properties of the instance.

Parameters:
meta the property values to initialize the instance with.
Throws:
NullPointerException if meta is null.
 
     private void initializeProperties(final Properties meta)
     {
         Property p;
 
         if(meta == null)
         {
             throw new NullPointerException("meta");
         }
 
         p = meta.getProperty("loggingThresholdMillis");
         this. = ((java.lang.Longp.getValue()).longValue();
 
     }
 // </editor-fold>//GEN-END:jdtausConstructors
 
     //------------------------------------------------------------Constructors--
     //--Dependencies------------------------------------------------------------
 
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies
     // This section is managed by jdtaus-container-mojo.
 
    
Configured Logger implementation.
 
     private transient Logger dLogger;

    
Gets the configured Logger implementation.

Returns:
the configured Logger implementation.
    private Logger getLogger()
    {
        Logger ret = null;
        if(this. != null)
        {
            ret = this.;
        }
        else
        {
            ret = (Logger) ContainerFactory.getContainer().
                getDependency(TaskDurationLogger.class,
                "Logger");
            if(ModelFactory.getModel().getModules().
                getImplementation(TaskDurationLogger.class.getName()).
                getDependencies().getDependency("Logger").
                isBound())
            {
                this. = ret;
            }
        }
        if(ret instanceof ContextInitializer && !((ContextInitializer) ret).
            isInitialized(ContextFactory.getContext()))
        {
            ((ContextInitializer) ret).initialize(ContextFactory.getContext());
        }
        return ret;
    }
// </editor-fold>//GEN-END:jdtausDependencies
    //------------------------------------------------------------Dependencies--
    //--Properties--------------------------------------------------------------
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties
    // This section is managed by jdtaus-container-mojo.

    
Property loggingThresholdMillis.

Serial:
    private long pLoggingThresholdMillis;

    
Gets the value of property loggingThresholdMillis.

Returns:
the value of property loggingThresholdMillis.
    public long getLoggingThresholdMillis()
    {
        return this.;
    }
// </editor-fold>//GEN-END:jdtausProperties
    //--------------------------------------------------------------Properties--
    //--TaskListener------------------------------------------------------------

    

This method measures the time a task is running and logs information for tasks running longer than specified by configuration property loggingThresholdMillis (defaults to 60000).

Parameters:
event the event send by a Task.
    public void onTaskEventfinal TaskEvent event )
    {
        if ( event != null )
        {
            final long now = System.currentTimeMillis();
            final long start = event.getTask().getTimestamp();
            if ( TaskEvent.ENDED == event.getType() &&
                now - start > this.getLoggingThresholdMillis() )
            {
                this.getLogger().info(
                    TaskDurationLoggerBundle.getInstance().
                    getDurationInfoMessage( Locale.getDefault() ).
                    formatnew Object[] { event.getTask().getDescription().
                                           getText( Locale.getDefault() ),
                                           new Datestart ),
                                           new Datenow ),
                                           new Longnow - start )
                        } ) );
            }
        }
    }
    //------------------------------------------------------------TaskListener--
    //--TaskDurationLogger------------------------------------------------------

    
Creates a new TaskDurationLogger instance.
    public TaskDurationLogger()
    {
        super();
        this.initializeProperties.getProperties() );
        this.assertValidProperties();
    }

    
Creates a new TaskDurationLogger instance taking the number of milliseconds a task at least needs to run to trigger a message when finished.

Parameters:
loggingThresholdMillis the number of milliseconds a task at least needs to run to trigger a message when finished.
Throws:
PropertyException if loggingThresholdMillis is negative or zero.
    public TaskDurationLoggerfinal long loggingThresholdMillis )
    {
        super();
        this.initializeProperties.getProperties() );
        this. = loggingThresholdMillis;
        this.assertValidProperties();
    }

    
Checks configured properties.

Throws:
PropertyException for illegal property value.
    private void assertValidProperties()
    {
        if ( this.getLoggingThresholdMillis() < 0L )
        {
            throw new PropertyException(
                "loggingThresholdMillis",
                Long.toStringthis.getLoggingThresholdMillis() ) );
        }
    }
    //------------------------------------------------------TaskDurationLogger--
New to GrepCode? Check out our FAQ X