/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.processor;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import org.apache.camel.AsyncCallback;
import org.apache.camel.AsyncProcessor;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Processor;
import org.apache.camel.impl.ProducerCache;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
import org.apache.camel.support.ServiceSupport;
import org.apache.camel.util.AsyncProcessorHelper;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.ServiceHelper;
import static org.apache.camel.util.ObjectHelper.notNull;
/**
* Implements a dynamic Recipient List
* pattern where the list of actual endpoints to send a message exchange to are
* dependent on some dynamic expression.
*
* @version
*/
public class RecipientList extends ServiceSupport implements AsyncProcessor {
private final CamelContext camelContext;
private ProducerCache producerCache;
private Expression expression;
private final String delimiter;
private boolean parallelProcessing;
private boolean stopOnException;
private boolean ignoreInvalidEndpoints;
private boolean streaming;
private long timeout;
private Processor onPrepare;
private boolean shareUnitOfWork;
private ExecutorService executorService;
private boolean shutdownExecutorService;
private ExecutorService aggregateExecutorService;
private AggregationStrategy aggregationStrategy = new UseLatestAggregationStrategy();
public RecipientList(CamelContext camelContext) {
// use comma by default as delimiter
this(camelContext, ",");
}
public RecipientList(CamelContext camelContext, String delimiter) {
notNull(camelContext, "camelContext");
ObjectHelper.notEmpty(delimiter, "delimiter");
this.camelContext = camelContext;
this.delimiter = delimiter;
}
public RecipientList(CamelContext camelContext, Expression expression) {
// use comma by default as delimiter
this(camelContext, expression, ",");
}
public RecipientList(CamelContext camelContext, Expression expression, String delimiter) {
notNull(camelContext, "camelContext");
ObjectHelper.notNull(expression, "expression");
ObjectHelper.notEmpty(delimiter, "delimiter");
this.camelContext = camelContext;
this.expression = expression;
this.delimiter = delimiter;
}
@Override
public String toString() {
return "RecipientList[" + (expression != null ? expression : "") + "]";
}
public void process(Exchange exchange) throws Exception {
AsyncProcessorHelper.process(this, exchange);
}
public boolean process(Exchange exchange, AsyncCallback callback) {
if (!isStarted()) {
throw new IllegalStateException("RecipientList has not been started: " + this);
}
// use the evaluate expression result if exists
Object recipientList = exchange.removeProperty(Exchange.EVALUATE_EXPRESSION_RESULT);
if (recipientList == null && expression != null) {
// fallback and evaluate the expression
recipientList = expression.evaluate(exchange, Object.class);
}
return sendToRecipientList(exchange, recipientList, callback);
}
/**
* Sends the given exchange to the recipient list
*/
public boolean sendToRecipientList(Exchange exchange, Object recipientList, AsyncCallback callback) {
Iterator