Steve973 commented on code in PR #11906: URL: https://github.com/apache/camel/pull/11906#discussion_r1383984723
########## components/camel-dynamic-router/src/main/java/org/apache/camel/component/dynamicrouter/DynamicRouterMulticastProcessor.java: ########## @@ -0,0 +1,292 @@ +/* + * 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.component.dynamicrouter; + +import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +import org.apache.camel.*; +import org.apache.camel.processor.FilterProcessor; +import org.apache.camel.processor.MulticastProcessor; +import org.apache.camel.processor.ProcessorExchangePair; +import org.apache.camel.spi.ProducerCache; +import org.apache.camel.support.*; +import org.apache.camel.support.service.ServiceHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.camel.component.dynamicrouter.DynamicRouterConstants.MODE_FIRST_MATCH; + +public class DynamicRouterMulticastProcessor extends MulticastProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(DynamicRouterMulticastProcessor.class); + + /** + * Template for a logging endpoint, showing all, and multiline. + */ + private static final String LOG_ENDPOINT = "log:%s.%s?level=%s&showAll=true&multiline=true"; + + private boolean ignoreInvalidEndpoints; + + private final ProducerCache producerCache; + + /** + * {@link FilterProcessor}s, mapped by subscription ID, to determine if the incoming exchange should be routed based + * on the content. + */ + private final TreeMap<String, PrioritizedFilter> filterMap; + + /** + * Indicates the behavior of the Dynamic Router when routing participants are selected to receive an incoming + * exchange. If the mode is "firstMatch", then the exchange is routed only to the first participant that has a + * matching predicate. If the mode is "allMatch", then the exchange is routed to all participants that have a + * matching predicate. + */ + private final String recipientMode; + + /** + * The {@link FilterProcessor} factory. + */ + private final Supplier<PrioritizedFilter.PrioritizedFilterFactory> filterProcessorFactorySupplier; + + /** + * Flag to log a warning if a message is dropped due to no matching filters. + */ + private final boolean warnDroppedMessage; + + public DynamicRouterMulticastProcessor(String id, CamelContext camelContext, Route route, String recipientMode, + final boolean warnDroppedMessage, + final Supplier<PrioritizedFilter.PrioritizedFilterFactory> filterProcessorFactorySupplier, + ProducerCache producerCache, AggregationStrategy aggregationStrategy, + boolean parallelProcessing, ExecutorService executorService, + boolean shutdownExecutorService, + boolean streaming, boolean stopOnException, + long timeout, Processor onPrepare, boolean shareUnitOfWork, + boolean parallelAggregate) { + super(camelContext, route, new ArrayList<>(), aggregationStrategy, parallelProcessing, executorService, + shutdownExecutorService, + streaming, stopOnException, timeout, onPrepare, + shareUnitOfWork, parallelAggregate); + setId(id); + this.producerCache = producerCache; + this.filterMap = new TreeMap<>(); + this.recipientMode = recipientMode; + this.filterProcessorFactorySupplier = filterProcessorFactorySupplier; + this.warnDroppedMessage = warnDroppedMessage; + } + + public boolean isIgnoreInvalidEndpoints() { + return ignoreInvalidEndpoints; + } + + public void setIgnoreInvalidEndpoints(boolean ignoreInvalidEndpoints) { + this.ignoreInvalidEndpoints = ignoreInvalidEndpoints; + } + + protected List<Processor> createEndpointProcessors(Exchange exchange) { + List<String> recipientList = matchFilters(exchange).stream() + .map(PrioritizedFilter::getEndpoint) + .distinct() + .map(String::trim) + .collect(Collectors.toList()); + if (recipientList.isEmpty()) { + // No matching filters, so we will use the default filter that will create a + // notification that there were no routing participants that matched the + // exchange, which results in a "dropped" message. + Message exchangeIn = exchange.getIn(); + Object originalBody = exchangeIn.getBody(); + exchangeIn.setHeader("originalBody", originalBody); + String endpoint = String.format(LOG_ENDPOINT, this.getClass().getCanonicalName(), getId(), + warnDroppedMessage ? "WARN" : "DEBUG"); + recipientList.add(endpoint); + String error = String.format( + "DynamicRouter '%s': no filters matched for an exchange with id: '%s', from route: '%s'. " + + "The 'originalBody' header contains the original message body.", + getId(), exchange.getExchangeId(), exchange.getFromEndpoint()); + exchangeIn.setBody(error, String.class); + } + + return recipientList.stream() + .map(uri -> { + Endpoint endpoint; + try { + endpoint = Optional.ofNullable(exchange.getContext().hasEndpoint(uri)) + .orElse(ExchangeHelper.resolveEndpoint(exchange, uri)); + } catch (Exception e) { + if (isIgnoreInvalidEndpoints()) { + LOG.debug("Endpoint uri is invalid: {}. This exception will be ignored.", uri, e); + return null; + } else { + // failure so break out + throw e; + } + } + return endpoint; + }) + .filter(Objects::nonNull) + .map(endpoint -> { + Producer producer = producerCache.acquireProducer(endpoint); + Route route = ExchangeHelper.getRoute(exchange); + return wrapInErrorHandler(route, exchange, producer); + }) + .toList(); + } Review Comment: Thanks. I see what you mean. Like I mentioned in the previous reply, I have simplified this a bit, so I am looking forward to your feedback. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org