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 * Indicates if this aggregation strategy uses pre-completion mode. 063 * @return <tt>true</tt> if this strategy uses pre-completion mode, or <tt>false</tt> otherwise. 064 */ 065 default boolean canPreComplete() { 066 return false; 067 } 068 069 /** 070 * Determines if the aggregation should complete the current group, and start a new group, or the aggregation 071 * should continue using the current group. This callback will only be called if {@link #canPreComplete()} 072 * returns <tt>true</tt>. 073 * 074 * @param oldExchange the oldest exchange (is <tt>null</tt> on first aggregation as we only have the new exchange) 075 * @param newExchange the newest exchange (can be <tt>null</tt> if there was no data possible to acquire) 076 * @return <tt>true</tt> to complete current group and start a new group, or <tt>false</tt> to keep using current 077 */ 078 default boolean preComplete(Exchange oldExchange, Exchange newExchange) { 079 return false; 080 } 081 082 /** 083 * The aggregated {@link Exchange} has completed 084 * 085 * <b>Important: </b> This method must <b>not</b> throw any exceptions. 086 * 087 * @param exchange the current aggregated exchange, or the original {@link org.apache.camel.Exchange} if no aggregation 088 * has been done before the completion occurred 089 */ 090 default void onCompletion(Exchange exchange) { 091 } 092 093 /** 094 * A timeout occurred. 095 * <p/> 096 * <b>Important: </b> This method must <b>not</b> throw any exceptions. 097 * 098 * @param exchange the current aggregated exchange, or the original {@link Exchange} if no aggregation 099 * has been done before the timeout occurred 100 * @param index the index, may be <tt>-1</tt> if not possible to determine the index 101 * @param total the total, may be <tt>-1</tt> if not possible to determine the total 102 * @param timeout the timeout value in millis, may be <tt>-1</tt> if not possible to determine the timeout 103 */ 104 default void timeout(Exchange exchange, int index, int total, long timeout) { 105 // log a WARN we timed out since it will not be aggregated and the Exchange will be lost 106 LoggerFactory.getLogger(getClass()).warn("Parallel processing timed out after {} millis for number {}. This task will be cancelled and will not be aggregated.", timeout, index); 107 } 108 109 /** 110 * Callback when the aggregated {@link Exchange} fails to add 111 * in the {@link org.apache.camel.spi.OptimisticLockingAggregationRepository} because of 112 * an {@link org.apache.camel.spi.OptimisticLockingAggregationRepository.OptimisticLockingException}. 113 * <p/> 114 * Please note that when aggregating {@link Exchange}'s to be careful not to modify and return the {@code oldExchange} 115 * from the {@link AggregationStrategy#aggregate(org.apache.camel.Exchange, org.apache.camel.Exchange)} method. 116 * If you are using the default MemoryAggregationRepository this will mean you have modified the value of an object 117 * already referenced/stored by the MemoryAggregationRepository. This makes it impossible for optimistic locking 118 * to work correctly with the MemoryAggregationRepository. 119 * <p/> 120 * You should instead return either the new {@code newExchange} or a completely new instance of {@link Exchange}. This 121 * is due to the nature of how the underlying {@link java.util.concurrent.ConcurrentHashMap} performs CAS operations 122 * on the value identity. 123 * 124 * @see java.util.concurrent.ConcurrentHashMap 125 */ 126 default void onOptimisticLockFailure(Exchange oldExchange, Exchange newExchange) { 127 LoggerFactory.getLogger(getClass()).trace("onOptimisticLockFailure with AggregationStrategy: {}, oldExchange: {}, newExchange: {}", this, oldExchange, newExchange); 128 } 129 130}