View Javadoc

1   /*
2    * $Id: AbstractPolicyTemplate.java 19191 2010-08-25 21:05:23Z tcarlson $
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.policies;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.context.WorkManager;
16  import org.mule.api.retry.RetryCallback;
17  import org.mule.api.retry.RetryContext;
18  import org.mule.api.retry.RetryNotifier;
19  import org.mule.api.retry.RetryPolicy;
20  import org.mule.api.retry.RetryPolicyTemplate;
21  import org.mule.retry.DefaultRetryContext;
22  import org.mule.retry.PolicyStatus;
23  import org.mule.retry.RetryPolicyExhaustedException;
24  import org.mule.retry.notifiers.ConnectNotifier;
25  
26  import java.io.InterruptedIOException;
27  import java.util.Map;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * Base class for RetryPolicyTemplate implementations.  Uses ConnectNotifier as RetryNotifier
34   * by default.
35   */
36  public abstract class AbstractPolicyTemplate implements RetryPolicyTemplate, MuleContextAware
37  {
38      protected RetryNotifier notifier = new ConnectNotifier();
39      
40      /** This data will be made available to the RetryPolicy via the RetryContext. */
41      private Map<Object, Object> metaInfo;
42  
43      private MuleContext muleContext;
44  
45      protected transient final Log logger = LogFactory.getLog(getClass());
46  
47      public void setMuleContext(MuleContext context)
48      {
49          this.muleContext = context;
50      }
51  
52      public RetryContext execute(RetryCallback callback, WorkManager workManager) throws Exception
53      {
54          PolicyStatus status = null;
55          RetryPolicy policy = createRetryInstance();
56          DefaultRetryContext context = new DefaultRetryContext(callback.getWorkDescription(), 
57              metaInfo);
58          context.setMuleContext(muleContext);
59  
60          try
61          {
62              Exception cause = null;
63              do
64              {
65                  try
66                  {
67                      callback.doWork(context);
68                      if (notifier != null)
69                      {
70                          notifier.onSuccess(context);
71                      }
72                      break;
73                  }
74                  catch (Exception e)
75                  {
76                      cause = e;
77                      if (logger.isDebugEnabled())
78                      {
79                          logger.debug(cause);
80                      }
81                      if (notifier != null)
82                      {
83                          notifier.onFailure(context, cause);
84                      }
85                      if (cause instanceof InterruptedException || cause instanceof InterruptedIOException)
86                      {
87                          logger.error("Process was interrupted (InterruptedException), ceasing process");
88                          break;
89                      }
90                      else
91                      {
92                          status = policy.applyPolicy(cause);
93                      }
94                  }
95              }
96              while (status.isOk());
97  
98              if (status == null || status.isOk())
99              {
100                 return context;
101             }
102             else
103             {
104                 context.setFailed(cause);
105                 throw new RetryPolicyExhaustedException(cause, callback.getWorkDescription());
106             }
107         }
108         finally
109         {
110             if (status != null && status.getThrowable() != null)
111             {
112                 if (logger.isDebugEnabled())
113                 {
114                     logger.debug(status.getThrowable());
115                 }
116             }
117         }
118     }
119     
120     public RetryNotifier getNotifier()
121     {
122         return notifier;
123     }
124 
125     public void setNotifier(RetryNotifier retryNotifier)
126     {
127         this.notifier = retryNotifier;
128     }
129 
130     public Map<Object, Object> getMetaInfo()
131     {
132         return metaInfo;
133     }
134 
135     public void setMetaInfo(Map<Object, Object> metaInfo)
136     {
137         this.metaInfo = metaInfo;
138     }
139 
140     // For Spring IoC only
141     public void setId(String id)
142     {
143         // ignore
144     }
145 }