View Javadoc

1   /*
2    * $Id: PollingReceiverWorker.java 19230 2010-08-26 21:02: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.MuleException;
15  
16  import javax.resource.spi.work.Work;
17  
18  public class PollingReceiverWorker implements Work
19  {
20      protected final AbstractPollingMessageReceiver receiver;
21      protected volatile boolean running = false;
22  
23      public PollingReceiverWorker(AbstractPollingMessageReceiver pollingMessageReceiver)
24      {
25          super();
26          receiver = pollingMessageReceiver;
27      }
28  
29      public AbstractPollingMessageReceiver getReceiver()
30      {
31          return receiver;
32      }
33      
34      public boolean isRunning()
35      {
36          return running;
37      }
38  
39      // the run() method will exit after each poll() since it will be invoked again
40      // by the scheduler
41      public void run()
42      {
43          // Make sure we start with a clean slate.
44          RequestContext.clear();
45          if (receiver.isStarted())
46          {
47              running = true;
48              try
49              {
50                  // make sure we are connected, wait if necessary
51                  receiver.connected.whenTrue(null);
52                  receiver.poll();
53              }
54              catch (InterruptedException e)
55              {
56                 // stop polling
57                  try
58                  {
59                      receiver.stop();
60                  }
61                  catch (MuleException e1)
62                  {
63                      receiver.getConnector().getMuleContext().getExceptionListener().handleException(e1);
64                  }
65              }
66              catch (Exception e)
67              {
68                  receiver.getConnector().getMuleContext().getExceptionListener().handleException(e);
69              }
70              finally
71              {
72                  running = false;
73              }
74          }
75      }
76  
77      public void release()
78      {
79          // nop
80      }
81  
82  }