View Javadoc

1   /*
2    * $Id: AsynchronousRetryTemplate.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 java.util.Map;
14  
15  import javax.resource.spi.work.WorkException;
16  
17  import org.mule.api.context.WorkManager;
18  import org.mule.api.retry.RetryCallback;
19  import org.mule.api.retry.RetryContext;
20  import org.mule.api.retry.RetryNotifier;
21  import org.mule.api.retry.RetryPolicy;
22  import org.mule.api.retry.RetryPolicyTemplate;
23  import org.mule.retry.RetryPolicyExhaustedException;
24  import org.mule.util.concurrent.Latch;
25  
26  /**
27   * This class is a wrapper for a {@link RetryPolicyTemplate} and will execute any retry work within a separate thread.
28   * An optional {@link Latch} can be passed into this template, in which case execution will only occur once the latch is
29   * released.
30   */
31  public class AsynchronousRetryTemplate implements RetryPolicyTemplate
32  {
33      private final RetryPolicyTemplate delegate;
34      private Latch startLatch;
35  
36      public AsynchronousRetryTemplate(RetryPolicyTemplate delegate)
37      {
38          this.delegate = delegate;
39      }
40  
41      public RetryContext execute(RetryCallback callback, WorkManager workManager) throws Exception
42      {
43          if (workManager == null)
44          {
45              throw new IllegalStateException(
46                  "Cannot schedule a work till the workManager is initialized. Probably the connector hasn't been initialized yet");
47          }
48  
49          RetryWorker worker = new RetryWorker(delegate, callback, workManager, startLatch);
50          FutureRetryContext context = worker.getRetryContext();
51  
52          try
53          {
54              workManager.scheduleWork(worker);
55          }
56          catch (WorkException e)
57          {
58              throw new RetryPolicyExhaustedException(e, null);
59          }
60          return context;
61      }
62  
63      public RetryPolicy createRetryInstance()
64      {
65          return delegate.createRetryInstance();
66      }
67  
68      public RetryNotifier getNotifier()
69      {
70          return delegate.getNotifier();
71      }
72  
73      public void setNotifier(RetryNotifier retryNotifier)
74      {
75          delegate.setNotifier(retryNotifier);
76      }
77  
78      public Map<Object, Object> getMetaInfo()
79      {
80          return delegate.getMetaInfo();
81      }
82  
83      public void setMetaInfo(Map<Object, Object> metaInfo)
84      {
85          delegate.setMetaInfo(metaInfo);
86      }
87  
88      public RetryPolicyTemplate getDelegate()
89      {
90          return delegate;
91      }
92  
93      public void setStartLatch(Latch latch)
94      {
95          this.startLatch = latch;
96      }
97  }