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.activemq.transport.reliable; 018 019import java.io.IOException; 020 021/** 022 * Throws an exception if packets are dropped causing the transport to be 023 * closed. 024 * 025 * 026 */ 027public class ExceptionIfDroppedReplayStrategy implements ReplayStrategy { 028 029 private int maximumDifference = 5; 030 031 public ExceptionIfDroppedReplayStrategy() { 032 } 033 034 public ExceptionIfDroppedReplayStrategy(int maximumDifference) { 035 this.maximumDifference = maximumDifference; 036 } 037 038 public boolean onDroppedPackets(ReliableTransport transport, int expectedCounter, int actualCounter, int nextAvailableCounter) throws IOException { 039 int difference = actualCounter - expectedCounter; 040 long count = Math.abs(difference); 041 if (count > maximumDifference) { 042 throw new IOException("Packets dropped on: " + transport + " count: " + count + " expected: " + expectedCounter + " but was: " + actualCounter); 043 } 044 045 // lets discard old commands 046 return difference > 0; 047 } 048 049 public void onReceivedPacket(ReliableTransport transport, long expectedCounter) { 050 } 051 052 public int getMaximumDifference() { 053 return maximumDifference; 054 } 055 056 /** 057 * Sets the maximum allowed difference between an expected packet and an 058 * actual packet before an error occurs 059 */ 060 public void setMaximumDifference(int maximumDifference) { 061 this.maximumDifference = maximumDifference; 062 } 063 064}