View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport;
8   
9   import org.mule.RequestContext;
10  import org.mule.api.MuleException;
11  
12  import javax.resource.spi.work.Work;
13  
14  public class PollingReceiverWorker implements Work
15  {
16      protected final AbstractPollingMessageReceiver receiver;
17      protected volatile boolean running = false;
18  
19      public PollingReceiverWorker(AbstractPollingMessageReceiver pollingMessageReceiver)
20      {
21          super();
22          receiver = pollingMessageReceiver;
23      }
24  
25      public AbstractPollingMessageReceiver getReceiver()
26      {
27          return receiver;
28      }
29      
30      public boolean isRunning()
31      {
32          return running;
33      }
34  
35      // the run() method will exit after each poll() since it will be invoked again
36      // by the scheduler
37      public void run()
38      {
39          // Make sure we start with a clean slate.
40          RequestContext.clear();
41          if (receiver.isStarted())
42          {
43              running = true;
44              try
45              {
46                  poll();
47              }
48              catch (InterruptedException e)
49              {
50                 // stop polling
51                  try
52                  {
53                      receiver.stop();
54                  }
55                  catch (MuleException e1)
56                  {
57                      receiver.getConnector().getMuleContext().getExceptionListener().handleException(e1);
58                  }
59              }
60              catch (Exception e)
61              {
62                  receiver.getConnector().getMuleContext().getExceptionListener().handleException(e);
63              }
64              finally
65              {
66                  running = false;
67              }
68          }
69      }
70  
71      protected void poll() throws Exception
72      {
73          receiver.poll();
74      }
75  
76      public void release()
77      {
78          // nop
79      }
80  
81  }