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;
018
019import java.util.concurrent.CountDownLatch;
020
021import org.apache.camel.AsyncCallback;
022import org.apache.camel.AsyncProcessor;
023import org.apache.camel.Endpoint;
024import org.apache.camel.Exchange;
025import org.apache.camel.ExchangePattern;
026import org.apache.camel.Producer;
027import org.apache.camel.util.ServiceHelper;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * Ensures a {@link Producer} is executed within an {@link org.apache.camel.spi.UnitOfWork}.
033 *
034 * @version 
035 */
036public final class UnitOfWorkProducer implements Producer {
037
038    private static final Logger LOG = LoggerFactory.getLogger(UnitOfWorkProducer.class);
039    private final Producer producer;
040    private final AsyncProcessor processor;
041
042    /**
043     * The producer which should be executed within an {@link org.apache.camel.spi.UnitOfWork}.
044     *
045     * @param producer the producer
046     */
047    public UnitOfWorkProducer(Producer producer) {
048        this.producer = producer;
049        // wrap in unit of work
050        CamelInternalProcessor internal = new CamelInternalProcessor(producer);
051        internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(null));
052        this.processor = internal;
053    }
054
055    public Endpoint getEndpoint() {
056        return producer.getEndpoint();
057    }
058
059    public Exchange createExchange() {
060        return producer.createExchange();
061    }
062
063    public Exchange createExchange(ExchangePattern pattern) {
064        return producer.createExchange(pattern);
065    }
066
067    @Deprecated
068    public Exchange createExchange(Exchange exchange) {
069        return producer.createExchange(exchange);
070    }
071
072    public void process(final Exchange exchange) throws Exception {
073        final CountDownLatch latch = new CountDownLatch(1);
074        boolean sync = processor.process(exchange, new AsyncCallback() {
075            public void done(boolean doneSync) {
076                if (!doneSync) {
077                    LOG.trace("Asynchronous callback received for exchangeId: {}", exchange.getExchangeId());
078                    latch.countDown();
079                }
080            }
081
082            @Override
083            public String toString() {
084                return "Done " + processor;
085            }
086        });
087        if (!sync) {
088            LOG.trace("Waiting for asynchronous callback before continuing for exchangeId: {} -> {}",
089                    exchange.getExchangeId(), exchange);
090            latch.await();
091            LOG.trace("Asynchronous callback received, will continue routing exchangeId: {} -> {}",
092                    exchange.getExchangeId(), exchange);
093        }
094    }
095
096    public void start() throws Exception {
097        ServiceHelper.startService(processor);
098    }
099
100    public void stop() throws Exception {
101        ServiceHelper.stopService(processor);
102    }
103
104    public boolean isSingleton() {
105        return producer.isSingleton();
106    }
107
108    @Override
109    public String toString() {
110        return "UnitOfWork(" + producer + ")";
111    }
112}