Skip to content

Instantly share code, notes, and snippets.

@jbaruch
Created April 29, 2010 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbaruch/384265 to your computer and use it in GitHub Desktop.
Save jbaruch/384265 to your computer and use it in GitHub Desktop.
spring-batch samples source code patched to work with mongo-db
Index: spring-batch-samples/src/main/resources/data-source-context.xml
===================================================================
--- spring-batch-samples/src/main/resources/data-source-context.xml (revision 4050)
+++ spring-batch-samples/src/main/resources/data-source-context.xml (revision )
@@ -28,6 +28,31 @@
<property name="dataSource" ref="dataSource" />
</bean>
+ <bean id="mongoInitializer" class="org.springframework.batch.mongo.config.MongoDbInitializer">
+ <property name="db" ref="db"/>
+ <property name="initialize" value="true"/>
+ </bean>
+
+ <bean id="db" class="org.springframework.batch.mongo.config.MongoDbFactoryBean">
+ <constructor-arg index="0" value="batch"/>
+ <constructor-arg index="1" value="localhost"/>
+ <constructor-arg index="2" value="27017"/>
+ </bean>
+
+ <bean class="org.springframework.batch.mongo.dao.MongoExecutionContextDao" id="executionContextDao">
+ <property name="db" ref="db"/>
+ </bean>
+ <bean class="org.springframework.batch.mongo.dao.MongoJobInstanceDao" id="jobInstanceDao">
+ <property name="db" ref="db"/>
+ </bean>
+ <bean class="org.springframework.batch.mongo.dao.MongoJobExecutionDao" id="jobExecutionDao">
+ <property name="db" ref="db"/>
+ </bean>
+ <bean class="org.springframework.batch.mongo.dao.MongoStepExecutionDao" id="stepExecutionDao">
+ <property name="db" ref="db"/>
+ </bean>
+
+
<!-- Set up or detect a System property called "ENVIRONMENT" used to construct a properties file on the classpath. The default is "hsql". -->
<bean id="environment"
class="org.springframework.batch.support.SystemPropertyInitializer">
Index: spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml
===================================================================
--- spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml (revision 4032)
+++ spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml (revision )
@@ -12,14 +12,23 @@
<property name="jobRepository" ref="jobRepository" />
</bean>
- <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
- p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" />
+ <bean id="jobRepository"
+ class="org.springframework.batch.core.repository.support.SimpleJobRepository">
+ <constructor-arg ref="executionContextDao"/>
+ <constructor-arg ref="jobInstanceDao"/>
+ <constructor-arg ref="jobExecutionDao"/>
+ <constructor-arg ref="stepExecutionDao"/>
+ </bean>
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator" p:jobLauncher-ref="jobLauncher"
p:jobExplorer-ref="jobExplorer" p:jobRepository-ref="jobRepository" p:jobRegistry-ref="jobRegistry" />
- <bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
- p:dataSource-ref="dataSource" />
+ <bean id="jobExplorer" class="org.springframework.batch.core.explore.support.SimpleJobExplorer">
+ <constructor-arg ref="executionContextDao"/>
+ <constructor-arg ref="jobInstanceDao"/>
+ <constructor-arg ref="jobExecutionDao"/>
+ <constructor-arg ref="stepExecutionDao"/>
+ </bean>
<bean class="org.springframework.batch.core.configuration.support.AutomaticJobRegistrar">
<property name="applicationContextFactories">
@@ -41,10 +50,10 @@
</bean>
<bean id="logAdvice" class="org.springframework.batch.sample.common.LogAdvice" />
-
+
<bean id="customerIncrementer" parent="incrementerParent">
<property name="incrementerName" value="CUSTOMER_SEQ" />
</bean>
-
+
</beans>
Index: spring-batch-samples/src/main/resources/simple-job-launcher-context.xml
===================================================================
--- spring-batch-samples/src/main/resources/simple-job-launcher-context.xml (revision 4050)
+++ spring-batch-samples/src/main/resources/simple-job-launcher-context.xml (revision )
@@ -19,8 +19,12 @@
</bean>
<bean id="jobRepository"
- class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
- p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" p:lobHandler-ref="lobHandler"/>
+ class="org.springframework.batch.core.repository.support.SimpleJobRepository">
+ <constructor-arg ref="executionContextDao"/>
+ <constructor-arg ref="jobInstanceDao"/>
+ <constructor-arg ref="jobExecutionDao"/>
+ <constructor-arg ref="stepExecutionDao"/>
+ </bean>
<bean id="mapJobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"
@@ -32,8 +36,12 @@
p:jobRepository-ref="jobRepository" p:jobRegistry-ref="jobRegistry" />
<bean id="jobExplorer"
- class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
- p:dataSource-ref="dataSource" />
+ class="org.springframework.batch.core.explore.support.SimpleJobExplorer">
+ <constructor-arg ref="executionContextDao"/>
+ <constructor-arg ref="jobInstanceDao"/>
+ <constructor-arg ref="jobExecutionDao"/>
+ <constructor-arg ref="stepExecutionDao"/>
+ </bean>
<bean id="jobRegistry"
class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
Index: spring-batch-core/pom.xml
===================================================================
--- spring-batch-core/pom.xml (revision 4066)
+++ spring-batch-core/pom.xml (revision )
@@ -132,6 +132,46 @@
<artifactId>log4j</artifactId>
<optional>true</optional>
</dependency>
+ <dependency>
+ <groupId>org.mongodb</groupId>
+ <artifactId>mongo-java-driver</artifactId>
+ <version>1.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.batch</groupId>
+ <artifactId>springbatch-over-mongodb</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.springframework</groupId>
+ <artifactId>org.springframework.aop</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.springframework</groupId>
+ <artifactId>org.springframework.asm</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.springframework</groupId>
+ <artifactId>org.springframework.beans</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.springframework</groupId>
+ <artifactId>org.springframework.core</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.springframework</groupId>
+ <artifactId>org.springframework.transaction</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.springframework</groupId>
+ <artifactId>org.springframework.context</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>cglib</groupId>
+ <artifactId>cglib</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
</dependencies>
<build>
<plugins>
Index: spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java
===================================================================
--- spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java (revision 4032)
+++ spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java (revision )
@@ -1,20 +1,12 @@
package org.springframework.batch.sample;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.math.BigDecimal;
-import java.util.Map;
-
-import javax.sql.DataSource;
-
+import com.mongodb.BasicDBObjectBuilder;
+import com.mongodb.DB;
+import com.mongodb.DBObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.springframework.batch.core.BatchStatus;
-import org.springframework.batch.core.JobExecution;
-import org.springframework.batch.core.JobParametersInvalidException;
-import org.springframework.batch.core.UnexpectedJobExecutionException;
+import org.springframework.batch.core.*;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.launch.JobParametersNotFoundException;
@@ -22,6 +14,8 @@
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
+import org.springframework.batch.mongo.dao.MongoJobExecutionDao;
+import org.springframework.batch.mongo.dao.MongoStepExecutionDao;
import org.springframework.batch.sample.common.SkipCheckingListener;
import org.springframework.batch.sample.domain.trade.internal.TradeWriter;
import org.springframework.beans.factory.annotation.Autowired;
@@ -32,10 +26,16 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
+import javax.sql.DataSource;
+import java.math.BigDecimal;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
/**
* Error is encountered during writing - transaction is rolled back and the
* error item is skipped on second attempt to process the chunk.
- *
+ *
* @author Robert Kasanicky
* @author Dan Garrette
*/
@@ -60,6 +60,9 @@
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
+ @Autowired
+ private DB db;
+
@Before
public void setUp() {
simpleJdbcTemplate.update("DELETE from TRADE");
@@ -172,7 +175,7 @@
// to output
// System.err.println(simpleJdbcTemplate.queryForList("SELECT * FROM TRADE"));
assertEquals(5, simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE where VERSION=?", 1));
-
+
// 1 record skipped in processing second step
assertEquals(1, SkipCheckingListener.getProcessSkips());
@@ -187,11 +190,14 @@
System.err.println(jobExecution.getExecutionContext());
assertEquals(new BigDecimal("340.45"), jobExecution.getExecutionContext().get(TradeWriter.TOTAL_AMOUNT_KEY));
- Map<String, Object> step1Execution = getStepExecutionAsMap(jobExecution, "step1");
- assertEquals(new Long(4), step1Execution.get("COMMIT_COUNT"));
- assertEquals(new Long(8), step1Execution.get("READ_COUNT"));
- assertEquals(new Long(7), step1Execution.get("WRITE_COUNT"));
+ DBObject result = db.getCollection(StepExecution.class.getSimpleName()).findOne(BasicDBObjectBuilder.start()
+ .add(MongoJobExecutionDao.JOB_EXECUTION_ID_KEY, jobExecution.getId())
+ .add(MongoStepExecutionDao.STEP_NAME_KEY, "step1").get());
+
+ assertEquals(4, result.get(MongoStepExecutionDao.COMMIT_COUNT_KEY));
+ assertEquals(8, result.get(MongoStepExecutionDao.READ_COUNT_KEY));
+ assertEquals(7, result.get(MongoStepExecutionDao.WRITE_COUNT_KEY));
- }
+ }
private void validateLaunchWithoutSkips(JobExecution jobExecution) {
@@ -208,16 +214,9 @@
}
- private Map<String, Object> getStepExecutionAsMap(JobExecution jobExecution, String stepName) {
- long jobExecutionId = jobExecution.getId();
- return simpleJdbcTemplate.queryForMap(
- "SELECT * from BATCH_STEP_EXECUTION where JOB_EXECUTION_ID = ? and STEP_NAME = ?", jobExecutionId,
- stepName);
- }
-
/**
* Launch the entire job, including all steps, in order.
- *
+ *
* @return JobExecution, so that the test may validate the exit status
*/
public long launchJobWithIncrementer() {
Index: spring-batch-core-tests/src/test/resources/simple-job-launcher-context.xml
===================================================================
--- spring-batch-core-tests/src/test/resources/simple-job-launcher-context.xml (revision 3843)
+++ spring-batch-core-tests/src/test/resources/simple-job-launcher-context.xml (revision )
@@ -18,8 +18,12 @@
</bean>
<bean id="jobRepository"
- class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
- p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" />
+ class="org.springframework.batch.core.repository.support.SimpleJobRepository">
+ <constructor-arg ref="executionContextDao"/>
+ <constructor-arg ref="jobInstanceDao"/>
+ <constructor-arg ref="jobExecutionDao"/>
+ <constructor-arg ref="stepExecutionDao"/>
+ </bean>
<bean id="mapJobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"
@@ -30,9 +34,12 @@
p:jobLauncher-ref="jobLauncher" p:jobExplorer-ref="jobExplorer"
p:jobRepository-ref="jobRepository" p:jobRegistry-ref="jobRegistry" />
- <bean id="jobExplorer"
- class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
- p:dataSource-ref="dataSource" />
+ <bean id="jobExplorer" class="org.springframework.batch.core.explore.support.SimpleJobExplorer">
+ <constructor-arg ref="executionContextDao"/>
+ <constructor-arg ref="jobInstanceDao"/>
+ <constructor-arg ref="jobExecutionDao"/>
+ <constructor-arg ref="stepExecutionDao"/>
+ </bean>
<bean id="jobRegistry"
class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
Index: spring-batch-infrastructure/pom.xml
===================================================================
--- spring-batch-infrastructure/pom.xml (revision 4066)
+++ spring-batch-infrastructure/pom.xml (revision )
@@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-batch-infrastructure</artifactId>
- <version>2.1.2.CI-SNAPSHOT </version>
+ <version>2.1.2.CI-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Infrastructure</name>
<description><![CDATA[The Spring Batch Infrastructure is a set of
Index: spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml
===================================================================
--- spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml (revision 4050)
+++ spring-batch-samples/src/main/resources/adhoc-job-launcher-context.xml (revision )
@@ -37,11 +37,14 @@
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator">
<property name="jobExplorer">
- <bean class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
- <property name="dataSource" ref="dataSource" />
+ <bean class="org.springframework.batch.core.explore.support.SimpleJobExplorer">
+ <constructor-arg ref="executionContextDao"/>
+ <constructor-arg ref="jobInstanceDao"/>
+ <constructor-arg ref="jobExecutionDao"/>
+ <constructor-arg ref="stepExecutionDao"/>
- </bean>
- </property>
- <property name="jobRepository" ref="jobRepository" />
+ </bean>
+ </property>
+ <property name="jobRepository" ref="jobRepository" />
<property name="jobRegistry" ref="jobRegistry" />
<property name="jobLauncher">
<bean parent="jobLauncher">
Index: spring-batch-core/src/test/resources/org/springframework/batch/core/repository/dao/sql-dao-test.xml
===================================================================
--- spring-batch-core/src/test/resources/org/springframework/batch/core/repository/dao/sql-dao-test.xml (revision 2174)
+++ spring-batch-core/src/test/resources/org/springframework/batch/core/repository/dao/sql-dao-test.xml (revision )
@@ -11,25 +11,6 @@
<constructor-arg ref="executionContextDao" />
</bean>
- <bean id="executionContextDao" class="org.springframework.batch.core.repository.dao.JdbcExecutionContextDao">
- <property name="jdbcTemplate" ref="jdbcTemplate" />
- </bean>
-
- <bean id="jobInstanceDao" class="org.springframework.batch.core.repository.dao.JdbcJobInstanceDao">
- <property name="jdbcTemplate" ref="jdbcTemplate" />
- <property name="jobIncrementer" ref="jobIncrementer" />
- </bean>
-
- <bean id="jobExecutionDao" class="org.springframework.batch.core.repository.dao.JdbcJobExecutionDao">
- <property name="jdbcTemplate" ref="jdbcTemplate" />
- <property name="jobExecutionIncrementer" ref="jobExecutionIncrementer" />
- </bean>
-
- <bean id="stepExecutionDao" class="org.springframework.batch.core.repository.dao.JdbcStepExecutionDao">
- <property name="jdbcTemplate" ref="jdbcTemplate" />
- <property name="stepExecutionIncrementer" ref="stepExecutionIncrementer" />
- </bean>
-
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
Index: spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java
===================================================================
--- spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java (revision 4050)
+++ spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java (revision )
@@ -16,22 +16,18 @@
package org.springframework.batch.sample;
-import static org.junit.Assert.assertEquals;
-
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.sql.DataSource;
-
+import com.mongodb.BasicDBObjectBuilder;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.JobExecution;
+import org.springframework.batch.core.StepExecution;
+import org.springframework.batch.mongo.dao.MongoJobExecutionDao;
+import org.springframework.batch.mongo.dao.MongoStepExecutionDao;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowCallbackHandler;
@@ -39,6 +35,16 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import javax.sql.DataSource;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/customerFilterJob.xml", "/job-runner-context.xml" })
public class CustomerFilterJobFunctionalTests {
@@ -48,6 +54,8 @@
private List<Customer> customers;
private int activeRow = 0;
+ private DB db;
+
private SimpleJdbcTemplate simpleJdbcTemplate;
private Map<String, Double> credits = new HashMap<String, Double>();
@@ -59,6 +67,11 @@
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
+ @Autowired
+ public void setDb(DB db) {
+ this.db = db;
+ }
+
@Before
public void onSetUp() throws Exception {
simpleJdbcTemplate.update("delete from TRADE");
@@ -96,18 +109,16 @@
}
});
- Map<String, Object> step1Execution = this.getStepExecution(jobExecution, "uploadCustomer");
- assertEquals("4", step1Execution.get("READ_COUNT").toString());
- assertEquals("1", step1Execution.get("FILTER_COUNT").toString());
- assertEquals("3", step1Execution.get("WRITE_COUNT").toString());
+ DBCollection collection = db.getCollection(StepExecution.class.getSimpleName());
+ DBObject step1Execution = collection.findOne(BasicDBObjectBuilder.start()
+ .add(MongoJobExecutionDao.JOB_EXECUTION_ID_KEY, jobExecution.getId())
+ .add(MongoStepExecutionDao.STEP_NAME_KEY, "uploadCustomer").get());
- }
+ assertEquals("4", step1Execution.get(MongoStepExecutionDao.READ_COUNT_KEY).toString());
+ assertEquals("1", step1Execution.get(MongoStepExecutionDao.FILTER_COUT_KEY).toString());
+ assertEquals("3", step1Execution.get(MongoStepExecutionDao.WRITE_COUNT_KEY).toString());
- private Map<String, Object> getStepExecution(JobExecution jobExecution, String stepName) {
- Long jobExecutionId = jobExecution.getId();
- return simpleJdbcTemplate.queryForMap(
- "SELECT * from BATCH_STEP_EXECUTION where JOB_EXECUTION_ID = ? and STEP_NAME = ?", jobExecutionId,
- stepName);
+
}
private static class Customer {
@@ -135,7 +146,7 @@
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
@@ -150,7 +161,7 @@
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
Index: spring-batch-core/ivy.xml
===================================================================
--- spring-batch-core/ivy.xml (revision 2805)
+++ spring-batch-core/ivy.xml (revision )
@@ -43,6 +43,8 @@
<dependency org="org.springframework" name="org.springframework.transaction" rev="2.5.6.A" conf="compile->runtime,provided"/>
<dependency org="javax.annotation" name="com.springsource.javax.annotation" rev="1.0.0" conf="test->runtime,provided"/>
<dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15" conf="optional->runtime,provided"/>
+ <dependency org="org.mongodb" name="mongo-java-driver" rev="1.2.1" conf="compile->runtime,provided"/>
+ <dependency org="org.springframework.batch" name="springbatch-over-mongodb" rev="1.0-SNAPSHOT" conf="compile->runtime,provided"/>
</dependencies>
</ivy-module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment