IN PROGRESS - issue BATCH-121: BatchResourceFactoryBean - is it adding any value?
http://opensource.atlassian.com/projects/spring/browse/BATCH-121
This commit is contained in:
@@ -17,7 +17,13 @@
|
||||
package org.springframework.batch.execution.facade;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import org.springframework.batch.core.domain.JobIdentifier;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.execution.runtime.ScheduledJobIdentifier;
|
||||
import org.springframework.batch.execution.scope.StepContext;
|
||||
import org.springframework.batch.execution.scope.StepContextAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
@@ -51,7 +57,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @see FactoryBean
|
||||
*/
|
||||
public class BatchResourceFactoryBean extends AbstractFactoryBean implements ResourceLoaderAware {
|
||||
public class BatchResourceFactoryBean extends AbstractFactoryBean implements ResourceLoaderAware, StepContextAware {
|
||||
|
||||
private static final String BATCH_ROOT_PATTERN = "%BATCH_ROOT%";
|
||||
|
||||
@@ -75,6 +81,8 @@ public class BatchResourceFactoryBean extends AbstractFactoryBean implements Res
|
||||
private String jobStream = "";
|
||||
|
||||
private int jobRun = 0;
|
||||
|
||||
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
|
||||
|
||||
private String scheduleDate = "";
|
||||
|
||||
@@ -83,6 +91,15 @@ public class BatchResourceFactoryBean extends AbstractFactoryBean implements Res
|
||||
private String stepName = "";
|
||||
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
/**
|
||||
* Always false because we are usually expecting to be step scoped.
|
||||
*
|
||||
* @see org.springframework.beans.factory.config.AbstractFactoryBean#isSingleton()
|
||||
*/
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
@@ -92,6 +109,25 @@ public class BatchResourceFactoryBean extends AbstractFactoryBean implements Res
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.batch.execution.scope.StepContextAware#setStepScopeContext(org.springframework.core.AttributeAccessor)
|
||||
*/
|
||||
public void setStepContext(StepContext context) {
|
||||
Assert.state(context.getStepExecution()!=null, "The StepContext does not have an execution.");
|
||||
StepExecution execution = context.getStepExecution();
|
||||
stepName = execution.getStep().getName();
|
||||
jobName = execution.getStep().getJob().getName();
|
||||
JobIdentifier identifier = execution.getJobExecution().getJobIdentifier();
|
||||
if (identifier instanceof ScheduledJobIdentifier) {
|
||||
ScheduledJobIdentifier scheduledJobIdentifier = (ScheduledJobIdentifier) identifier;
|
||||
jobStream = scheduledJobIdentifier.getJobStream();
|
||||
jobRun = scheduledJobIdentifier.getJobRun();
|
||||
scheduleDate = dateFormat.format(scheduledJobIdentifier.getScheduleDate());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Resource representing the file defined by the file pattern.
|
||||
*
|
||||
@@ -177,5 +213,9 @@ public class BatchResourceFactoryBean extends AbstractFactoryBean implements Res
|
||||
public void setScheduleDate(String scheduleDate) {
|
||||
this.scheduleDate = scheduleDate;
|
||||
}
|
||||
|
||||
public void setDateFormatPattern(String pattern) {
|
||||
dateFormat = new SimpleDateFormat(pattern);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,14 +15,13 @@
|
||||
*/
|
||||
package org.springframework.batch.execution.scope;
|
||||
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
|
||||
/**
|
||||
* Marker interface for beans to be injected with a {@link RepeatContext}.
|
||||
* Useful for business logic implementations that want to store some state in
|
||||
* the context, to communicate between iterations, or with an enclosing
|
||||
* interceptor.
|
||||
* Marker interface for beans to be injected with a {@link StepContext}. Useful
|
||||
* for business logic implementations that want to store some state in the
|
||||
* context, to communicate between iterations, or with an enclosing executor.<br/>
|
||||
*
|
||||
* A bean which is step scoped which also implements this interface will be
|
||||
* injected with the context at the start of the bean lifecycle.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
@@ -30,9 +29,10 @@ import org.springframework.core.AttributeAccessor;
|
||||
public interface StepContextAware {
|
||||
|
||||
/**
|
||||
* Callback for injection of {@link RepeatContext}.
|
||||
* Callback for injection of {@link StepContext}.
|
||||
*
|
||||
* @param context the current context supplied by framework.
|
||||
* @param context
|
||||
* the current context supplied by framework.
|
||||
*/
|
||||
void setStepScopeContext(AttributeAccessor context);
|
||||
void setStepContext(StepContext context);
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public class StepScope implements Scope,
|
||||
scopedObject = objectFactory.getObject();
|
||||
context.setAttribute(name, scopedObject);
|
||||
if (scopedObject instanceof StepContextAware) {
|
||||
((StepContextAware) scopedObject).setStepScopeContext(context);
|
||||
((StepContextAware) scopedObject).setStepContext(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user