View Javadoc

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