Class SimpleTimeLimiter

  • All Implemented Interfaces:
    TimeLimiter

    @Beta
    @GwtIncompatible
    public final class SimpleTimeLimiter
    extends java.lang.Object
    implements TimeLimiter
    A TimeLimiter that runs method calls in the background using an ExecutorService. If the time limit expires for a given method call, the thread running the call will be interrupted.
    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.ExecutorService executor  
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleTimeLimiter()
      Constructs a TimeLimiter instance using a Executors.newCachedThreadPool() to execute proxied method calls.
      SimpleTimeLimiter​(java.util.concurrent.ExecutorService executor)
      Constructs a TimeLimiter instance using the given executor service to execute proxied method calls.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T callWithTimeout​(java.util.concurrent.Callable<T> callable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit, boolean amInterruptible)
      Invokes a specified Callable, timing out after the specified time limit.
      private static boolean declaresInterruptedEx​(java.lang.reflect.Method method)  
      private static java.util.Set<java.lang.reflect.Method> findInterruptibleMethods​(java.lang.Class<?> interfaceType)  
      private static <T> T newProxy​(java.lang.Class<T> interfaceType, java.lang.reflect.InvocationHandler handler)  
      <T> T newProxy​(T target, java.lang.Class<T> interfaceType, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)
      Returns an instance of interfaceType that delegates all method calls to the target object, enforcing the specified time limit on each call.
      private static java.lang.Exception throwCause​(java.lang.Exception e, boolean combineStackTraces)  
      • Methods inherited from class java.lang.Object

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

      • executor

        private final java.util.concurrent.ExecutorService executor
    • Constructor Detail

      • SimpleTimeLimiter

        public SimpleTimeLimiter​(java.util.concurrent.ExecutorService executor)
        Constructs a TimeLimiter instance using the given executor service to execute proxied method calls.

        Warning: using a bounded executor may be counterproductive! If the thread pool fills up, any time callers spend waiting for a thread may count toward their time limit, and in this case the call may even time out before the target method is ever invoked.

        Parameters:
        executor - the ExecutorService that will execute the method calls on the target objects; for example, a Executors.newCachedThreadPool().
      • SimpleTimeLimiter

        public SimpleTimeLimiter()
        Constructs a TimeLimiter instance using a Executors.newCachedThreadPool() to execute proxied method calls.

        Warning: using a bounded executor may be counterproductive! If the thread pool fills up, any time callers spend waiting for a thread may count toward their time limit, and in this case the call may even time out before the target method is ever invoked.

    • Method Detail

      • newProxy

        public <T> T newProxy​(T target,
                              java.lang.Class<T> interfaceType,
                              long timeoutDuration,
                              java.util.concurrent.TimeUnit timeoutUnit)
        Description copied from interface: TimeLimiter
        Returns an instance of interfaceType that delegates all method calls to the target object, enforcing the specified time limit on each call. This time-limited delegation is also performed for calls to Object.equals(java.lang.Object), Object.hashCode(), and Object.toString().

        If the target method call finishes before the limit is reached, the return value or exception is propagated to the caller exactly as-is. If, on the other hand, the time limit is reached, the proxy will attempt to abort the call to the target, and will throw an UncheckedTimeoutException to the caller.

        It is important to note that the primary purpose of the proxy object is to return control to the caller when the timeout elapses; aborting the target method call is of secondary concern. The particular nature and strength of the guarantees made by the proxy is implementation-dependent. However, it is important that each of the methods on the target object behaves appropriately when its thread is interrupted.

        Specified by:
        newProxy in interface TimeLimiter
        Parameters:
        target - the object to proxy
        interfaceType - the interface you wish the returned proxy to implement
        timeoutDuration - with timeoutUnit, the maximum length of time that callers are willing to wait on each method call to the proxy
        timeoutUnit - with timeoutDuration, the maximum length of time that callers are willing to wait on each method call to the proxy
        Returns:
        a time-limiting proxy
      • callWithTimeout

        public <T> T callWithTimeout​(java.util.concurrent.Callable<T> callable,
                                     long timeoutDuration,
                                     java.util.concurrent.TimeUnit timeoutUnit,
                                     boolean amInterruptible)
                              throws java.lang.Exception
        Description copied from interface: TimeLimiter
        Invokes a specified Callable, timing out after the specified time limit. If the target method call finished before the limit is reached, the return value or exception is propagated to the caller exactly as-is. If, on the other hand, the time limit is reached, we attempt to abort the call to the target, and throw an UncheckedTimeoutException to the caller.
        Specified by:
        callWithTimeout in interface TimeLimiter
        Parameters:
        callable - the Callable to execute
        timeoutDuration - with timeoutUnit, the maximum length of time to wait
        timeoutUnit - with timeoutDuration, the maximum length of time to wait
        amInterruptible - whether to respond to thread interruption by aborting the operation and throwing InterruptedException; if false, the operation is allowed to complete or time out, and the current thread's interrupt status is re-asserted.
        Returns:
        the result returned by the Callable
        Throws:
        java.lang.InterruptedException - if interruptible is true and our thread is interrupted during execution
        UncheckedTimeoutException - if the time limit is reached
        java.lang.Exception
      • throwCause

        private static java.lang.Exception throwCause​(java.lang.Exception e,
                                                      boolean combineStackTraces)
                                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • findInterruptibleMethods

        private static java.util.Set<java.lang.reflect.Method> findInterruptibleMethods​(java.lang.Class<?> interfaceType)
      • declaresInterruptedEx

        private static boolean declaresInterruptedEx​(java.lang.reflect.Method method)
      • newProxy

        private static <T> T newProxy​(java.lang.Class<T> interfaceType,
                                      java.lang.reflect.InvocationHandler handler)