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.spi; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.Exchange; 021 022/** 023 * A specialized {@link org.apache.camel.spi.AggregationRepository} which also supports 024 * optimistic locking. 025 * 026 * If the underlying implementation cannot perform optimistic locking, it should 027 * not implement this interface. 028 * 029 * @see org.apache.camel.processor.aggregate.MemoryAggregationRepository 030 * 031 * @version 032 */ 033public interface OptimisticLockingAggregationRepository extends AggregationRepository { 034 035 /** 036 * {@link Exception} used by an {@code AggregationRepository} to indicate that an optimistic 037 * update error has occurred and that the operation should be retried by the caller. 038 * <p/> 039 */ 040 class OptimisticLockingException extends RuntimeException { 041 private static final long serialVersionUID = 1L; 042 } 043 044 /** 045 * Add the given {@link org.apache.camel.Exchange} under the correlation key. 046 * <p/> 047 * Will perform optimistic locking to replace expected existing exchange with the new supplied exchange. 048 * <p/> 049 * If the {@code oldExchange} is null the underlying implementation is to assume this is the very first Exchange for the 050 * supplied correlation key. When the implementation comes to store to the Exchange if there is already an existing 051 * Exchange present for this correlation key the implementation should throw an OptimisticLockingException. 052 * <p/> 053 * If the {@code oldExchange} is not null the underlying implementation should use it to compare with the existing exchange 054 * when doing an atomic compare-and-set/swap operation. 055 * <p/> 056 * The implementation may achieve this by storing a version identifier in the Exchange as a parameter. Set before 057 * returning from {@link AggregationRepository#get(org.apache.camel.CamelContext, String)}} and retrieved from the 058 * exchange when passed to {@link AggregationRepository#add(org.apache.camel.CamelContext, String, org.apache.camel.Exchange)}. 059 * <p/> 060 * Note: The {@link org.apache.camel.processor.aggregate.MemoryAggregationRepository} is an exception to this recommendation. 061 * It uses the {@code oldExchange}'s Object identify to perform it's compare-and-set/swap operation, instead of a version 062 * parameter. This is not the recommended approach, and should be avoided. 063 * <p/> 064 * The {@link org.apache.camel.processor.aggregate.AggregateProcessor} will ensure that the exchange received from 065 * {@link OptimisticLockingAggregationRepository#get(org.apache.camel.CamelContext, String)} is passed as {@code oldExchange}, 066 * and that the aggregated exchange received from the {@link org.apache.camel.processor.aggregate.AggregationStrategy} 067 * is passed as the {@code newExchange}. 068 * 069 * @param camelContext the current CamelContext 070 * @param key the correlation key 071 * @param oldExchange the old exchange that is expected to exist when replacing with the new exchange 072 * @param newExchange the new aggregated exchange, to replace old exchange 073 * @return the old exchange if any existed 074 * @throws OptimisticLockingException This should be thrown when the currently stored exchange differs from the supplied oldExchange. 075 */ 076 Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange) throws OptimisticLockingException; 077 078 /** 079 * Removes the given Exchange when both the supplied key and Exchange are present in the repository. If the supplied Exchange 080 * does not match the Exchange actually stored against the key this method should throw an OptimisticLockingException 081 * to indicate that the value of the correlation key has changed from the expected value. 082 * <p/> 083 * @param camelContext the current CamelContext 084 * @param key the correlation key 085 * @param exchange the exchange to remove 086 * @throws OptimisticLockingException This should be thrown when the exchange has already been deleted, or otherwise modified. 087 */ 088 void remove(CamelContext camelContext, String key, Exchange exchange) throws OptimisticLockingException; 089}