View Javadoc

1   /*
2    * $Id: AbstractMuleEventWork.java 19191 2010-08-25 21:05:23Z tcarlson $
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.work;
12  
13  import org.mule.RequestContext;
14  import org.mule.api.MuleEvent;
15  
16  import javax.resource.spi.work.Work;
17  
18  /**
19   * Abstract implementation of Work to be used whenever Work needs to be scheduled
20   * that operates on a MuleEvent. The abstract implementation ensures that a copy of
21   * MuleEvent is used and that this copy is available in the RequestContext for this
22   * new thread. Implementations of AbstractMuleEventWork should be run/scheduled only
23   * once. NOTE: This approach does not attempt to resolve MULE-4409 so this work may
24   * need to be reverted to correctly fix MULE-4409 in future releases.
25   */
26  public abstract class AbstractMuleEventWork implements Work
27  {
28  
29      protected MuleEvent event;
30  
31      public AbstractMuleEventWork(MuleEvent event)
32      {
33          this.event = event;
34      }
35  
36      public void run()
37      {
38          // Create a new MuleEvent copy, set it in RequestContext and make it
39          // available via locally too.
40          event = RequestContext.setEvent(event);
41          doRun();
42      }
43  
44      protected abstract void doRun();
45  
46      public void release()
47      {
48          // no-op
49      }
50  
51      public MuleEvent getEvent()
52      {
53          return event;
54      }
55  }