1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.springframework.batch.retry.policy; |
18 | |
19 | import java.util.Collections; |
20 | import java.util.HashMap; |
21 | import java.util.Map; |
22 | |
23 | import org.springframework.batch.retry.RetryContext; |
24 | |
25 | /** |
26 | * Map-based implementation of {@link RetryContextCache}. The map backing the |
27 | * cache of contexts is synchronized. |
28 | * |
29 | * @author Dave Syer |
30 | * |
31 | */ |
32 | public class MapRetryContextCache implements RetryContextCache { |
33 | |
34 | /** |
35 | * Default value for maximum capacity of the cache. This is set to a |
36 | * reasonably low value (4096) to avoid users inadvertently filling the |
37 | * cache with item keys that are inconsistent. |
38 | */ |
39 | public static final int DEFAULT_CAPACITY = 4096; |
40 | |
41 | private Map map = Collections.synchronizedMap(new HashMap()); |
42 | |
43 | private int capacity; |
44 | |
45 | /** |
46 | * Create a {@link MapRetryContextCache} with default capacity. |
47 | */ |
48 | public MapRetryContextCache() { |
49 | this(DEFAULT_CAPACITY); |
50 | } |
51 | |
52 | /** |
53 | * @param defaultCapacity |
54 | */ |
55 | public MapRetryContextCache(int defaultCapacity) { |
56 | super(); |
57 | this.capacity = defaultCapacity; |
58 | } |
59 | |
60 | /** |
61 | * Public setter for the capacity. Prevents the cache from growing |
62 | * unboundedly if items that fail are misidentified and two references to an |
63 | * identical item actually do not have the same key. This can happen when |
64 | * users implement equals and hashCode based on mutable fields, for |
65 | * instance. |
66 | * |
67 | * @param capacity the capacity to set |
68 | */ |
69 | public void setCapacity(int capacity) { |
70 | this.capacity = capacity; |
71 | } |
72 | |
73 | public boolean containsKey(Object key) { |
74 | return map.containsKey(key); |
75 | } |
76 | |
77 | public RetryContext get(Object key) { |
78 | return (RetryContext) map.get(key); |
79 | } |
80 | |
81 | public void put(Object key, RetryContext context) { |
82 | if (map.size() >= capacity) { |
83 | throw new RetryCacheCapacityExceededException("Retry cache capacity limit breached. " + |
84 | "Do you need to re-consider the implementation of the key generator, " + |
85 | "or the equals and hashCode of the items that failed?"); |
86 | } |
87 | map.put(key, context); |
88 | } |
89 | |
90 | public void remove(Object key) { |
91 | map.remove(key); |
92 | } |
93 | |
94 | } |