- 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
.-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic class
static class
-
Method Summary
Modifier and TypeMethodDescriptionlong
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 invokedregister(Matcher)
and notunregister(Matcher)
and have been in this state for longer than the specified max execution interval and then interrupts these threads.static MatcherWatchdog
noop()
void
register
(org.joni.Matcher matcher) Registers the current matcher and interrupts the this matcher if the takes too long for this thread to invokeunregister(Matcher)
.void
unregister
(org.joni.Matcher matcher) Unregisters the current matcher and prevents it from being 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 invokeunregister(Matcher)
.- Parameters:
matcher
- The matcher to register
-
maxExecutionTimeInMillis
long maxExecutionTimeInMillis()- Returns:
- The maximum allowed time in milliseconds for a thread to invoke
unregister(Matcher)
afterregister(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 invokedregister(Matcher)
and notunregister(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 interruptmaxExecutionTime
- The time a thread has the execute an operation.relativeTimeSupplier
- A supplier that returns relative timescheduler
- A scheduler that is able to execute a command for each fixed interval
-
noop
- Returns:
- A noop implementation that does not interrupt threads and is useful for testing and pre-defined grok expressions.
-