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: 779038 $ 035 */ 036 public class SendProcessor extends ServiceSupport implements Processor { 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 void process(final Exchange exchange) throws Exception { 058 getProducerCache(exchange).doInProducer(destination, exchange, pattern, new ProducerCallback<Exchange>() { 059 public Exchange doInProducer(Producer producer, Exchange exchange, ExchangePattern pattern) throws Exception { 060 exchange = configureExchange(exchange, pattern); 061 producer.process(exchange); 062 return exchange; 063 } 064 }); 065 } 066 067 protected ProducerCache getProducerCache(Exchange exchange) throws Exception { 068 // setup producer cache as we need to use the pluggable service pool defined on camel context 069 if (producerCache == null) { 070 this.producerCache = new ProducerCache(exchange.getContext().getProducerServicePool()); 071 this.producerCache.start(); 072 } 073 return this.producerCache; 074 } 075 076 public Endpoint getDestination() { 077 return destination; 078 } 079 080 protected Exchange configureExchange(Exchange exchange, ExchangePattern pattern) { 081 if (pattern != null) { 082 exchange.setPattern(pattern); 083 } 084 return exchange; 085 } 086 087 protected void doStart() throws Exception { 088 if (producerCache != null) { 089 producerCache.start(); 090 } 091 } 092 093 protected void doStop() throws Exception { 094 if (producerCache != null) { 095 producerCache.stop(); 096 } 097 } 098 099 }