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.processor;
018
019 import org.apache.camel.Endpoint;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.ExchangePattern;
022 import org.apache.camel.Processor;
023 import org.apache.camel.Producer;
024 import org.apache.camel.ProducerCallback;
025 import org.apache.camel.impl.ProducerCache;
026 import org.apache.camel.impl.ServiceSupport;
027 import org.apache.camel.util.ObjectHelper;
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030
031 /**
032 * Processor for forwarding exchanges to an endpoint destination.
033 *
034 * @version $Revision: 788621 $
035 */
036 public class SendProcessor extends ServiceSupport implements Processor, Traceable {
037 protected static final transient Log LOG = LogFactory.getLog(SendProcessor.class);
038 protected ProducerCache producerCache;
039 protected Endpoint destination;
040 protected ExchangePattern pattern;
041
042 public SendProcessor(Endpoint destination) {
043 ObjectHelper.notNull(destination, "destination");
044 this.destination = destination;
045 }
046
047 public SendProcessor(Endpoint destination, ExchangePattern pattern) {
048 this(destination);
049 this.pattern = pattern;
050 }
051
052 @Override
053 public String toString() {
054 return "sendTo(" + destination + (pattern != null ? " " + pattern : "") + ")";
055 }
056
057 public String getTraceLabel() {
058 return destination.getEndpointUri();
059 }
060
061 public void process(final Exchange exchange) throws Exception {
062 getProducerCache(exchange).doInProducer(destination, exchange, pattern, new ProducerCallback<Exchange>() {
063 public Exchange doInProducer(Producer producer, Exchange exchange, ExchangePattern pattern) throws Exception {
064 exchange = configureExchange(exchange, pattern);
065 producer.process(exchange);
066 return exchange;
067 }
068 });
069 }
070
071 protected ProducerCache getProducerCache(Exchange exchange) throws Exception {
072 // setup producer cache as we need to use the pluggable service pool defined on camel context
073 if (producerCache == null) {
074 this.producerCache = new ProducerCache(exchange.getContext().getProducerServicePool());
075 this.producerCache.start();
076 }
077 return this.producerCache;
078 }
079
080 public Endpoint getDestination() {
081 return destination;
082 }
083
084 protected Exchange configureExchange(Exchange exchange, ExchangePattern pattern) {
085 if (pattern != null) {
086 exchange.setPattern(pattern);
087 }
088 return exchange;
089 }
090
091 protected void doStart() throws Exception {
092 if (producerCache != null) {
093 producerCache.start();
094 }
095 }
096
097 protected void doStop() throws Exception {
098 if (producerCache != null) {
099 producerCache.stop();
100 }
101 }
102
103 }