RESOLVED - issue BATCH-287: add double job parameter type

http://jira.springframework.org/browse/BATCH-287
This commit is contained in:
robokaso
2008-02-27 11:19:45 +00:00
parent ffcbb68f2a
commit 2f3cf7bec0
14 changed files with 157 additions and 51 deletions

View File

@@ -13,11 +13,13 @@ import java.util.Map.Entry;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* Value object representing runtime parameters to a batch job. Because the parameters have no individual meaning
* outside of the JobIdentifier they are contained within, it is a value object rather than an entity. It is also
* extremely important that a parameters object can be reliably compared to another for equality, in order to determine
* if one JobIdentifier equals another. Furthermore, because these parameters will need to be persisted, it is vital
* that the types added are restricted.
* Value object representing runtime parameters to a batch job. Because the
* parameters have no individual meaning outside of the JobParameters they are
* contained within, it is a value object rather than an entity. It is also
* extremely important that a parameters object can be reliably compared to
* another for equality, in order to determine if one JobParameters object
* equals another. Furthermore, because these parameters will need to be
* persisted, it is vital that the types added are restricted.
*
* @author Lucas Ward
* @since 1.0
@@ -28,34 +30,37 @@ public class JobParameters {
private final Map longMap;
private final Map doubleMap;
private final Map dateMap;
/**
* Default constructor. Creates a new empty JobRuntimeParameters. It should be noted that this constructor should
* only be used if an empty parameters is needed, since JobRuntimeParameters is immutable.
* Default constructor. Creates a new empty JobRuntimeParameters. It should
* be noted that this constructor should only be used if an empty parameters
* is needed, since JobRuntimeParameters is immutable.
*/
public JobParameters() {
this.stringMap = new LinkedHashMap();
this.longMap = new LinkedHashMap();
this.doubleMap = new LinkedHashMap();
this.dateMap = new LinkedHashMap();
}
/**
* Create a new parameters object based upon three maps for each of the three data types. See
* {@link JobParametersBuilder} for an easier way to create parameters.
*
* @param stringMap
* @param longMap
* @param dateMap
* Create a new parameters object based upon the maps for each of the
* supported data types. See {@link JobParametersBuilder} for an easier way
* to create parameters.
*/
public JobParameters(Map stringMap, Map longMap, Map dateMap) {
public JobParameters(Map stringMap, Map longMap, Map doubleMap, Map dateMap) {
super();
validateMap(stringMap, String.class);
validateMap(longMap, Long.class);
validateMap(doubleMap, Double.class);
validateMap(dateMap, Date.class);
this.stringMap = new LinkedHashMap(stringMap);
this.longMap = new LinkedHashMap(longMap);
this.doubleMap = new LinkedHashMap(doubleMap);
this.dateMap = copyDateMap(dateMap);
}
@@ -79,6 +84,16 @@ public class JobParameters {
return (Long) longMap.get(key);
}
/**
* Typesafe Getter for the Long represented by the provided key.
*
* @param key The key to get a value for
* @return The <code>Double</code> value
*/
public Double getDouble(String key) {
return (Double) doubleMap.get(key);
}
/**
* Typesafe Getter for the Date represented by the provided key.
*
@@ -90,14 +105,16 @@ public class JobParameters {
}
/**
* Get a map of all parameters, including string, long, and date. It should be noted that a
* Collections$UnmodifiableMap is returned, ensuring immutability.
* Get a map of all parameters, including string, long, and date. It should
* be noted that a Collections$UnmodifiableMap is returned, ensuring
* immutability.
*
* @return an unmodifiable map containing all parameters.
*/
public Map getParameters() {
Map tempMap = new LinkedHashMap(stringMap);
tempMap.putAll(longMap);
tempMap.putAll(doubleMap);
tempMap.putAll(dateMap);
return Collections.unmodifiableMap(tempMap);
}
@@ -120,6 +137,15 @@ public class JobParameters {
return Collections.unmodifiableMap(longMap);
}
/**
* Get a map of only Double parameters
*
* @return long parameters.
*/
public Map getDoubleParameters() {
return Collections.unmodifiableMap(doubleMap);
}
/**
* Get a map of only Date parameters
*
@@ -133,12 +159,12 @@ public class JobParameters {
* @return true if the prameters is empty, false otherwise.
*/
public boolean isEmpty() {
return (dateMap.isEmpty() && longMap.isEmpty() && stringMap.isEmpty());
return (dateMap.isEmpty() && longMap.isEmpty() && doubleMap.isEmpty() && stringMap.isEmpty());
}
/*
* Convenience method for validating that a the provided map only contains a particular type as a value, with only a
* String as a key.
* Convenience method for validating that a the provided map only contains a
* particular type as a value, with only a String as a key.
*/
private void validateMap(Map map, Class type) {
@@ -181,20 +207,23 @@ public class JobParameters {
JobParameters parameters = (JobParameters) obj;
// Since the type contained by each map is known, it's safe to call Map.equals()
// Since the type contained by each map is known, it's safe to call
// Map.equals()
if (getParameters().equals(parameters.getParameters())) {
return true;
} else {
}
else {
return false;
}
}
public int hashCode() {
return new HashCodeBuilder(7, 21).append(stringMap).append(longMap).append(dateMap).toHashCode();
return new HashCodeBuilder(7, 21).append(stringMap).append(longMap).append(doubleMap).append(dateMap)
.toHashCode();
}
public String toString() {
return stringMap.toString() + longMap.toString() + dateMap.toString();
return stringMap.toString() + longMap.toString() + doubleMap.toString() + dateMap.toString();
}
}

View File

@@ -26,6 +26,8 @@ public class JobParametersBuilder {
private final Map stringMap;
private final Map longMap;
private final Map doubleMap;
private final Map dateMap;
@@ -36,6 +38,7 @@ public class JobParametersBuilder {
this.stringMap = new LinkedHashMap();
this.longMap = new LinkedHashMap();
this.doubleMap = new LinkedHashMap();
this.dateMap = new LinkedHashMap();
}
@@ -70,13 +73,26 @@ public class JobParametersBuilder {
*
* @param key - parameter accessor.
* @param parameter - runtime parameter
* @return a refernece to this object.
* @return a reference to this object.
*/
public JobParametersBuilder addLong(String key, Long parameter) {
Assert.notNull(parameter, "Parameter must not be null.");
longMap.put(key, parameter);
return this;
}
/**
* Add a new Double parameter for the given key.
*
* @param key - parameter accessor.
* @param parameter - runtime parameter
* @return a reference to this object.
*/
public JobParametersBuilder addDouble(String key, Double parameter) {
Assert.notNull(parameter, "Parameter must not be null.");
doubleMap.put(key, parameter);
return this;
}
/**
* Conversion method that takes the current state of this builder and
@@ -85,6 +101,6 @@ public class JobParametersBuilder {
* @return a valid JobRuntimeParameters object.
*/
public JobParameters toJobParameters() {
return new JobParameters(stringMap, longMap, dateMap);
return new JobParameters(stringMap, longMap, doubleMap, dateMap);
}
}

View File

@@ -24,6 +24,8 @@ public class JobParametersTests extends TestCase {
Map longMap;
Map dateMap;
Map doubleMap;
Date date1 = new Date(4321431242L);
@@ -43,12 +45,16 @@ public class JobParametersTests extends TestCase {
longMap = new HashMap();
longMap.put("long.key1", new Long(1));
longMap.put("long.key2", new Long(2));
doubleMap = new HashMap();
doubleMap.put("double.key1", new Double(1.1));
doubleMap.put("double.key2", new Double(2.2));
dateMap = new HashMap();
dateMap.put("date.key1", date1);
dateMap.put("date.key2", date2);
return new JobParameters(stringMap, longMap, dateMap);
return new JobParameters(stringMap, longMap, doubleMap, dateMap);
}
public void testBadLongKeyException() throws Exception {
@@ -57,7 +63,7 @@ public class JobParametersTests extends TestCase {
badLongMap.put(new Long(0), new Long(1));
try {
new JobParameters(stringMap, badLongMap, dateMap);
new JobParameters(stringMap, badLongMap, doubleMap, dateMap);
fail();
}
catch (IllegalArgumentException ex) {
@@ -71,7 +77,21 @@ public class JobParametersTests extends TestCase {
badLongMap.put("key", "bad long");
try {
new JobParameters(stringMap, badLongMap, dateMap);
new JobParameters(stringMap, badLongMap, doubleMap, dateMap);
fail();
}
catch (IllegalArgumentException ex) {
// expected
}
}
public void testBadDoubleConstructorException() throws Exception {
Map badDoubleMap = new HashMap();
badDoubleMap.put("key", "bad double");
try {
new JobParameters(stringMap, longMap, badDoubleMap, dateMap);
fail();
}
catch (IllegalArgumentException ex) {
@@ -85,7 +105,7 @@ public class JobParametersTests extends TestCase {
badMap.put("key", new Integer(2));
try {
new JobParameters(badMap, longMap, dateMap);
new JobParameters(badMap, longMap, doubleMap, dateMap);
fail();
}
catch (IllegalArgumentException ex) {
@@ -99,7 +119,7 @@ public class JobParametersTests extends TestCase {
badMap.put("key", new java.sql.Date(System.currentTimeMillis()));
try {
new JobParameters(stringMap, longMap, badMap);
new JobParameters(stringMap, longMap, doubleMap, badMap);
fail();
}
catch (IllegalArgumentException ex) {
@@ -126,6 +146,16 @@ public class JobParametersTests extends TestCase {
assertEquals(new Long(1), parameters.getLongParameters().get("long.key1"));
assertEquals(new Long(2), parameters.getLongParameters().get("long.key2"));
}
public void testGetDouble() {
assertEquals(new Double(1.1), parameters.getDouble("double.key1"));
assertEquals(new Double(2.2), parameters.getDouble("double.key2"));
}
public void testGetDoubleParameters() {
assertEquals(new Double(1.1), parameters.getDoubleParameters().get("double.key1"));
assertEquals(new Double(2.2), parameters.getDoubleParameters().get("double.key2"));
}
public void testGetDate() {
assertEquals(date1, parameters.getDate("date.key1"));
@@ -184,12 +214,16 @@ public class JobParametersTests extends TestCase {
longMap = new HashMap();
longMap.put("long.key2", new Long(2));
longMap.put("long.key1", new Long(1));
doubleMap = new HashMap();
doubleMap.put("double.key2", new Double(2.2));
doubleMap.put("double.key1", new Double(1.1));
dateMap = new HashMap();
dateMap.put("date.key2", date2);
dateMap.put("date.key1", date1);
JobParameters testProps = new JobParameters(stringMap, longMap, dateMap);
JobParameters testProps = new JobParameters(stringMap, longMap, doubleMap, dateMap);
props = testProps.getParameters();
stringBuilder = new StringBuilder();

View File

@@ -32,11 +32,11 @@ import org.springframework.util.Assert;
*/
public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements JobInstanceDao, InitializingBean {
private static final String CREATE_JOB = "INSERT into %PREFIX%JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY)"
private static final String CREATE_JOB_INSTANCE = "INSERT into %PREFIX%JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY)"
+ " values (?, ?, ?)";
private static final String CREATE_JOB_PARAMETERS = "INSERT into %PREFIX%JOB_PARAMS(JOB_INSTANCE_ID, KEY_NAME, TYPE_CD, "
+ "STRING_VAL, DATE_VAL, LONG_VAL) values (?, ?, ?, ?, ?, ?)";
+ "STRING_VAL, DATE_VAL, LONG_VAL, DOUBLE_VAL) values (?, ?, ?, ?, ?, ?, ?)";
private static final String FIND_JOBS = "SELECT JOB_INSTANCE_ID from %PREFIX%JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?";
@@ -59,7 +59,7 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
Long jobId = new Long(jobIncrementer.nextLongValue());
Object[] parameters = new Object[] { jobId, job.getName(), createJobKey(jobParameters) };
getJdbcTemplate().update(getQuery(CREATE_JOB), parameters,
getJdbcTemplate().update(getQuery(CREATE_JOB_INSTANCE), parameters,
new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR });
insertJobParameters(jobId, jobParameters);
@@ -104,6 +104,15 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
insertParameter(jobId, ParameterType.LONG, entry.getKey().toString(), entry.getValue());
}
}
parameters = jobParameters.getDoubleParameters();
if (!parameters.isEmpty()) {
for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) {
Entry entry = (Entry) it.next();
insertParameter(jobId, ParameterType.DOUBLE, entry.getKey().toString(), entry.getValue());
}
}
parameters = jobParameters.getDateParameters();
@@ -123,16 +132,19 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
Object[] args = new Object[0];
int[] argTypes = new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP,
Types.INTEGER };
Types.INTEGER, Types.DOUBLE };
if (type == ParameterType.STRING) {
args = new Object[] { jobId, key, type, value, new Timestamp(0L), new Long(0) };
args = new Object[] { jobId, key, type, value, new Timestamp(0L), new Long(0), new Double(0) };
}
else if (type == ParameterType.LONG) {
args = new Object[] { jobId, key, type, "", new Timestamp(0L), value };
args = new Object[] { jobId, key, type, "", new Timestamp(0L), value, new Double(0) };
}
else if (type == ParameterType.DOUBLE) {
args = new Object[] { jobId, key, type, "", new Timestamp(0L), new Long(0), value };
}
else if (type == ParameterType.DATE) {
args = new Object[] { jobId, key, type, "", value, new Long(0) };
args = new Object[] { jobId, key, type, "", value, new Long(0), new Double(0) };
}
getJdbcTemplate().update(getQuery(CREATE_JOB_PARAMETERS), args, argTypes);
@@ -196,8 +208,10 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
public static final ParameterType DATE = new ParameterType("DATE");
public static final ParameterType LONG = new ParameterType("LONG");
public static final ParameterType DOUBLE = new ParameterType("DOUBLE");
private static final ParameterType[] VALUES = { STRING, DATE, LONG };
private static final ParameterType[] VALUES = { STRING, DATE, LONG, DOUBLE };
public static ParameterType getType(String typeAsString) {

View File

@@ -33,7 +33,8 @@ CREATE TABLE BATCH_JOB_PARAMS (
KEY_NAME VARCHAR(100) NOT NULL ,
STRING_VAL VARCHAR(250) ,
DATE_VAL TIMESTAMP ,
LONG_VAL BIGINT );
LONG_VAL BIGINT ,
DOUBLE_VAL DOUBLE PRECISION);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT PRIMARY KEY ,

View File

@@ -33,7 +33,8 @@ CREATE TABLE BATCH_JOB_PARAMS (
KEY_NAME VARCHAR(100) NOT NULL ,
STRING_VAL VARCHAR(250) ,
DATE_VAL TIMESTAMP ,
LONG_VAL BIGINT );
LONG_VAL BIGINT ,
DOUBLE_VAL DOUBLE PRECISION);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,

View File

@@ -33,7 +33,8 @@ CREATE TABLE BATCH_JOB_PARAMS (
KEY_NAME VARCHAR(100) NOT NULL ,
STRING_VAL VARCHAR(250) ,
DATE_VAL TIMESTAMP ,
LONG_VAL BIGINT );
LONG_VAL BIGINT ,
DOUBLE_VAL DOUBLE PRECISION);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT IDENTITY PRIMARY KEY ,

View File

@@ -33,7 +33,8 @@ CREATE TABLE BATCH_JOB_PARAMS (
KEY_NAME VARCHAR(100) NOT NULL ,
STRING_VAL VARCHAR(250) ,
DATE_VAL TIMESTAMP ,
LONG_VAL BIGINT );
LONG_VAL BIGINT ,
DOUBLE_VAL DOUBLE PRECISION);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT unsigned PRIMARY KEY ,

View File

@@ -33,7 +33,8 @@ CREATE TABLE BATCH_JOB_PARAMS (
KEY_NAME VARCHAR(100) NOT NULL ,
STRING_VAL VARCHAR(250) ,
DATE_VAL TIMESTAMP ,
LONG_VAL NUMBER(38) );
LONG_VAL NUMBER(38) ,
DOUBLE_VAL DOUBLE PRECISION);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID NUMBER(38) PRIMARY KEY ,

View File

@@ -33,7 +33,8 @@ CREATE TABLE BATCH_JOB_PARAMS (
KEY_NAME VARCHAR(100) NOT NULL ,
STRING_VAL VARCHAR(250) ,
DATE_VAL TIMESTAMP ,
LONG_VAL BIGINT );
LONG_VAL BIGINT ,
DOUBLE_VAL DOUBLE PRECISION);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT PRIMARY KEY ,

View File

@@ -22,7 +22,8 @@ CREATE TABLE BATCH_JOB_PARAMS (
KEY_NAME VARCHAR(100) NOT NULL ,
STRING_VAL VARCHAR(250) ,
DATE_VAL TIMESTAMP ,
LONG_VAL ${BIGINT} );
LONG_VAL ${BIGINT} ,
DOUBLE_VAL ${DOUBLE});
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID ${BIGINT} $!{IDENTITY} PRIMARY KEY $!{GENERATED},

View File

@@ -46,12 +46,17 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa
put("longKey", new Long(1));
}
};
Map doubleParams = new HashMap() {
{
put("doubleKey", new Double(1.1));
}
};
Map dateParams = new HashMap() {
{
put("dateKey", new Date(1));
}
};
JobParameters jobParams = new JobParameters(stringParams, longParams, dateParams);
JobParameters jobParams = new JobParameters(stringParams, longParams, doubleParams, dateParams);
JobExecution firstExecution = jobRepository.createJobExecution(job, jobParams);
firstExecution.setStartTime(new Date());

View File

@@ -42,8 +42,8 @@ public abstract class AbstractJobDaoTests extends AbstractTransactionalDataSourc
protected JobExecutionDao jobExecutionDao;
protected JobParameters jobParameters = new JobParametersBuilder().addString("job.key", "jobKey").
addLong("long", new Long(1)).addDate("date", new Date(7)).toJobParameters();
protected JobParameters jobParameters = new JobParametersBuilder().addString("job.key", "jobKey").addLong("long",
new Long(1)).addDate("date", new Date(7)).addDouble("double", new Double(7.7)).toJobParameters();
protected JobInstance jobInstance;
@@ -226,7 +226,7 @@ public abstract class AbstractJobDaoTests extends AbstractTransactionalDataSourc
}
public void testJobWithDefaultJobIdentifier() throws Exception {
Job testDefaultJob = new JobSupport("testDefault");
// Create job.
jobInstance = jobInstanceDao.createJobInstance(testDefaultJob, jobParameters);

View File

@@ -22,7 +22,8 @@ CREATE TABLE BATCH_JOB_PARAMS (
KEY_NAME VARCHAR(100) NOT NULL ,
STRING_VAL VARCHAR(250) ,
DATE_VAL TIMESTAMP ,
LONG_VAL BIGINT );
LONG_VAL BIGINT ,
DOUBLE_VAL DOUBLE PRECISION);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT IDENTITY PRIMARY KEY ,