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