Class DelayedWriter

  • All Implemented Interfaces:
    java.lang.Runnable

    public class DelayedWriter
    extends java.lang.Thread
    This class supports delayed and timely ascending ordered writing of blade events to a BufferedWriter object. The goal of this feature is to write event blades in the chronological order of their starting date, regardless of the time they are actually available for writing, provided the delay is sufficient. This is mostly useful for action events: depending on response times variations, one action event E2 may be available for writing before another action event E1, while the corresponding request has been sent after, because the response time was shorter. Thanks to delayed writing, and as long as the maximum variation of response times is shorter than the delay, action reports are written in chronological order with regard to the request start date. But, when the maximum variation of response times from one request to another becomes greater than the delay, there is no guaranty that this order will be respected. The delay value in seconds may be set through system property defined by DELAY_S_PROP. Otherwise, default delay is defined by DELAY_S_DEFAULT.
    Author:
    Bruno Dillenseger
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean closing
      set to true when the writer must be closed
      protected static long delay_ms
      minimum age of events before actually writing them
      static java.lang.String DELAY_S_DEFAULT  
      static java.lang.String DELAY_S_PROP  
      protected java.util.SortedSet<BladeEvent> eventQ  
      protected static long maxPending
      maximum number of young events waiting to be written (subsumes delay_ms)
      static java.lang.String MAXPENDING_DEFAULT  
      static java.lang.String MAXPENDING_PROP  
      protected long offset
      Initial time in ms
      protected java.io.BufferedWriter writer  
      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Constructor Description
      DelayedWriter​(java.io.BufferedWriter out, long dateOffset_ms)
      Creates a delayed writer that will order blade events according to their date, before actually writing them using the provided BufferedWriter object, after the delay has expired.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the underlying buffered writer, after flushing all pending blade events to it.
      void run()
      The delay writer's activity consists in getting events from the blade events queue when they are sufficiently old (with respect to delay and blade event dates), and writing them to the underlying buffered writer.
      void write​(BladeEvent event)
      Delayed writing of a blade event.
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • delay_ms

        protected static long delay_ms
        minimum age of events before actually writing them
      • maxPending

        protected static long maxPending
        maximum number of young events waiting to be written (subsumes delay_ms)
      • MAXPENDING_DEFAULT

        public static final java.lang.String MAXPENDING_DEFAULT
        See Also:
        Constant Field Values
      • eventQ

        protected java.util.SortedSet<BladeEvent> eventQ
      • offset

        protected long offset
        Initial time in ms
      • writer

        protected java.io.BufferedWriter writer
      • closing

        protected volatile boolean closing
        set to true when the writer must be closed
    • Constructor Detail

      • DelayedWriter

        public DelayedWriter​(java.io.BufferedWriter out,
                             long dateOffset_ms)
        Creates a delayed writer that will order blade events according to their date, before actually writing them using the provided BufferedWriter object, after the delay has expired.
        Parameters:
        out - the buffered writer to be used for finally writing blades event
        dateOffset_ms - the initial date in ms (this value will be subtracted from the events dates when they are finally written to the buffered writer).
    • Method Detail

      • write

        public void write​(BladeEvent event)
                   throws java.io.IOException
        Delayed writing of a blade event. The delay applies from the date of the event (not current date when calling this method).
        Parameters:
        event - the blade event to write
        Throws:
        java.io.IOException - the delayed writer has been closed and cannot be used anymore.
      • close

        public void close()
        Closes the underlying buffered writer, after flushing all pending blade events to it. Any further attempt to write a blade event to this delayed writer will be rejected.
      • run

        public void run()
        The delay writer's activity consists in getting events from the blade events queue when they are sufficiently old (with respect to delay and blade event dates), and writing them to the underlying buffered writer. Sleeps if no event is old enough. The sleep duration depends on the delay value and the oldest pending event's date if any. Sleep is interrupted and all pending events are flushed to the buffered writer when the delayed writer is closed.
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread