View Javadoc

1   /*
2    * $Id: PollingReceiverWorker.java 22396 2011-07-12 21:26:04Z 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  
11  package org.mule.transport;
12  
13  import org.mule.RequestContext;
14  import org.mule.api.MessagingException;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  
18  import javax.resource.spi.work.Work;
19  
20  public class PollingReceiverWorker implements Work
21  {
22      protected final AbstractPollingMessageReceiver receiver;
23      protected volatile boolean running = false;
24  
25      public PollingReceiverWorker(AbstractPollingMessageReceiver pollingMessageReceiver)
26      {
27          super();
28          receiver = pollingMessageReceiver;
29      }
30  
31      public AbstractPollingMessageReceiver getReceiver()
32      {
33          return receiver;
34      }
35      
36      public boolean isRunning()
37      {
38          return running;
39      }
40  
41      // the run() method will exit after each poll() since it will be invoked again
42      // by the scheduler
43      @Override
44      public void run()
45      {
46          // Make sure we start with a clean slate.
47          RequestContext.clear();
48          if (receiver.isStarted())
49          {
50              running = true;
51              try
52              {
53                  poll();
54              }
55              catch (InterruptedException e)
56              {
57                  // stop polling
58                  try
59                  {
60                      receiver.stop();
61                  }
62                  catch (MuleException e1)
63                  {
64                      receiver.getConnector().getMuleContext().getExceptionListener().handleException(e1);
65                  }
66              }
67              catch (MessagingException e)
68              {
69                  MuleEvent event = (e).getEvent();
70                  event.getFlowConstruct().getExceptionListener().handleException(e, event);
71              }
72              catch (Exception e)
73              {
74                  receiver.getConnector().getMuleContext().getExceptionListener().handleException(e);
75              }
76              finally
77              {
78                  running = false;
79              }
80          }
81      }
82  
83      protected void poll() throws Exception
84      {
85          receiver.performPoll();
86      }
87  
88      @Override
89      public void release()
90      {
91          // nop
92      }
93  }