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: 794648 $
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 = exchange.copy(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                // lets see if we can get the task done before the timeout
078                boolean done = latch.await(timeout, TimeUnit.MILLISECONDS);
079                if (!done) {
080                    exchange.setException(new ExchangeTimedOutException(exchange, timeout));
081                }
082            } else {
083                // no wait, eg its a InOnly then just add to queue and return
084                queue.add(copy);
085            }
086        }
087    
088        @Override
089        protected void doStart() throws Exception {
090            super.doStart();
091            endpoint.onStarted(this);
092        }
093    
094        @Override
095        protected void doStop() throws Exception {
096            endpoint.onStopped(this);
097            super.doStop();
098        }
099    }