Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2006-2007 the original author or authors.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.springframework.batch.retry.policy;
 import java.util.Map;
Map-based implementation of RetryContextCache. The map backing the cache of contexts is synchronized and its entries are soft-referenced, so may be garbage collected under pressure.

Dave Syer
See also:
MapRetryContextCache for non-soft referenced version
 public class SoftReferenceMapRetryContextCache implements RetryContextCache {

Default value for maximum capacity of the cache. This is set to a reasonably low value (4096) to avoid users inadvertently filling the cache with item keys that are inconsistent.
 	public static final int DEFAULT_CAPACITY = 4096;
 	private Map<ObjectSoftReference<RetryContext>> map = Collections
 	private int capacity;

Create a SoftReferenceMapRetryContextCache with default capacity.

 	public SoftReferenceMapRetryContextCache(int defaultCapacity) {
 		this. = defaultCapacity;

Public setter for the capacity. Prevents the cache from growing unboundedly if items that fail are misidentified and two references to an identical item actually do not have the same key. This can happen when users implement equals and hashCode based on mutable fields, for instance.

capacity the capacity to set
 	public void setCapacity(int capacity) {
 		this. = capacity;
 	public boolean containsKey(Object key) {
 		if (!.containsKey(key)) {
 			return false;
 		if (.get(key).get() == null) {
 			// our reference was garbage collected
 		return .containsKey(key);
 	public RetryContext get(Object key) {
 		return .get(key).get();
 	public void put(Object keyRetryContext context) {
 		if (.size() >= ) {
 			throw new RetryCacheCapacityExceededException("Retry cache capacity limit breached. "
 					+ "Do you need to re-consider the implementation of the key generator, "
 					+ "or the equals and hashCode of the items that failed?");
 		.put(keynew SoftReference<RetryContext>(context));
	public void remove(Object key) {
New to GrepCode? Check out our FAQ X