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.processor;
8   
9   import org.mule.api.MuleRuntimeException;
10  import org.mule.api.processor.MessageProcessor;
11  import org.mule.config.i18n.CoreMessages;
12  
13  import javax.resource.spi.work.WorkEvent;
14  import javax.resource.spi.work.WorkListener;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  public class AsyncWorkListener implements WorkListener
20  {
21      protected Log logger = LogFactory.getLog(getClass());
22      protected MessageProcessor target;
23  
24      public AsyncWorkListener(MessageProcessor target)
25      {
26          this.target = target;
27      }
28  
29      public void workAccepted(WorkEvent event)
30      {
31          this.handleWorkException(event, "workAccepted");
32      }
33  
34      public void workRejected(WorkEvent event)
35      {
36          this.handleWorkException(event, "workRejected");
37      }
38  
39      public void workStarted(WorkEvent event)
40      {
41          this.handleWorkException(event, "workStarted");
42      }
43  
44      public void workCompleted(WorkEvent event)
45      {
46          this.handleWorkException(event, "workCompleted");
47      }
48  
49      protected void handleWorkException(WorkEvent event, String type)
50      {
51          if (event == null)
52          {
53              return;
54          }
55  
56          Throwable e = event.getException();
57  
58          if (e == null)
59          {
60              return;
61          }
62  
63          if (e.getCause() != null)
64          {
65              e = e.getCause();
66          }
67  
68          logger.error("Work caused exception on '" + type + "'. Work being executed was: "
69                       + event.getWork().toString());
70          throw new MuleRuntimeException(CoreMessages.errorInvokingMessageProcessorAsynchronously(target), e);
71      }
72  
73  }