Interface MatcherWatchdog

All Known Implementing Classes:
MatcherWatchdog.Default, MatcherWatchdog.Noop

public interface MatcherWatchdog
Protects against long running operations that happen between the register and unregister invocations. Threads that invoke register(Matcher), but take too long to invoke the unregister(Matcher) method will be interrupted. This is needed for Joni's Matcher.search(int, int, int) method, because it can end up spinning endlessly if the regular expression is too complex. Joni has checks that for every 30k iterations it checks if the current thread is interrupted and if so returns Matcher.INTERRUPTED.
  • Method Details

    • register

      void register(org.joni.Matcher matcher)
      Registers the current matcher and interrupts the this matcher if the takes too long for this thread to invoke unregister(Matcher).
      Parameters:
      matcher - The matcher to register
    • maxExecutionTimeInMillis

      long maxExecutionTimeInMillis()
      Returns:
      The maximum allowed time in milliseconds for a thread to invoke unregister(Matcher) after register(Matcher) has been invoked before this ThreadWatchDog starts to interrupting that thread.
    • unregister

      void unregister(org.joni.Matcher matcher)
      Unregisters the current matcher and prevents it from being interrupted.
      Parameters:
      matcher - The matcher to unregister
    • newInstance

      static MatcherWatchdog newInstance(long interval, long maxExecutionTime, LongSupplier relativeTimeSupplier, BiConsumer<Long,Runnable> scheduler)
      Returns an implementation that checks for each fixed interval if there are threads that have invoked register(Matcher) and not unregister(Matcher) and have been in this state for longer than the specified max execution interval and then interrupts these threads.
      Parameters:
      interval - The fixed interval to check if there are threads to interrupt
      maxExecutionTime - The time a thread has the execute an operation.
      relativeTimeSupplier - A supplier that returns relative time
      scheduler - A scheduler that is able to execute a command for each fixed interval
    • noop

      static MatcherWatchdog noop()
      Returns:
      A noop implementation that does not interrupt threads and is useful for testing and pre-defined grok expressions.