Add parallel processing sample (parallel version of football job)

This commit is contained in:
dsyer
2007-12-14 01:21:29 +00:00
parent 4bbd3b7b02
commit f7c5e0ff44
11 changed files with 937 additions and 24 deletions

View File

@@ -1,14 +1,36 @@
package org.springframework.batch.sample;
public class FootballJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
import javax.sql.DataSource;
import org.springframework.batch.sample.item.processor.StagingItemProcessor;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
public class FootballJobFunctionalTests extends
AbstractValidatingBatchLauncherTests {
private JdbcOperations jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
protected String[] getConfigLocations() {
return new String[] {"jobs/footballJob.xml###"};
return new String[] { "jobs/parallelJob.xml" };
}
protected void validatePostConditions() throws Exception {
// TODO Auto-generated method stub
protected void validatePostConditions() throws Exception {
int count;
count = jdbcTemplate.queryForInt(
"SELECT COUNT(*) from BATCH_STAGING where PROCESSED=?",
new Object[] {StagingItemProcessor.NEW});
assertEquals(0, count);
int total = jdbcTemplate.queryForInt(
"SELECT COUNT(*) from BATCH_STAGING");
count = jdbcTemplate.queryForInt(
"SELECT COUNT(*) from BATCH_STAGING where PROCESSED=?",
new Object[] {StagingItemProcessor.DONE});
assertEquals(total, count);
}
}

View File

@@ -0,0 +1,45 @@
package org.springframework.batch.sample.item.processor;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.core.domain.StepInstance;
import org.springframework.batch.core.runtime.SimpleJobIdentifier;
import org.springframework.batch.execution.scope.SimpleStepContext;
import org.springframework.batch.execution.scope.StepSynchronizationManager;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.util.ClassUtils;
public class StagingItemProcessorTests extends
AbstractTransactionalDataSourceSpringContextTests {
private StagingItemProcessor processor;
public void setProcessor(StagingItemProcessor processor) {
this.processor = processor;
}
protected String[] getConfigLocations() {
return new String[] { ClassUtils.addResourcePathToPackagePath(
StagingItemProcessor.class, "staging-test-context.xml") };
}
protected void prepareTestInstance() throws Exception {
SimpleStepContext stepScopeContext = StepSynchronizationManager
.open();
stepScopeContext.setStepExecution(new StepExecution(new StepInstance(
new Long(11)), new JobExecution(new JobInstance(
new SimpleJobIdentifier("job"), new Long(12)))));
super.prepareTestInstance();
}
public void testProcessInsertsNewItem() throws Exception {
int before = getJdbcTemplate().queryForInt(
"SELECT COUNT(*) from BATCH_STAGING");
processor.process("FOO");
int after = getJdbcTemplate().queryForInt(
"SELECT COUNT(*) from BATCH_STAGING");
assertEquals(before + 1, after);
}
}

View File

@@ -0,0 +1,163 @@
package org.springframework.batch.sample.item.provider;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.core.domain.StepInstance;
import org.springframework.batch.core.runtime.SimpleJobIdentifier;
import org.springframework.batch.execution.scope.SimpleStepContext;
import org.springframework.batch.execution.scope.StepSynchronizationManager;
import org.springframework.batch.repeat.context.RepeatContextSupport;
import org.springframework.batch.repeat.synch.BatchTransactionSynchronizationManager;
import org.springframework.batch.repeat.synch.RepeatSynchronizationManager;
import org.springframework.batch.sample.item.processor.StagingItemProcessor;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.util.ClassUtils;
public class StagingItemProviderTests extends
AbstractTransactionalDataSourceSpringContextTests {
private StagingItemProcessor processor;
private StagingItemProvider provider;
private Long jobId;
public void setProcessor(StagingItemProcessor processor) {
this.processor = processor;
}
public void setProvider(StagingItemProvider provider) {
this.provider = provider;
}
protected String[] getConfigLocations() {
return new String[] { ClassUtils.addResourcePathToPackagePath(
StagingItemProcessor.class, "staging-test-context.xml") };
}
protected void prepareTestInstance() throws Exception {
SimpleStepContext stepScopeContext = StepSynchronizationManager.open();
jobId = new Long(11);
stepScopeContext.setStepExecution(new StepExecution(new StepInstance(
new Long(12)), new JobExecution(new JobInstance(
new SimpleJobIdentifier("job"), jobId))));
RepeatSynchronizationManager.register(new RepeatContextSupport(null));
super.prepareTestInstance();
}
protected void onSetUpInTransaction() throws Exception {
processor.process("FOO");
processor.process("BAR");
processor.process("SPAM");
processor.process("BUCKET");
}
protected void onTearDownAfterTransaction() throws Exception {
provider.close();
getJdbcTemplate().update("DELETE FROM BATCH_STAGING");
}
public void testProviderUpdatesProcessIndicator() throws Exception {
long id = getJdbcTemplate().queryForLong(
"SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before = (String) getJdbcTemplate().queryForObject(
"SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
assertEquals(StagingItemProcessor.NEW, before);
Object item = provider.next();
assertEquals("FOO", item);
String after = (String) getJdbcTemplate().queryForObject(
"SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
assertEquals(StagingItemProcessor.DONE, after);
}
public void testUpdateProcessIndicatorAfterCommit() throws Exception {
testProviderUpdatesProcessIndicator();
setComplete();
endTransaction();
startNewTransaction();
long id = getJdbcTemplate().queryForLong(
"SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before = (String) getJdbcTemplate().queryForObject(
"SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
assertEquals(StagingItemProcessor.DONE, before);
}
public void testProviderRollsBackMultipleTimes() throws Exception {
setComplete();
endTransaction();
startNewTransaction();
// After a rollback we have to resynchronize the TX to simulate a real batch
BatchTransactionSynchronizationManager.resynchronize();
int count = getJdbcTemplate().queryForInt(
"SELECT COUNT(*) from BATCH_STAGING where JOB_ID=? AND PROCESSED=?",
new Object[] { jobId, StagingItemProcessor.NEW });
assertEquals(4, count);
Object item = provider.next();
assertEquals("FOO", item);
item = provider.next();
assertEquals("BAR", item);
endTransaction();
startNewTransaction();
BatchTransactionSynchronizationManager.resynchronize();
item = provider.next();
assertEquals("FOO", item);
item = provider.next();
assertEquals("BAR", item);
item = provider.next();
assertEquals("SPAM", item);
endTransaction();
startNewTransaction();
BatchTransactionSynchronizationManager.resynchronize();
item = provider.next();
assertEquals("FOO", item);
}
public void testProviderRollsBackProcessIndicator() throws Exception {
setComplete();
endTransaction();
startNewTransaction();
// After a rollback we have to resynchronize the TX to simulate a real batch
BatchTransactionSynchronizationManager.resynchronize();
long id = getJdbcTemplate().queryForLong(
"SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before = (String) getJdbcTemplate().queryForObject(
"SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
assertEquals(StagingItemProcessor.NEW, before);
Object item = provider.next();
assertEquals("FOO", item);
endTransaction();
startNewTransaction();
// After a rollback we have to resynchronize the TX to simulate a real batch
BatchTransactionSynchronizationManager.resynchronize();
String after = (String) getJdbcTemplate().queryForObject(
"SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
assertEquals(StagingItemProcessor.NEW, after);
item = provider.next();
assertEquals("FOO", item);
}
}

View File

@@ -0,0 +1,38 @@
package test.jdbc.datasource;
import java.io.File;
import javax.sql.DataSource;
import org.apache.derby.jdbc.EmbeddedDataSource;
import org.springframework.beans.factory.config.AbstractFactoryBean;
public class DerbyDataSourceFactoryBean extends AbstractFactoryBean {
private String dataDirectory = "derby-home";
DataSource dataSource;
public void setDataDirectory(String dataDirectory) {
this.dataDirectory = dataDirectory;
}
protected Object createInstance() throws Exception {
File directory = new File(dataDirectory);
System.setProperty("derby.system.home", directory.getCanonicalPath());
System.setProperty("derby.storage.fileSyncTransactionLog", "true");
System.setProperty("derby.storage.pageCacheSize", "100");
final EmbeddedDataSource ds = new EmbeddedDataSource();
ds.setDatabaseName("derbydb");
ds.setCreateDatabase("create");
dataSource = ds;
return ds;
}
public Class getObjectType() {
return DataSource.class;
}
}