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