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 */
017 package org.apache.camel.processor;
018
019 import org.apache.camel.AsyncCallback;
020 import org.apache.camel.AsyncProcessor;
021 import org.apache.camel.Exchange;
022 import org.apache.camel.Processor;
023 import org.apache.camel.spi.RouteContext;
024
025 /**
026 * A processor that processes the processor in a {@link org.apache.camel.spi.SubUnitOfWork} context.
027 * <p/>
028 * This processor ensures the {@link org.apache.camel.spi.UnitOfWork#beginSubUnitOfWork(org.apache.camel.Exchange)}
029 * and {@link org.apache.camel.spi.UnitOfWork#endSubUnitOfWork(org.apache.camel.Exchange)} is executed.
030 *
031 * @see org.apache.camel.spi.SubUnitOfWork
032 * @see org.apache.camel.spi.SubUnitOfWorkCallback
033 */
034 public class SubUnitOfWorkProcessor extends UnitOfWorkProcessor {
035
036 // See code comment in DefaultUnitOfWork for reasons why this implementation is named SubUnitOfWorkProcessor
037
038 public SubUnitOfWorkProcessor(Processor processor) {
039 super(processor);
040 }
041
042 public SubUnitOfWorkProcessor(AsyncProcessor processor) {
043 super(processor);
044 }
045
046 public SubUnitOfWorkProcessor(RouteContext routeContext, Processor processor) {
047 super(routeContext, processor);
048 }
049
050 public SubUnitOfWorkProcessor(RouteContext routeContext, AsyncProcessor processor) {
051 super(routeContext, processor);
052 }
053
054 @Override
055 public boolean process(final Exchange exchange, final AsyncCallback callback) {
056 // begin savepoint
057 exchange.getUnitOfWork().beginSubUnitOfWork(exchange);
058 // process the exchange
059 return super.process(exchange, new AsyncCallback() {
060 @Override
061 public void done(boolean doneSync) {
062 try {
063 // end sub unit of work
064 exchange.getUnitOfWork().endSubUnitOfWork(exchange);
065 } finally {
066 // must ensure callback is invoked
067 callback.done(doneSync);
068 }
069 }
070
071 @Override
072 public String toString() {
073 return "SubUnitOfWorkCallback";
074 }
075 });
076 }
077
078 @Override
079 public String toString() {
080 return "SubUnitOfWorkProcessor[" + processor + "]";
081 }
082 }