View Javadoc

1   /*
2    * $Id: PollingReceiverWorker.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers;
12  
13  import javax.resource.spi.work.Work;
14  
15  public class PollingReceiverWorker implements Work
16  {
17      protected final AbstractPollingMessageReceiver receiver;
18      protected volatile boolean running = false;
19  
20      public PollingReceiverWorker(AbstractPollingMessageReceiver pollingMessageReceiver)
21      {
22          super();
23          receiver = pollingMessageReceiver;
24      }
25  
26      public AbstractPollingMessageReceiver getReceiver()
27      {
28          return receiver;
29      }
30      
31      public boolean isRunning()
32      {
33          return running;
34      }
35  
36      // the run() method will exit after each poll() since it will be invoked again
37      // by the scheduler
38      public void run()
39      {
40          if (!receiver.stopped.get())
41          {
42              try
43              {
44                  running = true;
45                  // make sure we are connected, wait if necessary
46                  receiver.connected.whenTrue(null);
47                  receiver.poll();
48              }
49              catch (InterruptedException e)
50              {
51                  // stop polling
52                  receiver.stop();
53              }
54              catch (Exception e)
55              {
56                  receiver.handleException(e);
57              }
58              finally
59              {
60                  running = false;
61              }
62          }
63      }
64  
65      public void release()
66      {
67          // nop
68      }
69  
70  }