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; 018 019import org.slf4j.LoggerFactory; 020 021/** 022 * A strategy for aggregating two exchanges together into a single exchange. 023 * <p/> 024 * On the first invocation of the {@link #aggregate(org.apache.camel.Exchange, org.apache.camel.Exchange) aggregate} 025 * method the <tt>oldExchange</tt> parameter is <tt>null</tt>. The reason is that we have not aggregated anything yet. 026 * So its only the <tt>newExchange</tt> that has a value. Usually you just return the <tt>newExchange</tt> in this 027 * situation. But you still have the power to decide what to do, for example you can do some alternation on the exchange 028 * or remove some headers. And a more common use case is for instance to count some values from the body payload. That 029 * could be to sum up a total amount etc. 030 * <p/> 031 * Note that <tt>oldExchange</tt> may be <tt>null</tt> more than once when this strategy is throwing a {@link java.lang.RuntimeException} 032 * and <tt>parallelProcessing</tt> is used. You can work around this behavior using the <tt>stopOnAggregateException</tt> option. 033 * <p/> 034 * It is possible that <tt>newExchange</tt> is <tt>null</tt> which could happen if there was no data possible 035 * to acquire. Such as when using a <tt>PollEnricher</tt> to poll from a JMS queue which 036 * is empty and a timeout was set. 037 * <p/> 038 * Possible implementations include performing some kind of combining or delta processing, such as adding line items 039 * together into an invoice or just using the newest exchange and removing old exchanges such as for state tracking or 040 * market data prices; where old values are of little use. 041 * <p/> 042 * If an implementation also implements {@link org.apache.camel.Service} then any <a href="http://camel.apache.org/eip">EIP</a> 043 * that allowing configuring a {@link AggregationStrategy} will invoke the {@link org.apache.camel.Service#start()} 044 * and {@link org.apache.camel.Service#stop()} to control the lifecycle aligned with the EIP itself. 045 * <p/> 046 * If an implementation also implements {@link org.apache.camel.CamelContextAware} then any <a href="http://camel.apache.org/eip">EIP</a> 047 * that allowing configuring a {@link AggregationStrategy} will inject the {@link org.apache.camel.CamelContext} prior 048 * to using the aggregation strategy. 049 */ 050public interface AggregationStrategy { 051 052 /** 053 * Aggregates an old and new exchange together to create a single combined exchange 054 * 055 * @param oldExchange the oldest exchange (is <tt>null</tt> on first aggregation as we only have the new exchange) 056 * @param newExchange the newest exchange (can be <tt>null</tt> if there was no data possible to acquire) 057 * @return a combined composite of the two exchanges, favor returning the <tt>oldExchange</tt> whenever possible 058 */ 059 Exchange aggregate(Exchange oldExchange, Exchange newExchange); 060 061 /** 062 * Aggregates an old and new exchange together to create a single combined exchange. 063 * <p/> 064 * Important: Only Multicast and Recipient List EIP supports this method with access to the input exchange. All other EIPs 065 * does not and uses the {@link #aggregate(Exchange, Exchange)} method instead. 066 * 067 * @param oldExchange the oldest exchange (is <tt>null</tt> on first aggregation as we only have the new exchange) 068 * @param newExchange the newest exchange (can be <tt>null</tt> if there was no data possible to acquire) 069 * @param inputExchange the input exchange (input to the EIP) 070 * @return a combined composite of the two exchanges, favor returning the <tt>oldExchange</tt> whenever possible 071 */ 072 default Exchange aggregate(Exchange oldExchange, Exchange newExchange, Exchange inputExchange) { 073 return aggregate(oldExchange, newExchange); 074 } 075 076 /** 077 * Indicates if this aggregation strategy uses pre-completion mode. 078 * @return <tt>true</tt> if this strategy uses pre-completion mode, or <tt>false</tt> otherwise. 079 */ 080 default boolean canPreComplete() { 081 return false; 082 } 083 084 /** 085 * Determines if the aggregation should complete the current group, and start a new group, or the aggregation 086 * should continue using the current group. This callback will only be called if {@link #canPreComplete()} 087 * returns <tt>true</tt>. 088 * 089 * @param oldExchange the oldest exchange (is <tt>null</tt> on first aggregation as we only have the new exchange) 090 * @param newExchange the newest exchange (can be <tt>null</tt> if there was no data possible to acquire) 091 * @return <tt>true</tt> to complete current group and start a new group, or <tt>false</tt> to keep using current 092 */ 093 default boolean preComplete(Exchange oldExchange, Exchange newExchange) { 094 return false; 095 } 096 097 /** 098 * The aggregated {@link Exchange} has completed 099 * 100 * <b>Important: </b> This method must <b>not</b> throw any exceptions. 101 * 102 * @param exchange the current aggregated exchange, or the original {@link org.apache.camel.Exchange} if no aggregation 103 * has been done before the completion occurred 104 */ 105 default void onCompletion(Exchange exchange) { 106 } 107 108 /** 109 * A timeout occurred. 110 * <p/> 111 * <b>Important: </b> This method must <b>not</b> throw any exceptions. 112 * 113 * @param exchange the current aggregated exchange, or the original {@link Exchange} if no aggregation 114 * has been done before the timeout occurred 115 * @param index the index, may be <tt>-1</tt> if not possible to determine the index 116 * @param total the total, may be <tt>-1</tt> if not possible to determine the total 117 * @param timeout the timeout value in millis, may be <tt>-1</tt> if not possible to determine the timeout 118 */ 119 default void timeout(Exchange exchange, int index, int total, long timeout) { 120 // log a WARN we timed out since it will not be aggregated and the Exchange will be lost 121 LoggerFactory.getLogger(getClass()).warn("Parallel processing timed out after {} millis for number {}. This task will be cancelled and will not be aggregated.", timeout, index); 122 } 123 124 /** 125 * Callback when the aggregated {@link Exchange} fails to add 126 * in the {@link org.apache.camel.spi.OptimisticLockingAggregationRepository} because of 127 * an {@link org.apache.camel.spi.OptimisticLockingAggregationRepository.OptimisticLockingException}. 128 * <p/> 129 * Please note that when aggregating {@link Exchange}'s to be careful not to modify and return the {@code oldExchange} 130 * from the {@link AggregationStrategy#aggregate(org.apache.camel.Exchange, org.apache.camel.Exchange)} method. 131 * If you are using the default MemoryAggregationRepository this will mean you have modified the value of an object 132 * already referenced/stored by the MemoryAggregationRepository. This makes it impossible for optimistic locking 133 * to work correctly with the MemoryAggregationRepository. 134 * <p/> 135 * You should instead return either the new {@code newExchange} or a completely new instance of {@link Exchange}. This 136 * is due to the nature of how the underlying {@link java.util.concurrent.ConcurrentHashMap} performs CAS operations 137 * on the value identity. 138 * 139 * @see java.util.concurrent.ConcurrentHashMap 140 */ 141 default void onOptimisticLockFailure(Exchange oldExchange, Exchange newExchange) { 142 LoggerFactory.getLogger(getClass()).trace("onOptimisticLockFailure with AggregationStrategy: {}, oldExchange: {}, newExchange: {}", this, oldExchange, newExchange); 143 } 144 145}