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 | package org.springframework.batch.retry.policy; |
17 | |
18 | import java.util.Collections; |
19 | import java.util.HashMap; |
20 | import java.util.Map; |
21 | |
22 | import org.springframework.batch.retry.RetryContext; |
23 | |
24 | /** |
25 | * Map-based implementation of {@link RetryContextCache}. The map backing the |
26 | * cache of contexts is synchronized. |
27 | * |
28 | * @author Dave Syer |
29 | * |
30 | */ |
31 | public class MapRetryContextCache implements RetryContextCache { |
32 | |
33 | /** |
34 | * Default value for maximum capacity of the cache. This is set to a |
35 | * reasonably low value (4096) to avoid users inadvertently filling the |
36 | * cache with item keys that are inconsistent. |
37 | */ |
38 | public static final int DEFAULT_CAPACITY = 4096; |
39 | |
40 | private Map<Object, RetryContext> map = Collections.synchronizedMap(new HashMap<Object, RetryContext>()); |
41 | |
42 | private int capacity; |
43 | |
44 | /** |
45 | * Create a {@link MapRetryContextCache} with default capacity. |
46 | */ |
47 | public MapRetryContextCache() { |
48 | this(DEFAULT_CAPACITY); |
49 | } |
50 | |
51 | /** |
52 | * @param defaultCapacity |
53 | */ |
54 | public MapRetryContextCache(int defaultCapacity) { |
55 | super(); |
56 | this.capacity = defaultCapacity; |
57 | } |
58 | |
59 | /** |
60 | * Public setter for the capacity. Prevents the cache from growing |
61 | * unboundedly if items that fail are misidentified and two references to an |
62 | * identical item actually do not have the same key. This can happen when |
63 | * users implement equals and hashCode based on mutable fields, for |
64 | * instance. |
65 | * |
66 | * @param capacity the capacity to set |
67 | */ |
68 | public void setCapacity(int capacity) { |
69 | this.capacity = capacity; |
70 | } |
71 | |
72 | public boolean containsKey(Object key) { |
73 | return map.containsKey(key); |
74 | } |
75 | |
76 | public RetryContext get(Object key) { |
77 | return map.get(key); |
78 | } |
79 | |
80 | public void put(Object key, RetryContext context) { |
81 | if (map.size() >= capacity) { |
82 | throw new RetryCacheCapacityExceededException("Retry cache capacity limit breached. " |
83 | + "Do you need to re-consider the implementation of the key generator, " |
84 | + "or the equals and hashCode of the items that failed?"); |
85 | } |
86 | map.put(key, context); |
87 | } |
88 | |
89 | public void remove(Object key) { |
90 | map.remove(key); |
91 | } |
92 | } |