| 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.lang.ref.SoftReference; |
| 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 and its entries are soft-referenced, so may |
| 28 | * be garbage collected under pressure. |
| 29 | * |
| 30 | * @see MapRetryContextCache for non-soft referenced version |
| 31 | * |
| 32 | * @author Dave Syer |
| 33 | * |
| 34 | */ |
| 35 | public class SoftReferenceMapRetryContextCache implements RetryContextCache { |
| 36 | |
| 37 | /** |
| 38 | * Default value for maximum capacity of the cache. This is set to a |
| 39 | * reasonably low value (4096) to avoid users inadvertently filling the |
| 40 | * cache with item keys that are inconsistent. |
| 41 | */ |
| 42 | public static final int DEFAULT_CAPACITY = 4096; |
| 43 | |
| 44 | private Map<Object, SoftReference<RetryContext>> map = Collections |
| 45 | .synchronizedMap(new HashMap<Object, SoftReference<RetryContext>>()); |
| 46 | |
| 47 | private int capacity; |
| 48 | |
| 49 | /** |
| 50 | * Create a {@link SoftReferenceMapRetryContextCache} with default capacity. |
| 51 | */ |
| 52 | public SoftReferenceMapRetryContextCache() { |
| 53 | this(DEFAULT_CAPACITY); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param defaultCapacity |
| 58 | */ |
| 59 | public SoftReferenceMapRetryContextCache(int defaultCapacity) { |
| 60 | super(); |
| 61 | this.capacity = defaultCapacity; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Public setter for the capacity. Prevents the cache from growing |
| 66 | * unboundedly if items that fail are misidentified and two references to an |
| 67 | * identical item actually do not have the same key. This can happen when |
| 68 | * users implement equals and hashCode based on mutable fields, for |
| 69 | * instance. |
| 70 | * |
| 71 | * @param capacity the capacity to set |
| 72 | */ |
| 73 | public void setCapacity(int capacity) { |
| 74 | this.capacity = capacity; |
| 75 | } |
| 76 | |
| 77 | public boolean containsKey(Object key) { |
| 78 | if (!map.containsKey(key)) { |
| 79 | return false; |
| 80 | } |
| 81 | if (map.get(key).get() == null) { |
| 82 | // our reference was garbage collected |
| 83 | map.remove(key); |
| 84 | } |
| 85 | return map.containsKey(key); |
| 86 | } |
| 87 | |
| 88 | public RetryContext get(Object key) { |
| 89 | return map.get(key).get(); |
| 90 | } |
| 91 | |
| 92 | public void put(Object key, RetryContext context) { |
| 93 | if (map.size() >= capacity) { |
| 94 | throw new RetryCacheCapacityExceededException("Retry cache capacity limit breached. " |
| 95 | + "Do you need to re-consider the implementation of the key generator, " |
| 96 | + "or the equals and hashCode of the items that failed?"); |
| 97 | } |
| 98 | map.put(key, new SoftReference<RetryContext>(context)); |
| 99 | } |
| 100 | |
| 101 | public void remove(Object key) { |
| 102 | map.remove(key); |
| 103 | } |
| 104 | } |