RESOLVED - issue BATCH-447: ItemOrientedStep implementation causes mandatory dependency on backport-util-concurrent

Added AtomicCounter to remove backport dependency
This commit is contained in:
dsyer
2008-03-26 19:43:19 +00:00
parent ca2db2c31a
commit 76991edce7
5 changed files with 130 additions and 6 deletions

View File

@@ -0,0 +1,35 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.repeat.context;
/**
* @author Dave Syer
*
*/
interface AtomicCounter {
/**
* Atomic addition.
* @param delta the delta to add to the value
*/
void addAndGet(int delta);
/**
* @return the current value
*/
int intValue();
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2002-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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.repeat.context;
import org.springframework.core.JdkVersion;
import org.springframework.util.ClassUtils;
/**
* A factory that properly determines which version of the {@link AtomicCounter}
* to return based on the availability of Java 5 or Backport Concurrent.
*
* @author Dave Syer
*/
class AtomicCounterFactory {
/** Whether the backport-concurrent library is present on the classpath */
private static final boolean backportConcurrentAvailable = ClassUtils.isPresent(
"edu.emory.mathcs.backport.java.util.concurrent.Semaphore", AtomicCounterFactory.class.getClassLoader());
private final AtomicCounter counter;
public AtomicCounterFactory() {
if (JdkVersion.isAtLeastJava15()) {
counter = new JdkConcurrentAtomicCounter();
}
else if (backportConcurrentAvailable) {
counter = new BackportConcurrentAtomicCounter();
}
else {
throw new IllegalStateException("Cannot create AtomicCounter - "
+ "neither JDK 1.5 nor backport-concurrent available on the classpath");
}
}
public AtomicCounter getAtomicCounter() {
return counter;
}
}

View File

@@ -0,0 +1,19 @@
package org.springframework.batch.repeat.context;
/**
* @author Dave Syer
*
*/
class BackportConcurrentAtomicCounter implements AtomicCounter {
private edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger counter = new edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger();
public void addAndGet(int delta) {
counter.addAndGet(delta);
}
public int intValue() {
return counter.intValue();
}
}

View File

@@ -0,0 +1,19 @@
package org.springframework.batch.repeat.context;
/**
* @author Dave Syer
*
*/
class JdkConcurrentAtomicCounter implements AtomicCounter {
private java.util.concurrent.atomic.AtomicInteger counter = new java.util.concurrent.atomic.AtomicInteger();
public void addAndGet(int delta) {
counter.addAndGet(delta);
}
public int intValue() {
return counter.intValue();
}
}

View File

@@ -19,8 +19,6 @@ package org.springframework.batch.repeat.context;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.util.Assert;
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
/**
* Helper class for policies that need to count the number of occurrences of
* some event (e.g. an exception type in the context) in the scope of a batch.
@@ -49,7 +47,7 @@ public class RepeatContextCounter {
* @param delta the amount by which to increment the counter.
*/
final public void increment(int delta) {
AtomicInteger count = getCounter();
AtomicCounter count = getCounter();
count.addAndGet(delta);
}
@@ -96,7 +94,8 @@ public class RepeatContextCounter {
this.context = context;
}
if (!this.context.hasAttribute(countKey)) {
this.context.setAttribute(countKey, new AtomicInteger(0));
AtomicCounterFactory factory = new AtomicCounterFactory();
this.context.setAttribute(countKey, factory.getAtomicCounter());
}
}
@@ -108,8 +107,8 @@ public class RepeatContextCounter {
return getCounter().intValue();
}
private AtomicInteger getCounter() {
return ((AtomicInteger) context.getAttribute(countKey));
private AtomicCounter getCounter() {
return ((AtomicCounter) context.getAttribute(countKey));
}
}