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.impl;
018
019 import java.util.HashMap;
020 import java.util.Map;
021
022 import org.apache.camel.AsyncCallback;
023 import org.apache.camel.Endpoint;
024 import org.apache.camel.Exchange;
025 import org.apache.camel.ExchangePattern;
026 import org.apache.camel.FailedToCreateProducerException;
027 import org.apache.camel.Processor;
028 import org.apache.camel.Producer;
029 import org.apache.camel.RuntimeCamelException;
030 import org.apache.camel.impl.converter.AsyncProcessorTypeConverter;
031 import org.apache.camel.util.ServiceHelper;
032 import org.apache.commons.logging.Log;
033 import org.apache.commons.logging.LogFactory;
034
035 /**
036 * Cache containing created {@link Producer}.
037 *
038 * @version $Revision: 660275 $
039 */
040 public class ProducerCache<E extends Exchange> extends ServiceSupport {
041 private static final transient Log LOG = LogFactory.getLog(ProducerCache.class);
042
043 private Map<String, Producer<E>> producers = new HashMap<String, Producer<E>>();
044
045 public synchronized Producer<E> getProducer(Endpoint<E> endpoint) {
046 String key = endpoint.getEndpointUri();
047 Producer<E> answer = producers.get(key);
048 if (answer == null) {
049 try {
050 answer = endpoint.createProducer();
051 answer.start();
052 } catch (Exception e) {
053 throw new FailedToCreateProducerException(endpoint, e);
054 }
055 producers.put(key, answer);
056 }
057 return answer;
058 }
059
060 /**
061 * Sends the exchange to the given endpoint
062 *
063 * @param endpoint the endpoint to send the exchange to
064 * @param exchange the exchange to send
065 */
066 public void send(Endpoint<E> endpoint, E exchange) {
067 try {
068 Producer<E> producer = getProducer(endpoint);
069 producer.process(exchange);
070 } catch (Exception e) {
071 throw new RuntimeCamelException(e);
072 }
073 }
074
075 /**
076 * Sends an exchange to an endpoint using a supplied
077 * {@link Processor} to populate the exchange
078 *
079 * @param endpoint the endpoint to send the exchange to
080 * @param processor the transformer used to populate the new exchange
081 */
082 public E send(Endpoint<E> endpoint, Processor processor) {
083 try {
084 Producer<E> producer = getProducer(endpoint);
085 E exchange = producer.createExchange();
086 return sendExchange(endpoint, producer, processor, exchange);
087 } catch (Exception e) {
088 throw new RuntimeCamelException(e);
089 }
090 }
091
092 /**
093 * Sends an exchange to an endpoint using a supplied
094 * {@link Processor} to populate the exchange. The callback
095 * will be called when the exchange is completed.
096 *
097 * @param endpoint the endpoint to send the exchange to
098 * @param processor the transformer used to populate the new exchange
099 */
100 public E send(Endpoint<E> endpoint, Processor processor, AsyncCallback callback) {
101 try {
102 Producer<E> producer = getProducer(endpoint);
103 E exchange = producer.createExchange();
104 boolean sync = sendExchange(endpoint, producer, processor, exchange, callback);
105 setProcessedSync(exchange, sync);
106 return exchange;
107 } catch (Exception e) {
108 throw new RuntimeCamelException(e);
109 }
110 }
111
112 public static boolean isProcessedSync(Exchange exchange) {
113 Boolean rc = exchange.getProperty(ProducerCache.class.getName() + ".SYNC", Boolean.class);
114 return rc == null ? false : rc;
115 }
116
117 public static void setProcessedSync(Exchange exchange, boolean b) {
118 exchange.setProperty(ProducerCache.class.getName() + ".SYNC", b ? Boolean.TRUE : Boolean.FALSE);
119 }
120
121 /**
122 * Sends an exchange to an endpoint using a supplied
123 * {@link Processor} to populate the exchange
124 *
125 * @param endpoint the endpoint to send the exchange to
126 * @param pattern the message {@link ExchangePattern} such as
127 * {@link ExchangePattern#InOnly} or {@link ExchangePattern#InOut}
128 * @param processor the transformer used to populate the new exchange
129 */
130 public E send(Endpoint<E> endpoint, ExchangePattern pattern, Processor processor) {
131 try {
132 Producer<E> producer = getProducer(endpoint);
133 E exchange = producer.createExchange(pattern);
134 return sendExchange(endpoint, producer, processor, exchange);
135 } catch (Exception e) {
136 throw new RuntimeCamelException(e);
137 }
138 }
139
140
141 protected E sendExchange(Endpoint<E> endpoint, Producer<E> producer, Processor processor, E exchange) throws Exception {
142 // lets populate using the processor callback
143 processor.process(exchange);
144
145 // now lets dispatch
146 if (LOG.isDebugEnabled()) {
147 LOG.debug(">>>> " + endpoint + " " + exchange);
148 }
149 producer.process(exchange);
150 return exchange;
151 }
152
153 protected boolean sendExchange(Endpoint<E> endpoint, Producer<E> producer, Processor processor, E exchange, AsyncCallback callback) throws Exception {
154 // lets populate using the processor callback
155 processor.process(exchange);
156
157 // now lets dispatch
158 if (LOG.isDebugEnabled()) {
159 LOG.debug(">>>> " + endpoint + " " + exchange);
160 }
161 return AsyncProcessorTypeConverter.convert(producer).process(exchange, callback);
162 }
163
164 protected void doStop() throws Exception {
165 ServiceHelper.stopServices(producers.values());
166 producers.clear();
167 }
168
169 protected void doStart() throws Exception {
170 }
171 }