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.net.URISyntaxException;
020import java.util.HashMap;
021
022import org.apache.camel.AsyncCallback;
023import org.apache.camel.AsyncProcessor;
024import org.apache.camel.AsyncProducerCallback;
025import org.apache.camel.CamelContext;
026import org.apache.camel.Endpoint;
027import org.apache.camel.EndpointAware;
028import org.apache.camel.Exchange;
029import org.apache.camel.ExchangePattern;
030import org.apache.camel.Producer;
031import org.apache.camel.ServicePoolAware;
032import org.apache.camel.Traceable;
033import org.apache.camel.impl.InterceptSendToEndpoint;
034import org.apache.camel.impl.ProducerCache;
035import org.apache.camel.spi.IdAware;
036import org.apache.camel.support.ServiceSupport;
037import org.apache.camel.util.AsyncProcessorConverterHelper;
038import org.apache.camel.util.AsyncProcessorHelper;
039import org.apache.camel.util.EndpointHelper;
040import org.apache.camel.util.EventHelper;
041import org.apache.camel.util.ObjectHelper;
042import org.apache.camel.util.ServiceHelper;
043import org.apache.camel.util.StopWatch;
044import org.apache.camel.util.URISupport;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048/**
049 * Processor for forwarding exchanges to a static endpoint destination.
050 *
051 * @see SendDynamicProcessor
052 */
053public class SendProcessor extends ServiceSupport implements AsyncProcessor, Traceable, EndpointAware, IdAware {
054    protected static final Logger LOG = LoggerFactory.getLogger(SendProcessor.class);
055    protected transient String traceLabelToString;
056    protected final CamelContext camelContext;
057    protected final ExchangePattern pattern;
058    protected ProducerCache producerCache;
059    protected AsyncProcessor producer;
060    protected Endpoint destination;
061    protected ExchangePattern destinationExchangePattern;
062    protected String id;
063    protected volatile long counter;
064
065    public SendProcessor(Endpoint destination) {
066        this(destination, null);
067    }
068
069    public SendProcessor(Endpoint destination, ExchangePattern pattern) {
070        ObjectHelper.notNull(destination, "destination");
071        this.destination = destination;
072        this.camelContext = destination.getCamelContext();
073        this.pattern = pattern;
074        try {
075            this.destinationExchangePattern = null;
076            this.destinationExchangePattern = EndpointHelper.resolveExchangePatternFromUrl(destination.getEndpointUri());
077        } catch (URISyntaxException e) {
078            throw ObjectHelper.wrapRuntimeCamelException(e);
079        }
080        ObjectHelper.notNull(this.camelContext, "camelContext");
081    }
082
083    @Override
084    public String toString() {
085        return "sendTo(" + destination + ")";
086    }
087
088    public String getId() {
089        return id;
090    }
091
092    public void setId(String id) {
093        this.id = id;
094    }
095
096    /**
097     * @deprecated not longer supported.
098     */
099    @Deprecated
100    public void setDestination(Endpoint destination) {
101    }
102
103    public String getTraceLabel() {
104        if (traceLabelToString == null) {
105            traceLabelToString = URISupport.sanitizeUri(destination.getEndpointUri());
106        }
107        return traceLabelToString;
108    }
109
110    @Override
111    public Endpoint getEndpoint() {
112        return destination;
113    }
114
115    public void process(final Exchange exchange) throws Exception {
116        AsyncProcessorHelper.process(this, exchange);
117    }
118
119    public boolean process(Exchange exchange, final AsyncCallback callback) {
120        if (!isStarted()) {
121            exchange.setException(new IllegalStateException("SendProcessor has not been started: " + this));
122            callback.done(true);
123            return true;
124        }
125
126        // we should preserve existing MEP so remember old MEP
127        // if you want to permanently to change the MEP then use .setExchangePattern in the DSL
128        final ExchangePattern existingPattern = exchange.getPattern();
129
130        counter++;
131
132        // if we have a producer then use that as its optimized
133        if (producer != null) {
134
135            final Exchange target = configureExchange(exchange, pattern);
136
137            final boolean sending = EventHelper.notifyExchangeSending(exchange.getContext(), target, destination);
138            StopWatch sw = null;
139            if (sending) {
140                sw = new StopWatch();
141            }
142
143            // record timing for sending the exchange using the producer
144            final StopWatch watch = sw;
145
146            try {
147                LOG.debug(">>>> {} {}", destination, exchange);
148                return producer.process(exchange, new AsyncCallback() {
149                    @Override
150                    public void done(boolean doneSync) {
151                        try {
152                            // restore previous MEP
153                            target.setPattern(existingPattern);
154                            // emit event that the exchange was sent to the endpoint
155                            if (watch != null) {
156                                long timeTaken = watch.taken();
157                                EventHelper.notifyExchangeSent(target.getContext(), target, destination, timeTaken);
158                            }
159                        } finally {
160                            callback.done(doneSync);
161                        }
162                    }
163                });
164            } catch (Throwable throwable) {
165                exchange.setException(throwable);
166                callback.done(true);
167            }
168
169            return true;
170        }
171
172        // send the exchange to the destination using the producer cache for the non optimized producers
173        return producerCache.doInAsyncProducer(destination, exchange, pattern, callback, new AsyncProducerCallback() {
174            public boolean doInAsyncProducer(Producer producer, AsyncProcessor asyncProducer, final Exchange exchange,
175                                             ExchangePattern pattern, final AsyncCallback callback) {
176                final Exchange target = configureExchange(exchange, pattern);
177                LOG.debug(">>>> {} {}", destination, exchange);
178                return asyncProducer.process(target, new AsyncCallback() {
179                    public void done(boolean doneSync) {
180                        // restore previous MEP
181                        target.setPattern(existingPattern);
182                        // signal we are done
183                        callback.done(doneSync);
184                    }
185                });
186            }
187        });
188    }
189    
190    public Endpoint getDestination() {
191        return destination;
192    }
193
194    public ExchangePattern getPattern() {
195        return pattern;
196    }
197
198    protected Exchange configureExchange(Exchange exchange, ExchangePattern pattern) {
199        // destination exchange pattern overrides pattern
200        if (destinationExchangePattern != null) {
201            exchange.setPattern(destinationExchangePattern);
202        } else if (pattern != null) {
203            exchange.setPattern(pattern);
204        }
205        // set property which endpoint we send to
206        exchange.setProperty(Exchange.TO_ENDPOINT, destination.getEndpointUri());
207        return exchange;
208    }
209
210    public long getCounter() {
211        return counter;
212    }
213
214    public void reset() {
215        counter = 0;
216    }
217
218    protected void doStart() throws Exception {
219        if (producerCache == null) {
220            // use a single producer cache as we need to only hold reference for one destination
221            // and use a regular HashMap as we do not want a soft reference store that may get re-claimed when low on memory
222            // as we want to ensure the producer is kept around, to ensure its lifecycle is fully managed,
223            // eg stopping the producer when we stop etc.
224            producerCache = new ProducerCache(this, camelContext, new HashMap<String, Producer>(1));
225            // do not add as service as we do not want to manage the producer cache
226        }
227        ServiceHelper.startService(producerCache);
228
229        // the destination could since have been intercepted by a interceptSendToEndpoint so we got to
230        // lookup this before we can use the destination
231        Endpoint lookup = camelContext.hasEndpoint(destination.getEndpointKey());
232        if (lookup instanceof InterceptSendToEndpoint) {
233            if (LOG.isDebugEnabled()) {
234                LOG.debug("Intercepted sending to {} -> {}",
235                        URISupport.sanitizeUri(destination.getEndpointUri()), URISupport.sanitizeUri(lookup.getEndpointUri()));
236            }
237            destination = lookup;
238        }
239        // warm up the producer by starting it so we can fail fast if there was a problem
240        // however must start endpoint first
241        ServiceHelper.startService(destination);
242
243        // this SendProcessor is used a lot in Camel (eg every .to in the route DSL) and therefore we
244        // want to optimize for regular producers, by using the producer directly instead of the ProducerCache.
245        // Only for pooled and non-singleton producers we have to use the ProducerCache as it supports these
246        // kind of producer better (though these kind of producer should be rare)
247
248        Producer producer = producerCache.acquireProducer(destination);
249        if (producer instanceof ServicePoolAware || !producer.isSingleton()) {
250            // no we cannot optimize it - so release the producer back to the producer cache
251            // and use the producer cache for sending
252            producerCache.releaseProducer(destination, producer);
253        } else {
254            // yes we can optimize and use the producer directly for sending
255            this.producer = AsyncProcessorConverterHelper.convert(producer);
256        }
257    }
258
259    protected void doStop() throws Exception {
260        ServiceHelper.stopServices(producerCache, producer);
261    }
262
263    protected void doShutdown() throws Exception {
264        ServiceHelper.stopAndShutdownServices(producerCache, producer);
265    }
266}