Coverage Report - org.mule.util.concurrent.WaitPolicy
 
Classes in this File Line Coverage Branch Coverage Complexity
WaitPolicy
0%
0/16
0%
0/6
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.util.concurrent;
 8  
 
 9  
 import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
 10  
 import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler;
 11  
 import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
 12  
 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
 13  
 
 14  
 /**
 15  
  * A handler for unexecutable tasks that waits until the task can be submitted for
 16  
  * execution or times out. Generously snipped from the jsr166 repository at: <a
 17  
  * href="http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ThreadPoolExecutor.java"></a>.
 18  
  */
 19  
 // @Immutable
 20  
 public class WaitPolicy implements RejectedExecutionHandler
 21  
 {
 22  
     private final long time;
 23  
     private final TimeUnit timeUnit;
 24  
 
 25  
     /**
 26  
      * Constructs a <tt>WaitPolicy</tt> which waits (almost) forever.
 27  
      */
 28  
     public WaitPolicy()
 29  
     {
 30  
         // effectively waits forever
 31  0
         this(Long.MAX_VALUE, TimeUnit.SECONDS);
 32  0
     }
 33  
 
 34  
     /**
 35  
      * Constructs a <tt>WaitPolicy</tt> with timeout. A negative <code>time</code>
 36  
      * value is interpreted as <code>Long.MAX_VALUE</code>.
 37  
      */
 38  
     public WaitPolicy(long time, TimeUnit timeUnit)
 39  
     {
 40  0
         super();
 41  0
         this.time = (time < 0 ? Long.MAX_VALUE : time);
 42  0
         this.timeUnit = timeUnit;
 43  0
     }
 44  
 
 45  
     @SuppressWarnings("boxing")
 46  
     public void rejectedExecution(Runnable r, ThreadPoolExecutor e)
 47  
     {
 48  
         try
 49  
         {
 50  0
             if (e.isShutdown())
 51  
             {
 52  0
                 throw new RejectedExecutionException("ThreadPoolExecutor is already shut down");
 53  
             }
 54  0
             else if (!e.getQueue().offer(r, time, timeUnit))
 55  
             {
 56  0
                 String message = String.format("ThreadPoolExecutor did not accept within %1d %2s", 
 57  
                     time, timeUnit);
 58  0
                 throw new RejectedExecutionException(message);
 59  
             }
 60  
         }
 61  0
         catch (InterruptedException ie)
 62  
         {
 63  0
             Thread.currentThread().interrupt();
 64  0
             throw new RejectedExecutionException(ie);
 65  0
         }
 66  0
     }
 67  
 
 68  
 }