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.component.seda;
018    
019    import java.util.concurrent.BlockingQueue;
020    import java.util.concurrent.CountDownLatch;
021    import java.util.concurrent.TimeUnit;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.ExchangeTimedOutException;
025    import org.apache.camel.WaitForTaskToComplete;
026    import org.apache.camel.impl.SynchronizationAdapter;
027    import org.apache.camel.util.ExchangeHelper;
028    
029    /**
030     * @version $Revision: 889199 $
031     */
032    public class SedaProducer extends CollectionProducer {
033        private final SedaEndpoint endpoint;
034        private final WaitForTaskToComplete waitForTaskToComplete;
035        private final long timeout;
036    
037        public SedaProducer(SedaEndpoint endpoint, BlockingQueue<Exchange> queue, WaitForTaskToComplete waitForTaskToComplete, long timeout) {
038            super(endpoint, queue);
039            this.endpoint = endpoint;
040            this.waitForTaskToComplete = waitForTaskToComplete;
041            this.timeout = timeout;
042        }
043    
044        @Override
045        public void process(final Exchange exchange) throws Exception {
046            // use a new copy of the exchange to route async and handover the on completion to the new copy
047            // so its the new copy that performs the on completion callback when its done
048            Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, true);
049            // set a new from endpoint to be the seda queue
050            copy.setFromEndpoint(endpoint);
051    
052            WaitForTaskToComplete wait = waitForTaskToComplete;
053            if (exchange.getProperty(Exchange.ASYNC_WAIT) != null) {
054                wait = exchange.getProperty(Exchange.ASYNC_WAIT, WaitForTaskToComplete.class);
055            }
056    
057            if (wait == WaitForTaskToComplete.Always
058                || (wait == WaitForTaskToComplete.IfReplyExpected && ExchangeHelper.isOutCapable(exchange))) {
059    
060                // latch that waits until we are complete
061                final CountDownLatch latch = new CountDownLatch(1);
062    
063                // we should wait for the reply so install a on completion so we know when its complete
064                copy.addOnCompletion(new SynchronizationAdapter() {
065                    @Override
066                    public void onDone(Exchange response) {
067                        try {
068                            ExchangeHelper.copyResults(exchange, response);
069                        } finally {
070                            // always ensure latch is triggered
071                            latch.countDown();
072                        }
073                    }
074                });
075    
076                queue.add(copy);
077    
078                if (timeout > 0) {
079                    if (log.isTraceEnabled()) {
080                        log.trace("Waiting for task to complete using timeout (ms): " + timeout);
081                    }
082                    // lets see if we can get the task done before the timeout
083                    boolean done = latch.await(timeout, TimeUnit.MILLISECONDS);
084                    if (!done) {
085                        exchange.setException(new ExchangeTimedOutException(exchange, timeout));
086                    }
087                } else {
088                    if (log.isTraceEnabled()) {
089                        log.trace("Waiting for task to complete (blocking)");
090                    }
091                    // no timeout then wait until its done
092                    latch.await();
093                }
094            } else {
095                // no wait, eg its a InOnly then just add to queue and return
096                queue.add(copy);
097            }
098        }
099    
100        @Override
101        protected void doStart() throws Exception {
102            super.doStart();
103            endpoint.onStarted(this);
104        }
105    
106        @Override
107        protected void doStop() throws Exception {
108            endpoint.onStopped(this);
109            super.doStop();
110        }
111    }