Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.camel.AsyncCallback;
import org.apache.camel.AsyncProcessor;
import org.apache.camel.AsyncProducerCallback;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.ErrorHandlerFactory;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Expression;
import org.apache.camel.FailedToCreateProducerException;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.Traceable;
import org.apache.camel.builder.ExpressionBuilder;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.impl.EmptyProducerCache;
import org.apache.camel.impl.ProducerCache;
import org.apache.camel.spi.EndpointUtilizationStatistics;
import org.apache.camel.spi.IdAware;
import org.apache.camel.spi.RouteContext;
import org.apache.camel.support.ServiceSupport;
import org.apache.camel.util.AsyncProcessorHelper;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.KeyValueHolder;
import org.apache.camel.util.MessageHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.ServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.camel.processor.PipelineHelper.continueProcessing;
import static org.apache.camel.util.ObjectHelper.notNull;
/**
* Implements a Routing Slip
* pattern where the list of actual endpoints to send a message exchange to are
* dependent on the value of a message header.
*
* This implementation mirrors the logic from the {@link org.apache.camel.processor.Pipeline} in the async variation
* as the failover load balancer is a specialized pipeline. So the trick is to keep doing the same as the
* pipeline to ensure it works the same and the async routing engine is flawless.
*/
public class RoutingSlip extends ServiceSupport implements AsyncProcessor, Traceable, IdAware {
protected final Logger log = LoggerFactory.getLogger(getClass());
protected String id;
protected ProducerCache producerCache;
protected int cacheSize;
protected boolean ignoreInvalidEndpoints;
protected String header;
protected Expression expression;
protected String uriDelimiter;
protected final CamelContext camelContext;
private final ConcurrentMap errorHandlers = new ConcurrentHashMap();
/**
* Class that represents prepared fine grained error handlers when processing routingslip/dynamic-router exchanges
*
* This is similar to how multicast processor does.
*/
static final class PreparedErrorHandler extends KeyValueHolder {
PreparedErrorHandler(RouteContext key, Processor value) {
super(key, value);
}
}
/**
* The iterator to be used for retrieving the next routing slip(s) to be used.
*/
protected interface RoutingSlipIterator {
/**
* Are the more routing slip(s)?
*
* @param exchange the current exchange
* @return true if more slips, false otherwise.
*/
boolean hasNext(Exchange exchange);
/**
* Returns the next routing slip(s).
*
* @param exchange the current exchange
* @return the slip(s).
*/
Object next(Exchange exchange);
}
public RoutingSlip(CamelContext camelContext) {
notNull(camelContext, "camelContext");
this.camelContext = camelContext;
}
public RoutingSlip(CamelContext camelContext, Expression expression, String uriDelimiter) {
notNull(camelContext, "camelContext");
notNull(expression, "expression");
this.camelContext = camelContext;
this.expression = expression;
this.uriDelimiter = uriDelimiter;
this.header = null;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Expression getExpression() {
return expression;
}
public String getUriDelimiter() {
return uriDelimiter;
}
public void setDelimiter(String delimiter) {
this.uriDelimiter = delimiter;
}
public boolean isIgnoreInvalidEndpoints() {
return ignoreInvalidEndpoints;
}
public void setIgnoreInvalidEndpoints(boolean ignoreInvalidEndpoints) {
this.ignoreInvalidEndpoints = ignoreInvalidEndpoints;
}
public int getCacheSize() {
return cacheSize;
}
public void setCacheSize(int cacheSize) {
this.cacheSize = cacheSize;
}
@Override
public String toString() {
return "RoutingSlip[expression=" + expression + " uriDelimiter=" + uriDelimiter + "]";
}
public String getTraceLabel() {
return "routingSlip[" + expression + "]";
}
public void process(Exchange exchange) throws Exception {
AsyncProcessorHelper.process(this, exchange);
}
public boolean process(Exchange exchange, AsyncCallback callback) {
if (!isStarted()) {
exchange.setException(new IllegalStateException("RoutingSlip has not been started: " + this));
callback.done(true);
return true;
}
return doRoutingSlipWithExpression(exchange, this.expression, callback);
}
public boolean doRoutingSlip(Exchange exchange, Object routingSlip, AsyncCallback callback) {
if (routingSlip instanceof Expression) {
return doRoutingSlipWithExpression(exchange, (Expression) routingSlip, callback);
} else {
return doRoutingSlipWithExpression(exchange, ExpressionBuilder.constantExpression(routingSlip), callback);
}
}
/**
* Creates the route slip iterator to be used.
*
* @param exchange the exchange
* @param expression the expression
* @return the iterator, should never be null
*/
protected RoutingSlipIterator createRoutingSlipIterator(final Exchange exchange, final Expression expression) throws Exception {
Object slip = expression.evaluate(exchange, Object.class);
if (exchange.getException() != null) {
// force any exceptions occurred during evaluation to be thrown
throw exchange.getException();
}
final Iterator