BATCH-84: Modified JobIdentifier interface to include new JobRuntimeParameters value object, and modified its subclasses accordingly.
This commit is contained in:
@@ -15,19 +15,20 @@
|
||||
*/
|
||||
package org.springframework.batch.execution.runtime;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang.builder.HashCodeBuilder;
|
||||
import org.springframework.batch.core.domain.JobIdentifier;
|
||||
import org.springframework.batch.core.domain.JobRuntimeParameters;
|
||||
import org.springframework.batch.core.domain.JobRuntimeParametersBuilder;
|
||||
import org.springframework.batch.core.runtime.SimpleJobIdentifier;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class DefaultJobIdentifier extends SimpleJobIdentifier implements
|
||||
JobIdentifier {
|
||||
|
||||
private String key = "";
|
||||
public static final String JOB_KEY = "job.key";
|
||||
|
||||
/**
|
||||
* Default constructor package access only.
|
||||
@@ -47,39 +48,15 @@ public class DefaultJobIdentifier extends SimpleJobIdentifier implements
|
||||
* @param name the name for the job
|
||||
*/
|
||||
public DefaultJobIdentifier(String name, String key) {
|
||||
this(name);
|
||||
this.key = key;
|
||||
this(name, new JobRuntimeParametersBuilder().addString(JOB_KEY, key).toJobRuntimeParameters());
|
||||
}
|
||||
|
||||
public DefaultJobIdentifier(String name, JobRuntimeParameters parameters){
|
||||
super(name, parameters);
|
||||
}
|
||||
|
||||
public String getJobKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setJobKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds the key data to the base class.
|
||||
*
|
||||
* @see org.springframework.batch.core.runtime.SimpleJobIdentifier#toString()
|
||||
*/
|
||||
public String toString() {
|
||||
return super.toString() + ",key=" + key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the provided JobIdentifier equals this JobIdentifier. Two
|
||||
* Identifiers are considered to be equal if they have the same name,
|
||||
* stream, run, and schedule date.
|
||||
*/
|
||||
public boolean equals(Object other) {
|
||||
return EqualsBuilder.reflectionEquals(this, other) || EqualsBuilder.reflectionEquals(other, this);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
return getRuntimeParameters().getString(JOB_KEY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,8 +32,7 @@ public class DefaultJobIdentifierFactory implements JobIdentifierFactory {
|
||||
|
||||
public JobIdentifier getJobIdentifier(String name) {
|
||||
|
||||
DefaultJobIdentifier runtimeInformation = new DefaultJobIdentifier(name);
|
||||
runtimeInformation.setJobKey(key);
|
||||
DefaultJobIdentifier runtimeInformation = new DefaultJobIdentifier(name, key);
|
||||
return runtimeInformation;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,20 +16,14 @@
|
||||
|
||||
package org.springframework.batch.execution.runtime;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang.builder.HashCodeBuilder;
|
||||
import org.springframework.batch.core.domain.JobIdentifier;
|
||||
import org.springframework.batch.core.domain.JobRuntimeParametersBuilder;
|
||||
|
||||
public class ScheduledJobIdentifier extends DefaultJobIdentifier implements JobIdentifier {
|
||||
|
||||
private static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
|
||||
|
||||
private Date scheduleDate;
|
||||
|
||||
public static final String SCHEDULE_DATE = "schedule.date";
|
||||
|
||||
ScheduledJobIdentifier() {
|
||||
this(null);
|
||||
@@ -37,7 +31,6 @@ public class ScheduledJobIdentifier extends DefaultJobIdentifier implements JobI
|
||||
|
||||
public ScheduledJobIdentifier(String name) {
|
||||
super(name);
|
||||
initDate();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,41 +39,18 @@ public class ScheduledJobIdentifier extends DefaultJobIdentifier implements JobI
|
||||
*/
|
||||
public ScheduledJobIdentifier(String name, String key) {
|
||||
super(name, key);
|
||||
initDate();
|
||||
}
|
||||
|
||||
private void initDate() {
|
||||
try {
|
||||
scheduleDate = dateFormat.parse("19700101");
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalStateException("Could not parse trivial date 19700101");
|
||||
}
|
||||
|
||||
public ScheduledJobIdentifier(String name, Date scheduleDate){
|
||||
super(name, new JobRuntimeParametersBuilder().addDate(SCHEDULE_DATE, scheduleDate).toJobRuntimeParameters());
|
||||
}
|
||||
|
||||
public ScheduledJobIdentifier(String name, String jobKey, Date scheduleDate){
|
||||
super(name, new JobRuntimeParametersBuilder().addString(ScheduledJobIdentifier.JOB_KEY, jobKey).
|
||||
addDate(SCHEDULE_DATE, scheduleDate).toJobRuntimeParameters());
|
||||
}
|
||||
|
||||
public Date getScheduleDate() {
|
||||
return scheduleDate;
|
||||
return getRuntimeParameters().getDate(SCHEDULE_DATE);
|
||||
}
|
||||
|
||||
public void setScheduleDate(Date scheduleDate) {
|
||||
this.scheduleDate = scheduleDate;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return super.toString() + ",scheduleDate="
|
||||
+ scheduleDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the provided JobIdentifier equals this JobIdentifier. Two
|
||||
* Identifiers are considered to be equal if they have the same name,
|
||||
* stream, run, and schedule date.
|
||||
*/
|
||||
public boolean equals(Object other) {
|
||||
return EqualsBuilder.reflectionEquals(this, other) || EqualsBuilder.reflectionEquals(other, this);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.batch.execution.runtime;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.batch.core.domain.JobIdentifier;
|
||||
@@ -30,13 +33,14 @@ import org.springframework.batch.core.runtime.JobIdentifierFactory;
|
||||
*/
|
||||
public class ScheduledJobIdentifierFactory extends DefaultJobIdentifierFactory implements JobIdentifierFactory {
|
||||
|
||||
private Date scheduleDate = new Date();
|
||||
private Date scheduleDate;
|
||||
|
||||
private DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
|
||||
|
||||
public JobIdentifier getJobIdentifier(String name) {
|
||||
|
||||
ScheduledJobIdentifier identifier = new ScheduledJobIdentifier(name);
|
||||
identifier.setJobKey(key);
|
||||
identifier.setScheduleDate(scheduleDate);
|
||||
initDate();
|
||||
ScheduledJobIdentifier identifier = new ScheduledJobIdentifier(name, key, scheduleDate);
|
||||
return identifier;
|
||||
}
|
||||
|
||||
@@ -44,4 +48,15 @@ public class ScheduledJobIdentifierFactory extends DefaultJobIdentifierFactory i
|
||||
this.scheduleDate = scheduleDate;
|
||||
}
|
||||
|
||||
public void setDateFormat(DateFormat dateFormat){
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
private void initDate() {
|
||||
try {
|
||||
scheduleDate = dateFormat.parse("19700101");
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalStateException("Could not parse trivial date 19700101");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class SimpleJobExecutorFacadeTests extends TestCase {
|
||||
|
||||
private volatile boolean running = false;
|
||||
|
||||
private SimpleJobIdentifier jobIdentifier = new SimpleJobIdentifier();
|
||||
private SimpleJobIdentifier jobIdentifier = new SimpleJobIdentifier("TestJob");
|
||||
|
||||
private JobExecution jobExecution = new JobExecution(new JobInstance(
|
||||
jobIdentifier));
|
||||
|
||||
@@ -64,11 +64,8 @@ public abstract class AbstractJobDaoTests extends
|
||||
}
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
jobRuntimeInformation = new ScheduledJobIdentifier("Job1");
|
||||
jobRuntimeInformation.setName("Job1");
|
||||
jobRuntimeInformation.setJobKey("TestStream");
|
||||
jobRuntimeInformation.setScheduleDate(new SimpleDateFormat("yyyyMMdd")
|
||||
.parse("20070505"));
|
||||
jobRuntimeInformation = new ScheduledJobIdentifier("Job1", "TestStream",
|
||||
new SimpleDateFormat("yyyyMMdd").parse("20070505"));
|
||||
|
||||
// Create job.
|
||||
job = jobDao.createJob(jobRuntimeInformation);
|
||||
@@ -97,7 +94,7 @@ public abstract class AbstractJobDaoTests extends
|
||||
|
||||
public void testFindNonExistentJob() {
|
||||
// No job should be found since it hasn't been created.
|
||||
List jobs = jobDao.findJobs(new ScheduledJobIdentifier("Job2"));
|
||||
List jobs = jobDao.findJobs(new ScheduledJobIdentifier("Job2", "TestStream", new Date()));
|
||||
assertTrue(jobs.size() == 0);
|
||||
}
|
||||
|
||||
@@ -129,15 +126,14 @@ public abstract class AbstractJobDaoTests extends
|
||||
*/
|
||||
public void testCreateJobWithExistingName() {
|
||||
ScheduledJobIdentifier scheduledIdentifier = new ScheduledJobIdentifier(
|
||||
"ScheduledJob");
|
||||
"ScheduledJob", "key", new Date());
|
||||
|
||||
jobDao.createJob(scheduledIdentifier);
|
||||
|
||||
// Modifying the key should bring back a completely different
|
||||
// JobInstance
|
||||
ScheduledJobIdentifier newIdentifier = new ScheduledJobIdentifier(
|
||||
"ScheduledJob");
|
||||
newIdentifier.setJobKey("different key");
|
||||
"ScheduledJob", "different key", new Date());
|
||||
|
||||
List jobs;
|
||||
jobs = jobDao.findJobs(scheduledIdentifier);
|
||||
@@ -242,7 +238,7 @@ public abstract class AbstractJobDaoTests extends
|
||||
public void testZeroExecutionCount() {
|
||||
|
||||
JobInstance testJob = jobDao.createJob(new ScheduledJobIdentifier(
|
||||
"TestJob"));
|
||||
"TestJob", "key", new Date()));
|
||||
// no jobExecutions saved for new job, count should be 0
|
||||
assertEquals(jobDao.getJobExecutionCount(testJob.getId()), 0);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour
|
||||
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpInTransaction()
|
||||
*/
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
JobIdentifier jobIdentifier = new ScheduledJobIdentifier("TestJob");
|
||||
JobIdentifier jobIdentifier = new ScheduledJobIdentifier("TestJob", "TestStream", new Date());
|
||||
job = jobDao.createJob(jobIdentifier);
|
||||
step1 = stepDao.createStep(job, "TestStep1");
|
||||
step2 = stepDao.createStep(job, "TestStep2");
|
||||
|
||||
@@ -65,11 +65,8 @@ public class BatchResourceFactoryBeanTests extends TestCase {
|
||||
|
||||
resourceFactory.setRootDirectory(rootDir);
|
||||
|
||||
identifier = new ScheduledJobIdentifier("testJob");
|
||||
// define mock behaviour
|
||||
identifier.setScheduleDate(new SimpleDateFormat("yyyyMMdd")
|
||||
.parse("20070730"));
|
||||
identifier.setJobKey("testStream");
|
||||
identifier = new ScheduledJobIdentifier("testJob", "testStream", new SimpleDateFormat("yyyyMMdd")
|
||||
.parse("20070730"));
|
||||
|
||||
SimpleStepContext context = new SimpleStepContext();
|
||||
JobInstance job = new JobInstance(identifier);
|
||||
|
||||
@@ -37,10 +37,8 @@ public class DefaultJobIdentifierLabelGeneratorTests extends TestCase {
|
||||
* Test method for {@link org.springframework.batch.execution.resource.DefaultJobIdentifierLabelGenerator#getLabel()}.
|
||||
*/
|
||||
public void testGetLabelWithAllProperties() throws Exception {
|
||||
ScheduledJobIdentifier identifier = new ScheduledJobIdentifier(null);
|
||||
identifier.setName("foo");
|
||||
identifier.setJobKey("bar");
|
||||
identifier.setScheduleDate(new SimpleDateFormat("yyyyMMdd").parse("20070730"));
|
||||
ScheduledJobIdentifier identifier = new ScheduledJobIdentifier("foo", "bar",
|
||||
new SimpleDateFormat("yyyyMMdd").parse("20070730"));
|
||||
assertEquals("foo-bar-20070730", instance.getLabel(identifier));
|
||||
}
|
||||
|
||||
|
||||
@@ -25,32 +25,20 @@ public class DefaultJobIdentifierTests extends TestCase {
|
||||
|
||||
private DefaultJobIdentifier instance = new DefaultJobIdentifier(null);
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getName()}.
|
||||
*/
|
||||
public void testDefaultConstructor() {
|
||||
instance = new DefaultJobIdentifier();
|
||||
assertEquals(null, instance.getName());
|
||||
instance.setName("foo");
|
||||
assertEquals("foo", instance.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getName()}.
|
||||
*/
|
||||
public void testGetName() {
|
||||
assertEquals(null, instance.getName());
|
||||
instance.setName("foo");
|
||||
assertEquals("foo", instance.getName());
|
||||
DefaultJobIdentifier identifier = new DefaultJobIdentifier("foo");
|
||||
assertEquals("foo", identifier.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getJobKey()}.
|
||||
*/
|
||||
public void testGetJobStream() {
|
||||
assertEquals("", instance.getJobKey());
|
||||
instance.setJobKey("foo");
|
||||
assertEquals("foo", instance.getJobKey());
|
||||
DefaultJobIdentifier identifier = new DefaultJobIdentifier("bar", "foo");
|
||||
assertEquals("foo", identifier.getJobKey());
|
||||
}
|
||||
|
||||
|
||||
@@ -59,25 +47,17 @@ public class DefaultJobIdentifierTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testEqualsInstanceWithSameProperties() throws Exception {
|
||||
DefaultJobIdentifier other = new DefaultJobIdentifier(instance.getName());
|
||||
other.setJobKey(instance.getJobKey());
|
||||
assertEquals(instance, other);
|
||||
assertEquals(instance.hashCode(), other.hashCode());
|
||||
|
||||
DefaultJobIdentifier identifier = new DefaultJobIdentifier("foo", "bar");
|
||||
DefaultJobIdentifier other = new DefaultJobIdentifier("foo", "bar");
|
||||
|
||||
assertEquals(identifier, other);
|
||||
assertEquals(identifier.hashCode(), other.hashCode());
|
||||
}
|
||||
|
||||
public void testEqualsInstanceWithTimestamp() throws Exception {
|
||||
DefaultJobIdentifier other = new DefaultJobIdentifier(instance.getName());
|
||||
other.setJobKey(instance.getJobKey());
|
||||
assertEquals(instance, other);
|
||||
assertEquals(other, instance);
|
||||
assertEquals(instance.hashCode(), other.hashCode());
|
||||
}
|
||||
|
||||
public void testEqualsNull() throws Exception {
|
||||
assertNotSame(null, instance);
|
||||
}
|
||||
|
||||
public void testToString() throws Exception {
|
||||
assertTrue("String does not contain key: "+instance, instance.toString().indexOf("key=")>=0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,15 +25,14 @@ import junit.framework.TestCase;
|
||||
*/
|
||||
public class ScheduledJobIdentifierTests extends TestCase {
|
||||
|
||||
private ScheduledJobIdentifier instance = new ScheduledJobIdentifier(null);
|
||||
private ScheduledJobIdentifier instance = new ScheduledJobIdentifier(null, "");
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getName()}.
|
||||
*/
|
||||
public void testDefaultConstructor() {
|
||||
instance = new ScheduledJobIdentifier();
|
||||
assertEquals(null, instance.getName());
|
||||
instance.setName("foo");
|
||||
instance = new ScheduledJobIdentifier("foo", "bar", new Date());
|
||||
assertEquals("foo", instance.getName());
|
||||
}
|
||||
|
||||
@@ -41,8 +40,9 @@ public class ScheduledJobIdentifierTests extends TestCase {
|
||||
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getName()}.
|
||||
*/
|
||||
public void testGetName() {
|
||||
|
||||
assertEquals(null, instance.getName());
|
||||
instance.setName("foo");
|
||||
instance = new ScheduledJobIdentifier("foo");
|
||||
assertEquals("foo", instance.getName());
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class ScheduledJobIdentifierTests extends TestCase {
|
||||
*/
|
||||
public void testGetJobStream() {
|
||||
assertEquals("", instance.getJobKey());
|
||||
instance.setJobKey("foo");
|
||||
instance = new ScheduledJobIdentifier("bar", "foo");
|
||||
assertEquals("foo", instance.getJobKey());
|
||||
}
|
||||
|
||||
@@ -59,8 +59,7 @@ public class ScheduledJobIdentifierTests extends TestCase {
|
||||
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getScheduleDate()}.
|
||||
*/
|
||||
public void testGetScheduleDate() {
|
||||
assertNotNull(instance.getScheduleDate());
|
||||
instance.setScheduleDate(new Date(100L));
|
||||
instance = new ScheduledJobIdentifier("foo", "bar", new Date(100L));
|
||||
assertEquals(100L, instance.getScheduleDate().getTime());
|
||||
}
|
||||
|
||||
@@ -69,17 +68,15 @@ public class ScheduledJobIdentifierTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testEqualsInstanceWithSameProperties() throws Exception {
|
||||
ScheduledJobIdentifier other = new ScheduledJobIdentifier(instance.getName());
|
||||
other.setJobKey(instance.getJobKey());
|
||||
other.setScheduleDate(instance.getScheduleDate());
|
||||
instance = new ScheduledJobIdentifier("foo", "bar", new Date(100L));
|
||||
ScheduledJobIdentifier other = new ScheduledJobIdentifier("foo", "bar", new Date(100L));
|
||||
assertEquals(instance, other);
|
||||
assertEquals(instance.hashCode(), other.hashCode());
|
||||
}
|
||||
|
||||
public void testEqualsInstanceWithTimestamp() throws Exception {
|
||||
ScheduledJobIdentifier other = new ScheduledJobIdentifier(instance.getName());
|
||||
other.setJobKey(instance.getJobKey());
|
||||
other.setScheduleDate(new Date(instance.getScheduleDate().getTime()));
|
||||
instance = new ScheduledJobIdentifier("foo", "bar", new Date(100L));
|
||||
ScheduledJobIdentifier other = new ScheduledJobIdentifier("foo", "bar", new Date(100L));
|
||||
assertEquals(instance, other);
|
||||
assertEquals(other, instance);
|
||||
assertEquals(instance.hashCode(), other.hashCode());
|
||||
@@ -88,9 +85,4 @@ public class ScheduledJobIdentifierTests extends TestCase {
|
||||
public void testEqualsNull() throws Exception {
|
||||
assertNotSame(null, instance);
|
||||
}
|
||||
|
||||
public void testToString() throws Exception {
|
||||
assertTrue("String does not contain key: "+instance, instance.toString().indexOf("key=")>=0);
|
||||
assertTrue("String does not contain date: "+instance, instance.toString().indexOf("scheduleDate=")>=0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user