View Javadoc

1   /*
2    * $Id: RetryWorker.java 22088 2011-06-03 10:07:47Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.retry.async;
12  
13  import javax.resource.spi.work.Work;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.mule.api.context.WorkManager;
18  import org.mule.api.retry.RetryCallback;
19  import org.mule.api.retry.RetryPolicyTemplate;
20  import org.mule.util.concurrent.Latch;
21  
22  /**
23   * A {@link javax.resource.spi.work.Work} implementation used when executing a {@link RetryPolicyTemplate} in a separate
24   * thread.
25   */
26  public class RetryWorker implements Work
27  {
28      protected transient final Log logger = LogFactory.getLog(RetryWorker.class);
29  
30      private final RetryCallback callback;
31      private final WorkManager workManager;
32      private Exception exception = null;
33      private final FutureRetryContext context = new FutureRetryContext();
34      private final RetryPolicyTemplate delegate;
35      private Latch startLatch;
36  
37      public RetryWorker(RetryPolicyTemplate delegate, RetryCallback callback, WorkManager workManager)
38      {
39          this(delegate, callback, workManager, null);
40      }
41  
42      public RetryWorker(RetryPolicyTemplate delegate,
43                         RetryCallback callback,
44                         WorkManager workManager,
45                         Latch startLatch)
46      {
47          this.callback = callback;
48          this.workManager = workManager;
49          this.delegate = delegate;
50          this.startLatch = startLatch;
51          if (this.startLatch == null)
52          {
53              this.startLatch = new Latch();
54              this.startLatch.countDown();
55          }
56      }
57  
58      public void release()
59      {
60  
61      }
62  
63      public void run()
64      {
65          try
66          {
67              startLatch.await();
68          }
69          catch (InterruptedException e)
70          {
71              logger.warn("Retry thread interupted for callback: " + callback.getWorkDescription());
72              return;
73          }
74          try
75          {
76              context.setDelegateContext(delegate.execute(callback, workManager));
77          }
78          catch (Exception e)
79          {
80              this.exception = e;
81              logger.fatal(e, e);
82  
83          }
84      }
85  
86      public Exception getException()
87      {
88          return exception;
89      }
90  
91      public FutureRetryContext getRetryContext()
92      {
93          return context;
94      }
95  }