View Javadoc

1   /*
2    * $Id: AbstractReceiverResourceWorker.java 10733 2008-02-06 16:16:51Z 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  package org.mule.providers;
11  
12  import java.util.ArrayList;
13  import java.io.OutputStream;
14  
15  /**
16   * This is a Message receiver worker used by transports that do not have a way for the underlying transport
17   * to call back to the receiver when a message is available such as Jms. This worker provides a
18   * callback {@link #getNextMessage(Object)} where the receiver can read the next message from the underlying
19   * transport.
20   */
21  public abstract class AbstractReceiverResourceWorker extends AbstractReceiverWorker
22  {
23      protected Object resource;
24  
25      public AbstractReceiverResourceWorker(Object resource, AbstractMessageReceiver receiver)
26      {
27          this(resource, receiver, null);
28      }
29  
30      public AbstractReceiverResourceWorker(Object resource, AbstractMessageReceiver receiver, OutputStream out)
31      {
32          super(new ArrayList(), receiver, out);
33          this.resource = resource;
34      }
35  
36  
37      /**
38      * (non-Javadoc)
39      *
40      */
41      //@Override
42      public void doRun()
43      {
44          try
45          {
46              Object message = getNextMessage(resource);
47              while (message != null && !Thread.currentThread().isInterrupted())
48              {
49                  messages.add(message);
50                  super.doRun();
51                  if (!Thread.currentThread().isInterrupted())
52                  {
53                      message = getNextMessage(resource);
54                  }
55              }
56          }
57          catch (Exception e)
58          {
59              handleException(e);
60          }
61      }
62  
63      /**
64       * The method used to read the next message from the underlying transport.
65       * @param resource the resource to read from, this may be a socket, a directory or some higher level
66       * representation.
67       * @return the message read from the resource.  This can be raw data such as a byte[] or a UMOMessageAdapter.
68       * @throws Exception
69       */
70      protected abstract Object getNextMessage(Object resource) throws Exception;
71  }