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.processor.saga;
018
019import java.util.concurrent.CompletableFuture;
020
021import org.apache.camel.AsyncCallback;
022import org.apache.camel.CamelContext;
023import org.apache.camel.Exchange;
024import org.apache.camel.Processor;
025import org.apache.camel.model.SagaCompletionMode;
026import org.apache.camel.saga.CamelSagaCoordinator;
027import org.apache.camel.saga.CamelSagaService;
028import org.apache.camel.saga.CamelSagaStep;
029
030/**
031 * Saga processor implementing the REQUIRED propagation mode.
032 */
033public class RequiredSagaProcessor extends SagaProcessor {
034
035    public RequiredSagaProcessor(CamelContext camelContext, Processor childProcessor, CamelSagaService sagaService, SagaCompletionMode completionMode, CamelSagaStep step) {
036        super(camelContext, childProcessor, sagaService, completionMode, step);
037    }
038
039    @Override
040    public boolean process(Exchange exchange, AsyncCallback callback) {
041        getCurrentSagaCoordinator(exchange).whenComplete((existingCoordinator, ex) -> ifNotException(ex, exchange, callback, () -> {
042            CompletableFuture<CamelSagaCoordinator> coordinatorFuture;
043            final boolean inheritedCoordinator;
044            if (existingCoordinator != null) {
045                coordinatorFuture = CompletableFuture.completedFuture(existingCoordinator);
046                inheritedCoordinator = true;
047            } else {
048                coordinatorFuture = sagaService.newSaga();
049                inheritedCoordinator = false;
050            }
051
052            coordinatorFuture.whenComplete((coordinator, ex2) -> ifNotException(ex2, exchange, callback, () -> {
053                setCurrentSagaCoordinator(exchange, coordinator);
054                coordinator.beginStep(exchange, step).whenComplete((done, ex3) -> ifNotException(ex3, exchange, callback, () -> {
055                    super.process(exchange, doneSync -> {
056                        if (!inheritedCoordinator) {
057                            // Saga starts and ends here
058                            handleSagaCompletion(exchange, coordinator, null, callback);
059                        } else {
060                            callback.done(false);
061                        }
062                    });
063                }));
064            }));
065        }));
066
067        return false;
068    }
069}