001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.spi;
018
019import java.util.function.Predicate;
020
021import org.apache.camel.AsyncCallback;
022import org.apache.camel.Exchange;
023import org.apache.camel.Message;
024import org.apache.camel.Processor;
025import org.apache.camel.Route;
026import org.apache.camel.Service;
027
028/**
029 * An object representing the unit of work processing an {@link Exchange}
030 * which allows the use of {@link Synchronization} hooks. This object might map one-to-one with
031 * a transaction in JPA or Spring; or might not.
032 */
033public interface UnitOfWork extends Service {
034
035    String MDC_BREADCRUMB_ID = "camel.breadcrumbId";
036    String MDC_EXCHANGE_ID = "camel.exchangeId";
037    String MDC_MESSAGE_ID = "camel.messageId";
038    String MDC_CORRELATION_ID = "camel.correlationId";
039    String MDC_ROUTE_ID = "camel.routeId";
040    String MDC_CAMEL_CONTEXT_ID = "camel.contextId";
041    String MDC_TRANSACTION_KEY = "camel.transactionKey";
042
043    /**
044     * Adds a synchronization hook
045     *
046     * @param synchronization the hook
047     */
048    void addSynchronization(Synchronization synchronization);
049
050    /**
051     * Removes a synchronization hook
052     *
053     * @param synchronization the hook
054     */
055    void removeSynchronization(Synchronization synchronization);
056
057    /**
058     * Checks if the passed synchronization hook is already part of this unit of work.
059     *
060     * @param synchronization the hook
061     * @return <tt>true</tt>, if the passed synchronization is part of this unit of work, else <tt>false</tt>
062     */
063    boolean containsSynchronization(Synchronization synchronization);
064
065    /**
066     * Handover all the registered synchronizations to the target {@link org.apache.camel.Exchange}.
067     * <p/>
068     * This is used when a route turns into asynchronous and the {@link org.apache.camel.Exchange} that
069     * is continued and routed in the async thread should do the on completion callbacks instead of the
070     * original synchronous thread.
071     *
072     * @param target the target exchange
073     */
074    void handoverSynchronization(Exchange target);
075
076    /**
077     * Handover all the registered synchronizations to the target {@link org.apache.camel.Exchange}.
078     * <p/>
079     * This is used when a route turns into asynchronous and the {@link org.apache.camel.Exchange} that
080     * is continued and routed in the async thread should do the on completion callbacks instead of the
081     * original synchronous thread.
082     *
083     * @param target the target exchange
084     * @param filter optional filter to only handover if filter returns <tt>true</tt>
085     */
086    void handoverSynchronization(Exchange target, Predicate<Synchronization> filter);
087
088    /**
089     * Invoked when this unit of work has been completed, whether it has failed or completed
090     *
091     * @param exchange the current exchange
092     */
093    void done(Exchange exchange);
094
095    /**
096     * Invoked when this unit of work is about to be routed by the given route.
097     *
098     * @param exchange the current exchange
099     * @param route    the route
100     */
101    void beforeRoute(Exchange exchange, Route route);
102
103    /**
104     * Invoked when this unit of work is done being routed by the given route.
105     *
106     * @param exchange the current exchange
107     * @param route    the route
108     */
109    void afterRoute(Exchange exchange, Route route);
110
111    /**
112     * Returns the unique ID of this unit of work, lazily creating one if it does not yet have one
113     *
114     * @return the unique ID
115     */
116    String getId();
117
118    /**
119     * Gets the original IN {@link Message} this Unit of Work was started with.
120     * <p/>
121     * The original message is only returned if the option {@link org.apache.camel.RuntimeConfiguration#isAllowUseOriginalMessage()}
122     * is enabled. If its disabled an <tt>IllegalStateException</tt> is thrown.
123     *
124     * @return the original IN {@link Message}, or <tt>null</tt> if using original message is disabled.
125     */
126    Message getOriginalInMessage();
127
128    /**
129     * Are we transacted?
130     *
131     * @return <tt>true</tt> if transacted, <tt>false</tt> otherwise
132     */
133    boolean isTransacted();
134
135    /**
136     * Are we already transacted by the given transaction key?
137     *
138     * @param key the transaction key
139     * @return <tt>true</tt> if already, <tt>false</tt> otherwise
140     */
141    boolean isTransactedBy(Object key);
142
143    /**
144     * Mark this UnitOfWork as being transacted by the given transaction key.
145     * <p/>
146     * When the transaction is completed then invoke the {@link #endTransactedBy(Object)} method using the same key.
147     *
148     * @param key the transaction key
149     */
150    void beginTransactedBy(Object key);
151
152    /**
153     * Mark this UnitOfWork as not transacted anymore by the given transaction definition.
154     *
155     * @param key the transaction key
156     */
157    void endTransactedBy(Object key);
158
159    /**
160     * Gets the {@link RouteContext} that this {@link UnitOfWork} currently is being routed through.
161     * <p/>
162     * Notice that an {@link Exchange} can be routed through multiple routes and thus the
163     * {@link org.apache.camel.spi.RouteContext} can change over time.
164     *
165     * @return the route context
166     * @see #pushRouteContext(RouteContext)
167     * @see #popRouteContext()
168     */
169    RouteContext getRouteContext();
170
171    /**
172     * Pushes the {@link RouteContext} that this {@link UnitOfWork} currently is being routed through.
173     * <p/>
174     * Notice that an {@link Exchange} can be routed through multiple routes and thus the
175     * {@link org.apache.camel.spi.RouteContext} can change over time.
176     *
177     * @param routeContext the route context
178     */
179    void pushRouteContext(RouteContext routeContext);
180
181    /**
182     * When finished being routed under the current {@link org.apache.camel.spi.RouteContext}
183     * it should be removed.
184     *
185     * @return the route context or <tt>null</tt> if none existed
186     */
187    RouteContext popRouteContext();
188
189    /**
190     * Strategy for optional work to be execute before processing
191     * <p/>
192     * For example the {@link org.apache.camel.impl.MDCUnitOfWork} leverages this
193     * to ensure MDC is handled correctly during routing exchanges using the
194     * asynchronous routing engine.
195     *
196     * @param processor the processor to be executed
197     * @param exchange  the current exchange
198     * @param callback  the callback
199     * @return the callback to be used (can return a wrapped callback)
200     */
201    AsyncCallback beforeProcess(Processor processor, Exchange exchange, AsyncCallback callback);
202
203    /**
204     * Strategy for optional work to be executed after the processing
205     *
206     * @param processor the processor executed
207     * @param exchange  the current exchange
208     * @param callback  the callback used
209     * @param doneSync  whether the process was done synchronously or asynchronously
210     */
211    void afterProcess(Processor processor, Exchange exchange, AsyncCallback callback, boolean doneSync);
212
213    /**
214     * Create a child unit of work, which is associated to this unit of work as its parent.
215     * <p/>
216     * This is often used when EIPs need to support {@link SubUnitOfWork}s. For example a splitter,
217     * where the sub messages of the splitter all participate in the same sub unit of work.
218     * That sub unit of work then decides whether the Splitter (in general) is failed or a
219     * processed successfully.
220     *
221     * @param childExchange the child exchange
222     * @return the created child unit of work
223     * @see SubUnitOfWork
224     * @see SubUnitOfWorkCallback
225     */
226    UnitOfWork createChildUnitOfWork(Exchange childExchange);
227
228    /**
229     * Sets the parent unit of work.
230     *
231     * @param parentUnitOfWork the parent
232     */
233    void setParentUnitOfWork(UnitOfWork parentUnitOfWork);
234
235    /**
236     * Gets the {@link SubUnitOfWorkCallback} if this unit of work participates in a sub unit of work.
237     *
238     * @return the callback, or <tt>null</tt> if this unit of work is not part of a sub unit of work.
239     * @see #beginSubUnitOfWork(org.apache.camel.Exchange)
240     */
241    SubUnitOfWorkCallback getSubUnitOfWorkCallback();
242
243    /**
244     * Begins a {@link SubUnitOfWork}, where sub (child) unit of works participate in a parent unit of work.
245     * The {@link SubUnitOfWork} will callback to the parent unit of work using {@link SubUnitOfWorkCallback}s.
246     *
247     * @param exchange the exchange
248     */
249    void beginSubUnitOfWork(Exchange exchange);
250
251    /**
252     * Ends a {@link SubUnitOfWork}.
253     * <p/>
254     * The {@link #beginSubUnitOfWork(org.apache.camel.Exchange)} must have been invoked
255     * prior to this operation.
256     *
257     * @param exchange the exchange
258     */
259    void endSubUnitOfWork(Exchange exchange);
260
261}