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.work;
8   
9   import org.mule.OptimizedRequestContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.ThreadSafeAccess;
12  
13  import javax.resource.spi.work.Work;
14  
15  /**
16   * Abstract implementation of Work to be used whenever Work needs to be scheduled
17   * that operates on a MuleEvent. The abstract implementation ensures that a copy of
18   * MuleEvent is used and that this copy is available in the RequestContext for this
19   * new thread. Implementations of AbstractMuleEventWork should be run/scheduled only
20   * once. NOTE: This approach does not attempt to resolve MULE-4409 so this work may
21   * need to be reverted to correctly fix MULE-4409 in future releases.
22   */
23  public abstract class AbstractMuleEventWork implements Work
24  {
25  
26      protected MuleEvent event;
27  
28      public AbstractMuleEventWork(MuleEvent event)
29      {
30          // Event must be copied here rather than once work is executed, so main flow can't mutate the message
31          // before work execution
32          this.event = (MuleEvent) ((ThreadSafeAccess) event).newThreadCopy();
33      }
34  
35      public final void run()
36      {
37          // Set event in RequestContext now we are in new thread (fresh copy already made in constructor)
38          OptimizedRequestContext.unsafeSetEvent(event);
39          doRun();
40      }
41  
42      protected abstract void doRun();
43  
44      public void release()
45      {
46          // no-op
47      }
48  
49      public MuleEvent getEvent()
50      {
51          return event;
52      }
53  }