Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mule/util/AsyncMessageListener.java


1   /* 
2    * $Header: /cvsroot/mule/mule/src/java/org/mule/util/AsyncMessageListener.java,v 1.5 2003/10/20 21:44:38 rossmason Exp $
3    * $Revision: 1.5 $
4    * $Date: 2003/10/20 21:44:38 $
5    * ------------------------------------------------------------------------------------------------------
6    * 
7    * Copyright (c) Cubis Limited. All rights reserved.
8    * http://www.cubis.co.uk 
9    * 
10   * The software in this package is published under the terms of the BSD
11   * style license a copy of which has been included with this distribution in
12   * the LICENSE.txt file. 
13   *
14   */
15  package org.mule.util;
16  
17  import javax.jms.Message;
18  import javax.jms.MessageListener;
19  
20  /**
21   *  <p><code>AsyncMessageListener</code> is a MessageListener Facade or Proxy
22   *  which invokes its child MessageListener asynchronously on a seperate Thread
23   *
24   * @author <a href="mailto:ross.mason@cubis.co.uk">Ross Mason</a>
25   * @version $Revision: 1.5 $
26   */
27  
28  public class AsyncMessageListener implements MessageListener
29  {
30      /** the real MessageListener */
31      private MessageListener listener;
32      /** a ThreadPool on which tasks are invoked asynchronously */
33      private NamedThreadPool threadPool;
34      /** assigned to the Thread that executes the onMessage, which makes it easier to debug when looking at log files */
35      private String name = null;
36  
37      public AsyncMessageListener(NamedThreadPool threadPool, MessageListener listener)
38      {
39          this(threadPool, listener, "???");
40      }
41  
42      public AsyncMessageListener(NamedThreadPool threadPool, MessageListener listener, String name)
43      {
44          this.threadPool = threadPool;
45          this.listener = listener;
46          this.name = name;
47      }
48  
49      public void onMessage(final Message message)
50      {
51          // invoke a new Runnable task on a Thread pool
52          threadPool.invokeLater(new Thread(name)
53          {
54              public void run()
55              {
56                  try
57                  {
58                      listener.onMessage(message);
59                  }
60                  catch (Exception e)
61                  {
62                      // do a rollback?
63                      e.printStackTrace();
64                  }
65                  // do a commit?
66              }
67          });
68      }
69  }