KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > reliable > DefaultReplayStrategy


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.reliable;
19
20 import java.io.IOException JavaDoc;
21
22 /**
23  * Throws an exception if packets are dropped causing the transport to be
24  * closed.
25  *
26  * @version $Revision: 426366 $
27  */

28 public class DefaultReplayStrategy implements ReplayStrategy {
29
30     private int maximumDifference = 5;
31
32     public DefaultReplayStrategy() {
33     }
34
35     public DefaultReplayStrategy(int maximumDifference) {
36         this.maximumDifference = maximumDifference;
37     }
38
39     public boolean onDroppedPackets(ReliableTransport transport, int expectedCounter, int actualCounter, int nextAvailableCounter) throws IOException JavaDoc {
40         int difference = actualCounter - expectedCounter;
41         long count = Math.abs(difference);
42         if (count > maximumDifference) {
43             int upperLimit = actualCounter - 1;
44             if (upperLimit < expectedCounter) {
45                 upperLimit = expectedCounter;
46             }
47             transport.requestReplay(expectedCounter, upperLimit);
48         }
49
50         // lets discard old commands
51
return difference > 0;
52     }
53
54     public void onReceivedPacket(ReliableTransport transport, long expectedCounter) {
55         // TODO we could pro-actively evict stuff from the buffer if we knew there was only one client
56
}
57
58     public int getMaximumDifference() {
59         return maximumDifference;
60     }
61
62     /**
63      * Sets the maximum allowed difference between an expected packet and an
64      * actual packet before an error occurs
65      */

66     public void setMaximumDifference(int maximumDifference) {
67         this.maximumDifference = maximumDifference;
68     }
69
70 }
71
Popular Tags