OPEN - issue BATCH-304: BatchCommandLineLauncher simplified and rename

http://jira.springframework.org/browse/BATCH-304

Nuke JobIdentifier
This commit is contained in:
dsyer
2008-01-24 08:47:16 +00:00
parent 13bce7b1e1
commit 09fc32c4b9
24 changed files with 118 additions and 1016 deletions

View File

@@ -1,46 +0,0 @@
/*
* 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.core.domain;
/**
* Identifier strategy for {@link JobInstance}. Different batch projects can
* have different approaches and requirements regarding the identity of a job.
* The minimum requirement is to provide a unique name to identify a job and JobRuntimeParameters.
*
* @author Dave Syer
* @author Lucas Ward
* @since 1.0
*/
public interface JobIdentifier {
/**
* A name property for jobs provided by the {@link JobIdentifier} strategy.
*
* @return the name of the job
*/
public String getName();
/**
* A simple getter for the {@link JobParameters} that also identify
* this job.
*
* @return JobRuntimeParameters
*/
public JobParameters getJobInstanceProperties();
}

View File

@@ -5,7 +5,6 @@ package org.springframework.batch.core.domain;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -101,7 +100,7 @@ public class JobParameters {
* @return an unmodifiable map containing all parameters.
*/
public Map getParameters(){
Map tempMap = new HashMap(stringMap);
Map tempMap = new LinkedHashMap(stringMap);
tempMap.putAll(longMap);
tempMap.putAll(dateMap);
return Collections.unmodifiableMap(tempMap);

View File

@@ -18,7 +18,6 @@ package org.springframework.batch.core.repository;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.domain.StepExecution;

View File

@@ -1,94 +0,0 @@
/*
* 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.core.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.JobParameters;
import org.springframework.util.ClassUtils;
/**
* Simple, immutable, implementation of the JobIdentifier interface.
*
* @author Dave Syer
* @author Lucas Ward
*
*/
public class SimpleJobIdentifier implements JobIdentifier {
private String name;
private JobParameters runtimeParameters;
/**
* Default constructor. Since there it is required that the Identifier at least have a name,
* the default constructor should not be called.
*/
SimpleJobIdentifier() {
super();
}
/**
* Convenience constructor with name.
* @param name
*/
public SimpleJobIdentifier(String name) {
this(name, new JobParameters());
}
public SimpleJobIdentifier(String name, JobParameters runtimeParameters){
this.name = name;
this.runtimeParameters = runtimeParameters;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.runtime.JobIdentifier#getName()
*/
public String getName() {
return this.name;
}
public JobParameters getJobInstanceProperties() {
return runtimeParameters;
}
public String toString() {
return ClassUtils.getShortName(getClass())+": name=" + name + "parameters=" + runtimeParameters;
}
public boolean equals(Object obj) {
if(obj instanceof SimpleJobIdentifier == false){
return false;
}
if(this == obj){
return true;
}
SimpleJobIdentifier rhs = (SimpleJobIdentifier)obj;
return new EqualsBuilder().
append(runtimeParameters,rhs.getJobInstanceProperties()).
append(name, rhs.getName()).
isEquals();
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
}

View File

@@ -1,50 +0,0 @@
/*
* 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.core.runtime;
import junit.framework.TestCase;
/**
* @author Dave Syer
*
*/
public class SimpleJobIdentifierTests extends TestCase {
private SimpleJobIdentifier identifier = new SimpleJobIdentifier("foo");
/**
* Test method for
* {@link org.springframework.batch.core.runtime.SimpleJobIdentifier#getName()}.
*/
public void testGetName() {
assertEquals("foo", identifier.getName());
}
/**
* Test method for
* {@link org.springframework.batch.core.runtime.SimpleJobIdentifier#toString()}.
*/
public void testToString() {
assertTrue("SimpleJobIdentifier toString should contain name: " + identifier.toString(), identifier.toString()
.indexOf("name=") >= 0);
}
public void testEquals(){
SimpleJobIdentifier testIdentifier = new SimpleJobIdentifier("foo");
assertEquals(testIdentifier,identifier);
}
}

View File

@@ -19,7 +19,6 @@ package org.springframework.batch.execution.bootstrap.support;
import java.util.Properties;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.execution.launch.JobLauncher;
/**

View File

@@ -22,7 +22,6 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.domain.JobParametersBuilder;
import org.springframework.util.StringUtils;

View File

@@ -1,80 +0,0 @@
/*
* 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.execution.launch;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.NoSuchJobException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
/**
* Interface which defines a facade for running jobs. The interface is
* intentionally minimal and package private. It is convenient to be able to
* test a {@link JobLauncher} with stub implementations of this interface.
*
* @author Lucas Ward
* @author Dave Syer
*/
interface JobExecutorFacade {
/**
* Prepare a job execution identifiable by the {@link JobIdentifier}. THis
* can then be used to run the job with the {@link #start(JobExecution)}
* method. Implementations normally require a job to be
* locatable corresponding to the {@link JobIdentifier}, matching them at
* least by name.
*
* @param jobIdentifier
* the identifier of the job to start
*
* @throws NoSuchJobException
* @throws JobExecutionAlreadyRunningException
*/
JobExecution createExecutionFrom(JobIdentifier jobIdentifier)
throws NoSuchJobException, JobExecutionAlreadyRunningException;
/**
* Start a job execution.
*
* @param execution
* the execution of the job to start
* @throws NoSuchJobException
*/
void start(JobExecution execution) throws NoSuchJobException;
/**
* Stop the job execution that was started with this runtime information.
*
* @param jobIdentifier
* the {@link JobIdentifier}.
* @throws NoSuchJobExecutionException
* if a job with this runtime information is not running
*/
void stop(JobExecution execution) throws NoSuchJobExecutionException;
/**
* Simple check for whether or not there are jobs in progress. Can be used
* by clients to wait for all jobs to finish. Finer grained monitoring and
* reporting can be implemented using the persistent execution details
* (normally in a database), provided they are maintained by the
* implementation.
*
* @return true if any jobs are active.
*/
boolean isRunning();
}

View File

@@ -1,308 +0,0 @@
/*
* 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.execution.launch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.JobLocator;
import org.springframework.batch.core.domain.NoSuchJobException;
import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.core.executor.JobExecutor;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.execution.job.DefaultJobExecutor;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.statistics.StatisticsProvider;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* <p>
* Simple implementation of (@link {@link JobExecutorFacade}).
*
* <p>
* A {@link JobIdentifier} will be used to uniquely identify the job by the
* repository. Once the job is obtained, the {@link JobExecutor} will be used to
* run the job.
* </p>
*
* <p>
* Listeners can be registered for callbacks at the start and end of a job.
* </p>
*
* @author Lucas Ward
* @author Dave Syer
*
*/
class SimpleJobExecutorFacade implements JobExecutorFacade,
JobExecutionListener, StatisticsProvider, InitializingBean {
private Map jobExecutionRegistry = new HashMap();
private JobExecutor jobExecutor = new DefaultJobExecutor();
private JobRepository jobRepository;
// there is no sensible default for this
private JobLocator jobLocator;
private List listeners = new ArrayList();
private int running = 0;
private Object mutex = new Object();
/**
* Public setter for the listeners property.
*
* @param listeners
* the listeners to set - a list of {@link JobExecutionListener}.
*/
public void setJobExecutionListeners(List listeners) {
this.listeners = listeners;
}
/**
* Check mandatory properties (jobLocator, jobRepository).
*
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(jobRepository, "JobRepository must be provided.");
Assert.notNull(jobLocator,
"JobLocator must be provided.");
}
/**
* Public accessor for the running property.
*
* @return the running
*/
public boolean isRunning() {
synchronized (mutex) {
return running > 0;
}
}
/**
* Setter for injection of {@link JobLocator}.
*
* @param jobLocator
* the jobConfigurationLocator to set
*/
public void setJobLocator(
JobLocator jobLocator) {
this.jobLocator = jobLocator;
}
/**
* Setter for {@link JobExecutor}.
*
* @param jobExecutor
*/
public void setJobExecutor(JobExecutor jobExecutor) {
this.jobExecutor = jobExecutor;
}
/**
* Setter for {@link JobRepository}.
*
* @param jobRepository
*/
public void setJobRepository(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}
/**
* Locates a {@link Job} by using the name of the provided
* {@link JobIdentifier} and the {@link JobLocator}.
*
* @param jobIdentifier
* the identifier of the job that is being prepared.
*
* @throws IllegalArgumentException
* if the {@link JobIdentifier} is null or its name is null
* @throws NoSuchJobException
* if the {@link JobLocator} does not contain a
* {@link Job} with the name provided.
*
* @see org.springframework.batch.execution.launch.JobExecutorFacade#createExecutionFrom(org.springframework.batch.core.domain.JobIdentifier)
*/
public JobExecution createExecutionFrom(JobIdentifier jobIdentifier)
throws NoSuchJobException, JobExecutionAlreadyRunningException {
Assert.notNull(jobIdentifier, "JobIdentifier must not be null.");
Assert.notNull(jobIdentifier.getName(),
"JobIdentifier name must not be null.");
if (jobExecutionRegistry.containsKey(jobIdentifier)) {
throw new JobExecutionAlreadyRunningException(
"A job with this JobIdentifier is already executing in this container: "+jobIdentifier);
};
Job job = jobLocator
.getJob(jobIdentifier.getName());
return jobRepository.createJobExecution(job,
null);
}
/**
* Starts a job execution that was previously acquired from the
* {@link #createExecutionFrom(JobIdentifier)} method.
*
* @see org.springframework.batch.execution.launch.JobExecutorFacade#start(JobExecution)
*
* @throws NoSuchJobException
* if the {@link JobLocator} does not contain a
* {@link Job} with the name provided by the
* enclosed {@link JobIdentifier}.
*
*/
public void start(JobExecution execution)
throws NoSuchJobException {
Job job = jobLocator
.getJob(execution.getJobInstance().getJobName());
this.before(execution);
try {
jobExecutor.run(job, execution);
} finally {
this.after(execution);
}
}
/**
* Internal accounting for the job execution. Callback at start of job,
* dealing with internal housekeeping before delegating to listeners in the
* order that they were given.
*
* @param execution
*
* @see JobExecutionListener#before(JobExecution)
*/
public void before(JobExecution execution) {
synchronized (mutex) {
running++;
jobExecutionRegistry.put(execution.getJobInstance(),
execution);
}
for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
JobExecutionListener listener = (JobExecutionListener) iterator
.next();
listener.before(execution);
}
}
/**
* Broadcast stop signal to all the registered listeners.
*
* @param execution
*
* @see JobExecutionListener#onStop(JobExecution)
*/
public void onStop(JobExecution execution) {
for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
JobExecutionListener listener = (JobExecutionListener) iterator
.next();
listener.onStop(execution);
}
}
/**
* Internal accounting for the job execution. Callback at end of job
* delegating first to listeners, in reverse order to the list supplied, and
* then finally dealing with internal housekeeping.
*
* @param execution
*
* @see JobExecutionListener#after(JobExecution)
*/
public void after(JobExecution execution) {
ArrayList reversed = new ArrayList(listeners);
Collections.reverse(reversed);
for (Iterator iterator = reversed.iterator(); iterator.hasNext();) {
JobExecutionListener listener = (JobExecutionListener) iterator
.next();
listener.after(execution);
}
synchronized (mutex) {
// assume execution is synchronous so when we get to here we are
// not running any more
jobExecutionRegistry.remove(execution.getJobInstance());
running--;
}
}
/**
* Send a stop signal to the running execution by setting all their
* {@link RepeatContext} to terminate only. Then call the
* {@link JobExecutionListener#onStop(JobExecution)} method.
*
* @see org.springframework.batch.container.BatchContainer#onStop(org.springframework.batch.container.common.runtime.JobRuntimeInformation)
*/
public void stop(JobExecution execution) throws NoSuchJobExecutionException {
if (!jobExecutionRegistry.containsValue(execution)) {
throw new NoSuchJobExecutionException(
"The job is not executing in this executor: [" + execution
+ "]");
}
for (Iterator iter = execution.getStepExecutions().iterator(); iter
.hasNext();) {
StepExecution context = (StepExecution) iter.next();
context.setTerminateOnly();
}
this.onStop(execution);
}
/**
* Provides a snapshot of properties from running jobs (the ones that were
* launched from this {@link JobExecutorFacade).
*
* @return a read-only view of the state of the running jobs.
*/
public Properties getStatistics() {
int i = 0;
Properties props = new Properties();
for (Iterator iter = jobExecutionRegistry.values().iterator(); iter
.hasNext();) {
JobExecution element = (JobExecution) iter.next();
i++;
String runtime = "job" + i;
props.setProperty(runtime, "" + element.getJobInstance());
int j = 0;
for (Iterator iterator = element.getStepExecutions().iterator(); iterator
.hasNext();) {
StepExecution context = (StepExecution) iterator.next();
j++;
props.setProperty(runtime + ".step" + j, "" + context);
}
}
return props;
}
}

View File

@@ -23,7 +23,6 @@ import java.util.Properties;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.domain.Step;

View File

@@ -29,7 +29,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.domain.BatchStatus;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.repository.NoSuchBatchDomainObjectException;

View File

@@ -19,7 +19,6 @@ package org.springframework.batch.execution.repository.dao;
import java.util.List;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.JobParameters;

View File

@@ -1,59 +0,0 @@
/*
* 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.execution.resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.runtime.SimpleJobIdentifier;
import org.springframework.batch.execution.runtime.ScheduledJobIdentifier;
/**
* {@link JobIdentifierLabelGenerator} that knows about
* {@link SimpleJobIdentifier} and {@link ScheduledJobIdentifier} and provides a
* fixed format label for each.
*
*
* @author Dave Syer
*
*/
public class DefaultJobIdentifierLabelGenerator implements
JobIdentifierLabelGenerator {
private static final DateFormat dateFormat = new SimpleDateFormat(
"yyyyMMdd");
/**
* Concatenate the properties of the {@link JobIdentifier}. From a
* {@link SimpleJobIdentifier} we just get the name, and from a
* {@link ScheduledJobIdentifier} we get the name, stream, run and schedule
* date (yyyyMMdd) joined by hyphens.
*
* @see org.springframework.batch.execution.resource.JobIdentifierLabelGenerator#getLabel(org.springframework.batch.core.domain.JobIdentifier)
*/
public String getLabel(JobIdentifier jobIdentifier) {
if (jobIdentifier == null) {
return null;
}
if (jobIdentifier instanceof ScheduledJobIdentifier) {
ScheduledJobIdentifier id = (ScheduledJobIdentifier) jobIdentifier;
return jobIdentifier.getName() + "-" + id.getJobKey() + "-"
+ dateFormat.format(id.getScheduleDate());
}
return jobIdentifier.getName();
}
}

View File

@@ -0,0 +1,59 @@
/*
* 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.execution.resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.JobParameters;
/**
* {@link JobInstanceLabelGenerator} that knows about {@link JobParameters} and
* provides a fixed format label for each.
*
*
* @author Dave Syer
*
*/
public class DefaultJobInstanceLabelGenerator implements JobInstanceLabelGenerator {
private static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
/**
* Concatenate the properties of the {@link JobInstance}. From an instance
* with no additional parameters we just get the name, otherwise we get the
* parameters joined by hyphens (Strings then Longs then Dates). The date
* format is "yyyyMMdd".
*
* @see org.springframework.batch.execution.resource.JobInstanceLabelGenerator#getLabel(JobInstance)
*/
public String getLabel(JobInstance jobInstance) {
if (jobInstance == null) {
return null;
}
StringBuilder builder = new StringBuilder(""+jobInstance.getJobName());
Map map = jobInstance.getJobInstanceProperties().getParameters();
for (Iterator iterator = map.values().iterator(); iterator.hasNext();) {
Object value = iterator.next();
builder.append("-" + ((value instanceof Date) ? dateFormat.format(value) : ""+value));
}
return builder.toString();
}
}

View File

@@ -16,7 +16,7 @@
package org.springframework.batch.execution.resource;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.JobInstance;
/**
* Strategy for generating a label (e.g. for a file name) from a
@@ -26,16 +26,16 @@ import org.springframework.batch.core.domain.JobIdentifier;
* @author Dave Syer
*
*/
public interface JobIdentifierLabelGenerator {
public interface JobInstanceLabelGenerator {
/**
* Create a label from the {@link JobIdentifier}.
*
* @param jobIdentifier
* a {@link JobIdentifier}
* @return a short string describing the identifier with no whitespace or
* @param jobInstance
* a {@link JobInstance}
* @return a short string describing the job instance with no whitespace or
* special characters. Return null if the identifier is null.
*/
String getLabel(JobIdentifier jobIdentifier);
String getLabel(JobInstance jobInstance);
}

View File

@@ -1,62 +0,0 @@
/*
* 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.execution.runtime;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.domain.JobParametersBuilder;
import org.springframework.batch.core.runtime.SimpleJobIdentifier;
/**
* @author Dave Syer
* @author Lucas Ward
*
*/
public class DefaultJobIdentifier extends SimpleJobIdentifier implements
JobIdentifier {
public static final String JOB_KEY = "job.key";
/**
* Default constructor package access only.
*/
DefaultJobIdentifier() {
this(null);
}
/**
* @param name the name for the job
*/
public DefaultJobIdentifier(String name) {
super(name);
}
/**
* @param name the name for the job
*/
public DefaultJobIdentifier(String name, String key) {
this(name, new JobParametersBuilder().addString(JOB_KEY, key).toJobParameters());
}
public DefaultJobIdentifier(String name, JobParameters parameters){
super(name, parameters);
}
public String getJobKey() {
return getJobInstanceProperties().getString(JOB_KEY);
}
}

View File

@@ -1,80 +0,0 @@
/*
* 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.execution.runtime;
import java.util.Date;
import org.springframework.batch.core.domain.JobIdentifier;
import org.springframework.batch.core.domain.JobParametersBuilder;
/**
* Convenient {@link JobIdentifier} implementation that identifies itself by a
* {@link Date} as well as an optional String key. The time portion of the
* schedule date is significant, and clients are responsible for truncating it
* if it represents a date rather than a timestamp.
*
* @author Dave Syer
*
*/
public class ScheduledJobIdentifier extends DefaultJobIdentifier implements JobIdentifier {
public static final String SCHEDULE_DATE = "schedule.date";
ScheduledJobIdentifier() {
this(null);
}
public ScheduledJobIdentifier(String name) {
super(name);
}
/**
* Convenience constructor that leaves the schedule date null.
*
* @param name the name of the job
* @param key a unique key for this execution
*/
public ScheduledJobIdentifier(String name, String key) {
super(name, key);
}
/**
* Convenience constructor that leaves the key null.
*
* @param name the name of the job
* @param scheduleDate a timestamp
*/
public ScheduledJobIdentifier(String name, Date scheduleDate) {
super(name, new JobParametersBuilder().addDate(SCHEDULE_DATE, scheduleDate).toJobParameters());
}
/**
* Convenience constructor with all properties.
*
* @param name the name of the job
* @param key a unique key for this execution
* @param scheduleDate a timestamp
*/
public ScheduledJobIdentifier(String name, String key, Date scheduleDate) {
super(name, new JobParametersBuilder().addString(ScheduledJobIdentifier.JOB_KEY, key).addDate(
SCHEDULE_DATE, scheduleDate).toJobParameters());
}
public Date getScheduleDate() {
return getJobInstanceProperties().getDate(SCHEDULE_DATE);
}
}

View File

@@ -1,7 +0,0 @@
<html>
<body>
<p>
Specific implementations of runtime concerns.
</p>
</body>
</html>

View File

@@ -1,49 +0,0 @@
package org.springframework.batch.execution.resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import junit.framework.TestCase;
import org.springframework.batch.core.runtime.SimpleJobIdentifier;
import org.springframework.batch.execution.resource.DefaultJobIdentifierLabelGenerator;
import org.springframework.batch.execution.runtime.ScheduledJobIdentifier;
public class DefaultJobIdentifierLabelGeneratorTests extends TestCase {
DefaultJobIdentifierLabelGenerator instance = new DefaultJobIdentifierLabelGenerator();
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
/**
* Test method for {@link org.springframework.batch.execution.resource.DefaultJobIdentifierLabelGenerator#getLabel()}.
*/
public void testGetLabelFromNull() {
assertEquals(null, instance.getLabel(null));
}
/**
* Test method for {@link org.springframework.batch.execution.resource.DefaultJobIdentifierLabelGenerator#getLabel()}.
*/
public void testGetLabel() {
assertEquals("foo", instance.getLabel(new SimpleJobIdentifier("foo")));
}
/**
* Test method for {@link org.springframework.batch.execution.resource.DefaultJobIdentifierLabelGenerator#getLabel()}.
*/
public void testDefaultGetLabel() throws Exception {
System.out.println(instance.getLabel(new ScheduledJobIdentifier(null, dateFormat.parse("19700101"))));
assertEquals("null-null-19700101", instance.getLabel(new ScheduledJobIdentifier(null, dateFormat.parse("19700101"))));
}
/**
* Test method for {@link org.springframework.batch.execution.resource.DefaultJobIdentifierLabelGenerator#getLabel()}.
*/
public void testGetLabelWithAllProperties() throws Exception {
ScheduledJobIdentifier identifier = new ScheduledJobIdentifier("foo", "bar",
new SimpleDateFormat("yyyyMMdd").parse("20070730"));
assertEquals("foo-bar-20070730", instance.getLabel(identifier));
}
}

View File

@@ -0,0 +1,52 @@
package org.springframework.batch.execution.resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import junit.framework.TestCase;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.domain.JobParametersBuilder;
public class DefaultJobInstanceLabelGeneratorTests extends TestCase {
DefaultJobInstanceLabelGenerator instance = new DefaultJobInstanceLabelGenerator();
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
/**
* Test method for {@link org.springframework.batch.execution.resource.DefaultJobInstanceLabelGenerator#getLabel()}.
*/
public void testGetLabelFromNull() {
assertEquals(null, instance.getLabel(null));
}
/**
* Test method for {@link org.springframework.batch.execution.resource.DefaultJobInstanceLabelGenerator#getLabel()}.
*/
public void testGetLabel() {
assertEquals("foo", instance.getLabel(new JobInstance(null, new JobParameters(), new Job("foo"))));
}
/**
* Test method for {@link org.springframework.batch.execution.resource.DefaultJobInstanceLabelGenerator#getLabel()}.
*/
public void testDefaultGetLabel() throws Exception {
JobParameters jobParameters = new JobParametersBuilder().addDate("schedule.date", new Date(0)).addString("key", "bar").toJobParameters();
JobInstance job = new JobInstance(null, jobParameters, new Job(null));
assertEquals("null-bar-19700101", instance.getLabel(job));
}
/**
* Test method for {@link org.springframework.batch.execution.resource.DefaultJobInstanceLabelGenerator#getLabel()}.
*/
public void testGetLabelWithAllProperties() throws Exception {
JobParameters jobParameters = new JobParametersBuilder().addDate("schedule.date", new Date(0)).addString("key", "bar").toJobParameters();
JobInstance job = new JobInstance(null, jobParameters, new Job("foo"));
assertEquals("foo-bar-19700101", instance.getLabel(job));
}
}

View File

@@ -1,63 +0,0 @@
/*
* 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.execution.runtime;
import junit.framework.TestCase;
/**
* @author Dave Syer
*
*/
public class DefaultJobIdentifierTests extends TestCase {
private DefaultJobIdentifier instance = new DefaultJobIdentifier(null);
/**
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getJobName()}.
*/
public void testGetName() {
DefaultJobIdentifier identifier = new DefaultJobIdentifier("foo");
assertEquals("foo", identifier.getName());
}
/**
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getJobKey()}.
*/
public void testGetJobStream() {
DefaultJobIdentifier identifier = new DefaultJobIdentifier("bar", "foo");
assertEquals("foo", identifier.getJobKey());
}
public void testEqualsSelf() throws Exception {
assertEquals(instance, instance);
}
public void testEqualsInstanceWithSameProperties() throws Exception {
DefaultJobIdentifier identifier = new DefaultJobIdentifier("foo", "bar");
DefaultJobIdentifier other = new DefaultJobIdentifier("foo", "bar");
assertEquals(identifier, other);
assertEquals(identifier.hashCode(), other.hashCode());
}
public void testEqualsNull() throws Exception {
assertNotSame(null, instance);
}
}

View File

@@ -1,88 +0,0 @@
/*
* 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.execution.runtime;
import java.util.Date;
import junit.framework.TestCase;
/**
* @author Dave Syer
*
*/
public class ScheduledJobIdentifierTests extends TestCase {
private ScheduledJobIdentifier instance = new ScheduledJobIdentifier(null, "");
/**
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getJobName()}.
*/
public void testDefaultConstructor() {
assertEquals(null, instance.getName());
instance = new ScheduledJobIdentifier("foo", "bar", new Date());
assertEquals("foo", instance.getName());
}
/**
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getJobName()}.
*/
public void testGetName() {
assertEquals(null, instance.getName());
instance = new ScheduledJobIdentifier("foo");
assertEquals("foo", instance.getName());
}
/**
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getJobKey()}.
*/
public void testGetJobStream() {
assertEquals("", instance.getJobKey());
instance = new ScheduledJobIdentifier("bar", "foo");
assertEquals("foo", instance.getJobKey());
}
/**
* Test method for {@link org.springframework.batch.core.domain.JobInstance#getScheduleDate()}.
*/
public void testGetScheduleDate() {
instance = new ScheduledJobIdentifier("foo", "bar", new Date(100L));
assertEquals(100L, instance.getScheduleDate().getTime());
}
public void testEqualsSelf() throws Exception {
assertEquals(instance, instance);
}
public void testEqualsInstanceWithSameProperties() throws Exception {
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 {
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());
}
public void testEqualsNull() throws Exception {
assertNotSame(null, instance);
}
}

View File

@@ -12,18 +12,6 @@
<bean parent="stepScope"/>
<bean parent="jobConfigurationRegistryBeanPostProcessor"/>
<!-- Override the default jobExecutorFacade to provide a listener that can interrupt the job -->
<bean id="jobExecutorFacade" class="org.springframework.batch.execution.launch.SimpleJobExecutorFacade">
<property name="jobRepository" ref="simpleJobRepository" />
<property name="jobLocator" ref="jobConfigurationRegistry"/>
<property name="jobExecutor" ref="jobExecutor" />
<property name="jobExecutionListeners">
<list>
<bean class="org.springframework.batch.execution.bootstrap.support.ThreadInterruptJobExecutionListener"/>
</list>
</property>
</bean>
<bean id="jobLauncher" class="org.springframework.batch.execution.launch.SimpleJobLauncher">
<property name="jobExecutor" ref="jobExecutor" />
<property name="jobRepository" ref="simpleJobRepository" />

View File

@@ -1,7 +1,6 @@
package org.springframework.batch.sample.dao;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
@@ -15,7 +14,6 @@ import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.execution.runtime.ScheduledJobIdentifier;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
@@ -41,8 +39,6 @@ public class JdbcJobRepositoryTests extends AbstractTransactionalDataSourceSprin
}
protected void onSetUpInTransaction() throws Exception {
new ScheduledJobIdentifier("Job1", "TestStream",
new SimpleDateFormat("yyyyMMdd").parse("20070505"));
jobConfiguration = new Job("test-job");
jobConfiguration.setRestartable(true);
}