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