OPEN - issue BATCH-911: Consolidate Samples
http://jira.springframework.org/browse/BATCH-911 Consolidated non sequential and incrementer jobs into skipSample.
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
package org.springframework.batch.sample.domain.football.internal;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
|
||||
public class ErrorLogTasklet implements Tasklet {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
public RepeatStatus execute(StepContribution contribution, AttributeAccessor attributes) throws Exception {
|
||||
this.simpleJdbcTemplate.update("insert into ERROR_LOG values ('Some records were skipped!')");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package org.springframework.batch.sample.domain.football.internal;
|
||||
|
||||
import org.springframework.batch.core.ExitStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.flow.support.state.JobExecutionDecider;
|
||||
|
||||
public class SkipCheckingDecider implements JobExecutionDecider {
|
||||
|
||||
public String decide(JobExecution jobExecution, StepExecution stepExecution) {
|
||||
if (!stepExecution.getExitStatus().getExitCode().equals(ExitStatus.FAILED.getExitCode())
|
||||
&& stepExecution.getSkipCount() > 0) {
|
||||
return "COMPLETED WITH SKIPS";
|
||||
} else {
|
||||
return ExitStatus.FINISHED.getExitCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package org.springframework.batch.sample.domain.football.internal;
|
||||
|
||||
import org.springframework.batch.core.ExitStatus;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.StepExecutionListener;
|
||||
|
||||
public class SkipCheckingListener implements StepExecutionListener {
|
||||
|
||||
public ExitStatus afterStep(StepExecution stepExecution) {
|
||||
if (!stepExecution.getExitStatus().getExitCode().equals(ExitStatus.FAILED.getExitCode())
|
||||
&& stepExecution.getSkipCount() > 0) {
|
||||
return new ExitStatus("COMPLETED WITH SKIPS");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ package org.springframework.batch.sample.domain.trade;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang.builder.HashCodeBuilder;
|
||||
|
||||
|
||||
@@ -81,7 +80,18 @@ public class Trade implements Serializable {
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
return EqualsBuilder.reflectionEquals(this, o);
|
||||
if(!(o instanceof Trade)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(o == this){
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
Trade t = (Trade)o;
|
||||
return isin.equals(t.getIsin()) && quantity == t.getQuantity() &&
|
||||
price.equals(t.getPrice()) && customer.equals(t.getCustomer()) ;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package org.springframework.batch.sample.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
|
||||
/**
|
||||
* Remembers all items written - useful for testing.
|
||||
*/
|
||||
public class ItemTrackingItemWriter<T> implements ItemWriter<T> {
|
||||
|
||||
private List<T> items = new ArrayList<T>();
|
||||
|
||||
private T failed = null;
|
||||
|
||||
private int failure = -1;
|
||||
|
||||
private int counter = 0;
|
||||
|
||||
public void write(List<? extends T> items) throws Exception {
|
||||
if (failed!=null && items.contains(failed)) {
|
||||
throw new RuntimeException("write failed again");
|
||||
}
|
||||
this.items.addAll(items);
|
||||
int current = counter;
|
||||
counter += items.size();
|
||||
if (current < failure && counter >= failure) {
|
||||
failed = items.get(failure-current-1);
|
||||
this.items.remove(failed);
|
||||
throw new RuntimeException("write failed");
|
||||
}
|
||||
}
|
||||
|
||||
public List<T> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setWriteFailure(int failure) {
|
||||
this.failure = failure;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package org.springframework.batch.sample.tasklet;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
|
||||
/**
|
||||
* Dummy tasklet that retrieves message from the job execution context.
|
||||
*/
|
||||
public class DummyMessageReceivingTasklet extends StepExecutionListenerSupport implements Tasklet {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DummyMessageReceivingTasklet.class);
|
||||
|
||||
private String receivedMessage = null;
|
||||
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
ExecutionContext ctx = stepExecution.getJobExecution().getExecutionContext();
|
||||
receivedMessage = ctx.getString(DummyMessageSendingTasklet.MESSAGE_KEY);
|
||||
logger.info("Got message from context: " + receivedMessage);
|
||||
}
|
||||
|
||||
public RepeatStatus execute(StepContribution contribution, AttributeAccessor attributes) throws Exception {
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
|
||||
public String getReceivedMessage() {
|
||||
return receivedMessage;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package org.springframework.batch.sample.tasklet;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.core.ExitStatus;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
|
||||
/**
|
||||
* Dummy tasklet that stores a message in the job execution context.
|
||||
*/
|
||||
public class DummyMessageSendingTasklet extends StepExecutionListenerSupport implements Tasklet {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DummyMessageSendingTasklet.class);
|
||||
|
||||
public static final String MESSAGE_KEY = DummyMessageSendingTasklet.class.getSimpleName()+".MESSAGE";
|
||||
|
||||
private String message = "Hello!";
|
||||
|
||||
public ExitStatus afterStep(StepExecution stepExecution) {
|
||||
ExecutionContext ctx = stepExecution.getJobExecution().getExecutionContext();
|
||||
ctx.putString(MESSAGE_KEY, message);
|
||||
logger.info("Put message into context: " + message);
|
||||
return null;
|
||||
}
|
||||
|
||||
public RepeatStatus execute(StepContribution contribution, AttributeAccessor attributes) throws Exception {
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package org.springframework.batch.sample.tasklet;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.UnexpectedJobExecutionException;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Deletes files from an array of resources, so a pattern can be used in
|
||||
* configuration, e.g. <code>resources="/home/batch/job/**"</code>
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class FileDeletingTasklet implements Tasklet, InitializingBean {
|
||||
|
||||
private Resource[] resources;
|
||||
|
||||
public RepeatStatus execute(StepContribution contribution, AttributeAccessor attributes) throws Exception {
|
||||
for (Resource resource : resources) {
|
||||
boolean deleted = resource.getFile().delete();
|
||||
if (!deleted) {
|
||||
throw new UnexpectedJobExecutionException("Could not delete file " + resource);
|
||||
}
|
||||
}
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
|
||||
public void setResources(Resource[] resources) {
|
||||
this.resources = resources;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(resources, "Resources must be set");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user