View Javadoc

1   /*
2    * $Id: AbstractRedeliveryPolicy.java 22812 2011-09-01 06:57:42Z mike.schilling $
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  package org.mule.processor;
11  
12  import org.mule.api.MuleException;
13  import org.mule.api.construct.FlowConstruct;
14  import org.mule.api.construct.FlowConstructAware;
15  import org.mule.api.context.MuleContextAware;
16  import org.mule.api.lifecycle.Disposable;
17  import org.mule.api.lifecycle.Initialisable;
18  import org.mule.api.lifecycle.InitialisationException;
19  import org.mule.api.lifecycle.Lifecycle;
20  import org.mule.api.lifecycle.Startable;
21  import org.mule.api.lifecycle.Stoppable;
22  import org.mule.api.processor.MessageProcessor;
23  import org.mule.config.i18n.CoreMessages;
24  import org.mule.routing.MessageProcessorFilterPair;
25  
26  /**
27   * Implement a redelivery policy for Mule.  This is similar to JMS retry policies that will redeliver a message a maximum
28   * number of times.  If this maximum is exceeded, the message is sent to a dead letter queue,  Here, if the processing of the messages
29   * fails too often, the message is sent to the failedMessageProcessor MP, whence success is force to be returned, to allow
30   * the message to be considered "consumed".
31   */
32  public abstract class AbstractRedeliveryPolicy extends AbstractInterceptingMessageProcessor implements MessageProcessor, Lifecycle, MuleContextAware, FlowConstructAware
33  {
34  
35      protected FlowConstruct flowConstruct;
36      protected int maxRedeliveryCount;
37      protected MessageProcessor deadLetterQueue;
38  
39      @Override
40      public void setFlowConstruct(FlowConstruct flowConstruct)
41      {
42          this.flowConstruct = flowConstruct;
43          if (deadLetterQueue instanceof FlowConstructAware)
44          {
45              ((FlowConstructAware) deadLetterQueue).setFlowConstruct(flowConstruct);
46          }
47      }
48  
49      @Override
50      public void initialise() throws InitialisationException
51      {
52          if (maxRedeliveryCount < 1)
53          {
54              throw new InitialisationException(
55                  CoreMessages.initialisationFailure(
56                      "maxRedeliveryCount must be positive"), this);
57          }
58  
59          if (deadLetterQueue instanceof Initialisable)
60          {
61              ((Initialisable) deadLetterQueue).initialise();
62          }
63      }
64  
65      @Override
66      public void start() throws MuleException
67      {
68          if (deadLetterQueue instanceof Startable)
69          {
70              ((Startable) deadLetterQueue).start();
71          }
72      }
73  
74      @Override
75      public void stop() throws MuleException
76      {
77          if (deadLetterQueue instanceof Stoppable)
78          {
79              ((Stoppable) deadLetterQueue).stop();
80          }
81      }
82  
83      @Override
84      public void dispose()
85      {
86          if (deadLetterQueue instanceof Disposable)
87          {
88              ((Disposable) deadLetterQueue).dispose();
89          }
90      }
91  
92      public int getMaxRedeliveryCount()
93      {
94          return maxRedeliveryCount;
95      }
96  
97      public void setMaxRedeliveryCount(int maxRedeliveryCount)
98      {
99          this.maxRedeliveryCount = maxRedeliveryCount;
100     }
101 
102     public MessageProcessor getTheFailedMessageProcessor()
103     {
104         return deadLetterQueue;
105     }
106 
107     public void setDeadLetterQueue(MessageProcessorFilterPair failedMessageProcessorPair)
108     {
109         this.deadLetterQueue = failedMessageProcessorPair.getMessageProcessor();
110     }
111 }