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 */ 017package org.apache.camel.reifier; 018 019import java.util.ArrayList; 020import java.util.List; 021import java.util.concurrent.ExecutorService; 022 023import org.apache.camel.AggregationStrategy; 024import org.apache.camel.CamelContextAware; 025import org.apache.camel.Processor; 026import org.apache.camel.Route; 027import org.apache.camel.model.MulticastDefinition; 028import org.apache.camel.model.ProcessorDefinition; 029import org.apache.camel.processor.MulticastProcessor; 030import org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter; 031import org.apache.camel.processor.aggregate.ShareUnitOfWorkAggregationStrategy; 032import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy; 033 034public class MulticastReifier extends ProcessorReifier<MulticastDefinition> { 035 036 public MulticastReifier(Route route, ProcessorDefinition<?> definition) { 037 super(route, (MulticastDefinition) definition); 038 } 039 040 @Override 041 public Processor createProcessor() throws Exception { 042 Processor answer = this.createChildProcessor(true); 043 044 // force the answer as a multicast processor even if there is only one 045 // child processor in the multicast 046 if (!(answer instanceof MulticastProcessor)) { 047 List<Processor> list = new ArrayList<>(1); 048 list.add(answer); 049 answer = createCompositeProcessor(list); 050 } 051 return answer; 052 } 053 054 @Override 055 protected Processor createCompositeProcessor(List<Processor> list) throws Exception { 056 final AggregationStrategy strategy = createAggregationStrategy(); 057 058 boolean isParallelProcessing = parseBoolean(definition.getParallelProcessing(), false); 059 boolean isShareUnitOfWork = parseBoolean(definition.getShareUnitOfWork(), false); 060 boolean isStreaming = parseBoolean(definition.getStreaming(), false); 061 boolean isStopOnException = parseBoolean(definition.getStopOnException(), false); 062 boolean isParallelAggregate = parseBoolean(definition.getParallelAggregate(), false); 063 boolean isStopOnAggregateException = parseBoolean(definition.getStopOnAggregateException(), false); 064 065 boolean shutdownThreadPool = willCreateNewThreadPool(definition, isParallelProcessing); 066 ExecutorService threadPool = getConfiguredExecutorService("Multicast", definition, isParallelProcessing); 067 068 long timeout = definition.getTimeout() != null ? parseDuration(definition.getTimeout()) : 0; 069 if (timeout > 0 && !isParallelProcessing) { 070 throw new IllegalArgumentException("Timeout is used but ParallelProcessing has not been enabled."); 071 } 072 if (definition.getOnPrepareRef() != null) { 073 definition.setOnPrepare(mandatoryLookup(definition.getOnPrepareRef(), Processor.class)); 074 } 075 076 MulticastProcessor answer = new MulticastProcessor(camelContext, route, list, strategy, isParallelProcessing, threadPool, shutdownThreadPool, isStreaming, 077 isStopOnException, timeout, definition.getOnPrepare(), isShareUnitOfWork, isParallelAggregate, 078 isStopOnAggregateException); 079 return answer; 080 } 081 082 private AggregationStrategy createAggregationStrategy() { 083 AggregationStrategy strategy = definition.getAggregationStrategy(); 084 if (strategy == null && definition.getStrategyRef() != null) { 085 Object aggStrategy = lookup(parseString(definition.getStrategyRef()), Object.class); 086 if (aggStrategy instanceof AggregationStrategy) { 087 strategy = (AggregationStrategy)aggStrategy; 088 } else if (aggStrategy != null) { 089 AggregationStrategyBeanAdapter adapter = new AggregationStrategyBeanAdapter(aggStrategy, parseString(definition.getStrategyMethodName())); 090 if (definition.getStrategyMethodAllowNull() != null) { 091 adapter.setAllowNullNewExchange(parseBoolean(definition.getStrategyMethodAllowNull(), false)); 092 adapter.setAllowNullOldExchange(parseBoolean(definition.getStrategyMethodAllowNull(), false)); 093 } 094 strategy = adapter; 095 } else { 096 throw new IllegalArgumentException("Cannot find AggregationStrategy in Registry with name: " + definition.getStrategyRef()); 097 } 098 } 099 100 if (strategy == null) { 101 // default to use latest aggregation strategy 102 strategy = new UseLatestAggregationStrategy(); 103 } 104 105 if (strategy instanceof CamelContextAware) { 106 ((CamelContextAware)strategy).setCamelContext(camelContext); 107 } 108 109 if (parseBoolean(definition.getShareUnitOfWork(), false)) { 110 // wrap strategy in share unit of work 111 strategy = new ShareUnitOfWorkAggregationStrategy(strategy); 112 } 113 114 return strategy; 115 } 116 117}