Spring Batch

Spring Batch Example XML to MongoDB

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<h2 dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">Spring Batch and MongoDB-<&sol;h2>&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">In Spring Batch version introduce NoSQL database support with introducing <i>org&period;springframework&period;batch&period;item&period;data<&sol;i> package which contain following classes&period;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>AbstractPaginatedDataItemReader&period;class<&sol;li>&NewLine;<li>GemfireItemWriter&period;class<&sol;li>&NewLine;<li>MongoItemReader&period;class<&sol;li>&NewLine;<li>MongoItemWriter&period;class<&sol;li>&NewLine;<li>Neo4jItemReader&period;class<&sol;li>&NewLine;<li>Neo4jItemWriter&period;class<&sol;li>&NewLine;<li>RepositoryItemReader&period;class<&sol;li>&NewLine;<li>RepositoryItemWriter&period;class<&sol;li>&NewLine;<li>SpELMappingGemfireItemWriter&period;class<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2><b>Spring Batch<&sol;b><&sol;h2>&NewLine;<p>Spring Batch is a Spring-based framework for enterprise Java batch processing&period; An important aspect of Spring Batch is the separation between reading from and writing to resources and the processing of a single record&comma; called item in the Spring Batch lingo&period; There are a lot of existing item readers and writers for a wide range of resources like JDBC databases&comma; JMS messaging systems&comma; flat file etc&period; If the resource of your choice is not supported of of the box&comma; it is easy to implement your own reader and writer as we will see in a minute&period;<&sol;p>&NewLine;<h2><b>MongoDB<&sol;b><&sol;h2>&NewLine;<p>MongoDB is a popular NoSQL datastore&period; It stores so called documents &lpar;basically an ordered set of key&sol;value pairs where a value can be a simple data type like String or integer but also an array of values or a sub document&rpar;&period; MongoDB is optimized for heavy write throughput and horizontal scaling&period;<&sol;p>&NewLine;<p>Since I am a big fan of MongoDB on the one hand and introducing the Spring Batch framework at one of my customers on the other hand&comma; why not implement a Spring Batch item reader&lpar;xml reader&rpar; and writer for MongoDB&lpar;MongoItemWriter&rpar;&period;<&sol;p>&NewLine;<h2><b>MongoDB Item Writer-<i>MongoItemWriter<&sol;i><&sol;b><&sol;h2>&NewLine;<p>My first approach to the item writer was very naive&period; I just took the DBObject item list and inserted them into the target collection&period; This can be done with the following configuration&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&excl;-- write it to MongoDB&comma; 'employee' collection &lpar;table&rpar; --&gt&semi; &NewLine; &lt&semi;bean id&equals;"mongodbItemWriter" class&equals;"org&period;springframework&period;batch&period;item&period;data&period;MongoItemWriter"&gt&semi; &NewLine; &lt&semi;property name&equals;"template" ref&equals;"mongoTemplate" &sol;&gt&semi; &NewLine; &lt&semi;property name&equals;"collection" value&equals;"employee" &sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine;<&sol;pre>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p><b>These are possible parameters&colon;<&sol;b><br &sol;>&NewLine;<i><b>template <&sol;b><&sol;i>and <b><i>collection <&sol;i><&sol;b>determine the MongoDB template and what collection to write to&period; These parameters are required&comma; all other are optional&period;<&sol;p>&NewLine;<h2><b>MongoDB Item Reader-<i>MongoItemReader<&sol;i><&sol;b><&sol;h2>&NewLine;<p>Implementing the item reader was straightforward&period; It was merely a matter of passing parameters to the underlying MongoDB driver API&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&excl;-- reader it from MongoDB&comma; 'employee' collection &lpar;table&rpar; --&gt&semi; &NewLine; &lt&semi;bean id&equals;"mongodbItemReader" class&equals;"org&period;springframework&period;batch&period;item&period;data&period;MongoItemReader"&gt&semi; &NewLine; &lt&semi;property name&equals;"template" ref&equals;"mongoTemplate" &sol;&gt&semi; &NewLine; &lt&semi;property name&equals;"query" value&equals;"&lbrace;age&colon; &lbrace;&dollar;gt&colon; 22&rcub;" &sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>We have three kinds of parameters&colon;<&sol;b><&sol;p>&NewLine;<p><b><i>template <&sol;i><&sol;b>and <i><b>collection <&sol;b><&sol;i>determine the MongoDB template and what collection to read from&period; These parameters are required&comma; all other are optional&period;<br &sol;>&NewLine;<i><b>query <&sol;b><&sol;i>and <i><b>keys <&sol;b><&sol;i>are making up the MongoDB query&period; The first one is the query itself&comma; the second one selects the field to read&period; If you don’t set a query string&comma; all documents from the collection are read&period;<&sol;p>&NewLine;<p>By default&comma; the item reader emits DBObject instances that come from the MongoDB driver API&period; These objects are basically ordered hashmaps&period; If you want to use another representation of your data in the item processor&comma; you can write a custom converter&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public class DocumentEmployeeConverter implements Converter&lt&semi;DBObject Employee&gt&semi; &lbrace; &NewLine; &NewLine; &commat;Override &NewLine; public Employee convert&lpar;DBObject document&rpar; &lbrace; &NewLine; Employee emp &equals; new Employee&lpar;&rpar;&semi; &NewLine; emp&period;setEmpid&lpar;&lpar;String&rpar;document&period;get&lpar;"&lowbar;id"&rpar;&rpar;&semi; &NewLine; emp&period;setName&lpar;&lpar;String&rpar;document&period;get&lpar;"name"&rpar;&rpar;&semi; &NewLine; emp&period;setAge&lpar;&lpar;Integer&rpar;document&period;get&lpar;"age"&rpar;&rpar;&semi; &NewLine; emp&period;setSalary&lpar;&lpar;Integer&rpar;document&period;get&lpar;"salary"&rpar;&rpar;&semi; &NewLine; emp&period;setAddress&lpar;&lpar;String&rpar;document&period;get&lpar;"address"&rpar;&rpar;&semi; &NewLine; return emp&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>Example XML File To MongoDB Database-<&sol;b><&sol;h2>&NewLine;<p>Now we will discuss how to configure a Spring Batch job to read data from an XML file &lpar;XStream library&rpar; into a no SQL database &lpar;MongoDB&rpar;&period; In additional&comma; create a unit test case to launch and test the batch jobs&period;<&sol;p>&NewLine;<h3><b>Tools and libraries used<&sol;b><&sol;h3>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Spring Tool Suite &lpar;STS&rpar;<&sol;li>&NewLine;<li>JDK 1&period;6<&sol;li>&NewLine;<li>Spring Core 3&period;2&period;2&period;RELEASE<&sol;li>&NewLine;<li>Spring OXM 3&period;2&period;2&period;RELEASE<&sol;li>&NewLine;<li>Spring Batch 2&period;2&period;0&period;RELEASE<&sol;li>&NewLine;<li>MongoDB Java Driver 2&period;7&period;3<&sol;li>&NewLine;<li>MongoDB 2&period;10&period;1<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><b>1&period; Project Directory Structure<&sol;b><&sol;h3>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2014&sol;03&sol;111-3&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<h3><b>2&period; Input XML File<&sol;b><br &sol;>&NewLine;<b>employees&period;xml<&sol;b><&sol;h3>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;employees&gt&semi; &NewLine; &lt&semi;employee&gt&semi; &NewLine; &lt&semi;address&gt&semi;delhi&lt&semi;&sol;address&gt&semi; &NewLine; &lt&semi;age&gt&semi;17&lt&semi;&sol;age&gt&semi; &NewLine; &lt&semi;empid&gt&semi;1111&lt&semi;&sol;empid&gt&semi; &NewLine; &lt&semi;name&gt&semi;ATUL KUMAR&lt&semi;&sol;name&gt&semi; &NewLine; &lt&semi;salary&gt&semi;300000&period;0&lt&semi;&sol;salary&gt&semi; &NewLine; &lt&semi;&sol;employee&gt&semi; &NewLine; &lt&semi;employee&gt&semi; &NewLine; &lt&semi;address&gt&semi;delhi&lt&semi;&sol;address&gt&semi; &NewLine; &lt&semi;age&gt&semi;27&lt&semi;&sol;age&gt&semi; &NewLine; &lt&semi;empid&gt&semi;2222&lt&semi;&sol;empid&gt&semi; &NewLine; &lt&semi;name&gt&semi;Dinesh Rajput&lt&semi;&sol;name&gt&semi; &NewLine; &lt&semi;salary&gt&semi;60000&period;0&lt&semi;&sol;salary&gt&semi; &NewLine; &lt&semi;&sol;employee&gt&semi; &NewLine; &lt&semi;employee&gt&semi; &NewLine; &lt&semi;address&gt&semi;delhi&lt&semi;&sol;address&gt&semi; &NewLine; &lt&semi;age&gt&semi;21&lt&semi;&sol;age&gt&semi; &NewLine; &lt&semi;empid&gt&semi;3333&lt&semi;&sol;empid&gt&semi; &NewLine; &lt&semi;name&gt&semi;ASHUTOSH RAJPUT&lt&semi;&sol;name&gt&semi; &NewLine; &lt&semi;salary&gt&semi;400000&period;0&lt&semi;&sol;salary&gt&semi; &NewLine; &lt&semi;&sol;employee&gt&semi; &NewLine; &lt&semi;employee&gt&semi; &NewLine; &lt&semi;address&gt&semi;Kanpur&lt&semi;&sol;address&gt&semi; &NewLine; &lt&semi;age&gt&semi;27&lt&semi;&sol;age&gt&semi; &NewLine; &lt&semi;empid&gt&semi;4444&lt&semi;&sol;empid&gt&semi; &NewLine; &lt&semi;name&gt&semi;Adesh Verma&lt&semi;&sol;name&gt&semi; &NewLine; &lt&semi;salary&gt&semi;80000&period;0&lt&semi;&sol;salary&gt&semi; &NewLine; &lt&semi;&sol;employee&gt&semi; &NewLine; &lt&semi;employee&gt&semi; &NewLine; &lt&semi;address&gt&semi;Noida&lt&semi;&sol;address&gt&semi; &NewLine; &lt&semi;age&gt&semi;37&lt&semi;&sol;age&gt&semi; &NewLine; &lt&semi;empid&gt&semi;5555&lt&semi;&sol;empid&gt&semi; &NewLine; &lt&semi;name&gt&semi;Dinesh Rajput&lt&semi;&sol;name&gt&semi; &NewLine; &lt&semi;salary&gt&semi;300000&period;0&lt&semi;&sol;salary&gt&semi; &NewLine; &lt&semi;&sol;employee&gt&semi; &NewLine;&lt&semi;&sol;employees&gt&semi; &NewLine;<&sol;pre>&NewLine;<h3><b>3&period; ItemReader for XML File<&sol;b><&sol;h3>&NewLine;<p>In this example&comma; we use Jaxb2Marshaller to map XML values and attributes to an object&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;bean id&equals;"xmlItemReader" class&equals;"org&period;springframework&period;batch&period;item&period;xml&period;StaxEventItemReader"&gt&semi; &NewLine; &lt&semi;property name&equals;"resource" value&equals;"classpath&colon;xml&sol;employees&period;xml" &sol;&gt&semi; &NewLine; &lt&semi;property name&equals;"unmarshaller" ref&equals;"empUnMarshaller" &sol;&gt&semi; &NewLine; &lt&semi;property name&equals;"fragmentRootElementName" value&equals;"employee" &sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>Employee&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;batch&period;bean&semi; &NewLine; &NewLine;import javax&period;xml&period;bind&period;annotation&period;XmlAccessOrder&semi; &NewLine;import javax&period;xml&period;bind&period;annotation&period;XmlAccessorOrder&semi; &NewLine;import javax&period;xml&period;bind&period;annotation&period;XmlRootElement&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;XmlRootElement&lpar;name&equals;"employee"&rpar; &NewLine;&commat;XmlAccessorOrder&lpar;XmlAccessOrder&period;UNDEFINED&rpar; &NewLine;public class Employee &lbrace; &NewLine; private int empid&semi; &NewLine; private String name&semi; &NewLine; private int age&semi; &NewLine; private float salary&semi; &NewLine; private String address&semi; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;return the empid &NewLine; &ast;&sol; &NewLine; public int getEmpid&lpar;&rpar; &lbrace; &NewLine; return empid&semi; &NewLine; &rcub; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param empid the empid to set &NewLine; &ast;&sol; &NewLine; public void setEmpid&lpar;int empid&rpar; &lbrace; &NewLine; this&period;empid &equals; empid&semi; &NewLine; &rcub; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;return the name &NewLine; &ast;&sol; &NewLine; public String getName&lpar;&rpar; &lbrace; &NewLine; return name&semi; &NewLine; &rcub; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param name the name to set &NewLine; &ast;&sol; &NewLine; public void setName&lpar;String name&rpar; &lbrace; &NewLine; this&period;name &equals; name&semi; &NewLine; &rcub; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;return the age &NewLine; &ast;&sol; &NewLine; public int getAge&lpar;&rpar; &lbrace; &NewLine; return age&semi; &NewLine; &rcub; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param age the age to set &NewLine; &ast;&sol; &NewLine; public void setAge&lpar;int age&rpar; &lbrace; &NewLine; this&period;age &equals; age&semi; &NewLine; &rcub; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;return the salary &NewLine; &ast;&sol; &NewLine; public float getSalary&lpar;&rpar; &lbrace; &NewLine; return salary&semi; &NewLine; &rcub; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param salary the salary to set &NewLine; &ast;&sol; &NewLine; public void setSalary&lpar;float salary&rpar; &lbrace; &NewLine; this&period;salary &equals; salary&semi; &NewLine; &rcub; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;return the address &NewLine; &ast;&sol; &NewLine; public String getAddress&lpar;&rpar; &lbrace; &NewLine; return address&semi; &NewLine; &rcub; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param address the address to set &NewLine; &ast;&sol; &NewLine; public void setAddress&lpar;String address&rpar; &lbrace; &NewLine; this&period;address &equals; address&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>4&period; MongoDB configuration<&sol;b><br &sol;>&NewLine;<b>mongodbConfig&period;xml<&sol;b><&sol;h3>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;beans xmlns&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans" &NewLine; xmlns&colon;mongo&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;data&sol;mongo" &NewLine; xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" &NewLine; xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans&sol;spring-beans-4&period;0&period;xsd &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;data&sol;mongo &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;data&sol;mongo&sol;spring-mongo-1&period;0&period;xsd"&gt&semi; &NewLine; &NewLine; &lt&semi;mongo&colon;mongo host&equals;"127&period;0&period;0&period;1" port&equals;"27017" &sol;&gt&semi; &NewLine; &lt&semi;mongo&colon;db-factory dbname&equals;"davdb" id&equals;"mongoDbFactory"&sol;&gt&semi; &NewLine; &NewLine; &lt&semi;bean id&equals;"mongoTemplate" class&equals;"org&period;springframework&period;data&period;mongodb&period;core&period;MongoTemplate"&gt&semi; &NewLine; &lt&semi;constructor-arg name&equals;"mongoDbFactory" ref&equals;"mongoDbFactory" &sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;beans&gt&semi; &NewLine;<&sol;pre>&NewLine;<h3><b>5&period; Spring Batch Core configuration<&sol;b><br &sol;>&NewLine;<b>Define <i>jobRepository <&sol;i>and <i>jobLauncher<&sol;i>&period;<&sol;b><br &sol;>&NewLine;<b>applicationContext&period;xml<&sol;b><&sol;h3>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;beans xmlns&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans" &NewLine; xmlns&colon;context&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context" &NewLine; xmlns&colon;p&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;p" &NewLine; xmlns&colon;mvc&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc" &NewLine; xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" &NewLine; xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans&sol;spring-beans-4&period;0&period;xsd &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context&sol;spring-context-4&period;0&period;xsd &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc&sol;spring-mvc-4&period;0&period;xsd"&gt&semi; &NewLine; &NewLine; &lt&semi;bean id&equals;"transactionManager" class&equals;"org&period;springframework&period;batch&period;support&period;transaction&period;ResourcelessTransactionManager"&sol;&gt&semi; &NewLine; &NewLine; &lt&semi;bean id&equals;"jobLauncher" class&equals;"org&period;springframework&period;batch&period;core&period;launch&period;support&period;SimpleJobLauncher"&gt&semi; &NewLine; &lt&semi;property name&equals;"jobRepository" ref&equals;"jobRepository"&sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine; &lt&semi;bean id&equals;"jobRepository" class&equals;"org&period;springframework&period;batch&period;core&period;repository&period;support&period;MapJobRepositoryFactoryBean"&gt&semi; &NewLine; &lt&semi;property name&equals;"transactionManager" ref&equals;"transactionManager"&sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine; &lt&semi;bean id&equals;"simpleJob" class&equals;"org&period;springframework&period;batch&period;core&period;job&period;SimpleJob" abstract&equals;"true"&gt&semi; &NewLine; &lt&semi;property name&equals;"jobRepository" ref&equals;"jobRepository" &sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;beans&gt&semi; &NewLine;<&sol;pre>&NewLine;<h3><b>6&period; Spring Batch Jobs Configuration file<&sol;b><&sol;h3>&NewLine;<p>First&comma; I define the <i><b>simple-job&period;xml<&sol;b><&sol;i> and <i><b>mongodbConfig&period;xml<&sol;b><&sol;i> files for configuration&period; In these file&comma; I specify the <i>org&period;springframework&period;batch&period;item&period;xml&period;StaxEventItemReader&comma;<&sol;i> which is a class from the Spring Batch framework&period; I specify the resource to the <i>org&period;springframework&period;batch&period;item&period;xml&period;StaxEventItemReader<&sol;i> as the path of the input xml file&period; Here I say the resource value is <b><i>classpath&colon;xmlemployees&period;xml<&sol;i><&sol;b>&comma; i&period;e&period;&comma; the location of input file <i><b>employees&period;xml<&sol;b><&sol;i>&period; I also define the <i>unmarshaller <&sol;i>object for converting xml data to java object of Employee class&period; Then I define <i>fragmentRootElementName<&sol;i>&comma; which have value employee &period; I can cater that through my defined <i><b>EmployeeFilterProcessor <&sol;b><&sol;i>class which implements the ItemProcessor class of the Spring Batch framework&period;<&sol;p>&NewLine;<p>After this&comma; I specify the MongoDB details by mentioning the hostname where the database is installed and also the port number&period; I access the database through the MongoTemplate&comma; which takes the reference of the database details mentioned through the id &lpar;i&period;e&period;&comma; Mongo as the argument&rpar;&period; In the MongoTemplate I also pass the other argument &lpar;i&period;e&period;&comma; the name of the database I will work with inside the MongoDB&rpar;&comma; and in this case it is &&num;8220&semi;new&period;&&num;8221&semi; Now I define my own class&comma; MongoDBItemWriter&comma; which is the extension of the ItemWriter class in Spring Batch&period; This class now reads the MongoTemplate to get the details of the database&period;<&sol;p>&NewLine;<p>Next&comma; I specify the DynamicJobParameters class&comma; which implements the JobParametersIncrementer from the Spring Batch&period; This works as the incrementer for the job&period;<&sol;p>&NewLine;<p>Finally&comma; I specify my batch job where I give the batch&colon;step and batch&colon;tasklet details&period; The batch job here is simpleDojJob&comma; which contains a single step that holds the tasklet where the task mentioned is to read the batch&colon;chunk from the xmlItemReader&period; I also mention the process and the itemwriter details&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;beans xmlns&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans" &NewLine; xmlns&colon;context&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context" &NewLine; xmlns&colon;p&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;p" &NewLine; xmlns&colon;batch&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;batch" &NewLine; xmlns&colon;mvc&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc" &NewLine; xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" &NewLine; xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans&sol;spring-beans-4&period;0&period;xsd &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context&sol;spring-context-4&period;0&period;xsd &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc&sol;spring-mvc-4&period;0&period;xsd &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;batch &NewLine;http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;batch&sol;spring-batch-2&period;0&period;xsd"&gt&semi; &NewLine; &NewLine; &lt&semi;import resource&equals;"applicationContext&period;xml"&sol;&gt&semi; &NewLine; &lt&semi;import resource&equals;"mongodbConfig&period;xml"&sol;&gt&semi; &NewLine; &lt&semi;bean id&equals;"employeeFilterProcessor" class&equals;"com&period;doj&period;batch&period;processor&period;EmployeeFilterProcessor"&gt&semi; &NewLine; &lt&semi;bean id&equals;"xmlItemReader" class&equals;"org&period;springframework&period;batch&period;item&period;xml&period;StaxEventItemReader"&gt&semi; &NewLine; &lt&semi;property name&equals;"resource" value&equals;"classpath&colon;xml&sol;employees&period;xml" &sol;&gt&semi; &NewLine; &lt&semi;property name&equals;"unmarshaller" ref&equals;"empUnMarshaller" &sol;&gt&semi; &NewLine; &lt&semi;property name&equals;"fragmentRootElementName" value&equals;"employee" &sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine; &lt&semi;bean id&equals;"empUnMarshaller" class&equals;"org&period;springframework&period;oxm&period;jaxb&period;Jaxb2Marshaller"&gt&semi; &NewLine; &lt&semi;property name&equals;"classesToBeBound"&gt&semi; &NewLine; &lt&semi;value&gt&semi;com&period;doj&period;batch&period;bean&period;Employee&lt&semi;&sol;value&gt&semi; &NewLine; &lt&semi;&sol;property&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine; &lt&semi;&excl;-- write it to MongoDB&comma; 'employee' collection &lpar;table&rpar; --&gt&semi; &NewLine; &lt&semi;bean id&equals;"mongodbItemWriter" class&equals;"org&period;springframework&period;batch&period;item&period;data&period;MongoItemWriter"&gt&semi; &NewLine; &lt&semi;property name&equals;"template" ref&equals;"mongoTemplate" &sol;&gt&semi; &NewLine; &lt&semi;property name&equals;"collection" value&equals;"employee" &sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine; &lt&semi;batch&colon;job id&equals;"simpleDojJob" parent&equals;"simpleJob"&gt&semi; &NewLine; &lt&semi;batch&colon;step id&equals;"step1"&gt&semi; &NewLine; &lt&semi;batch&colon;tasklet&gt&semi; &NewLine; &lt&semi;batch&colon;chunk reader&equals;"xmlItemReader" processor&equals;"employeeFilterProcessor" writer&equals;"mongodbItemWriter" commit-interval&equals;"2"&sol;&gt&semi; &NewLine; &lt&semi;&sol;batch&colon;tasklet&gt&semi; &NewLine; &lt&semi;&sol;batch&colon;step&gt&semi; &NewLine; &lt&semi;&sol;batch&colon;job&gt&semi; &NewLine;&lt&semi;&sol;beans&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>8&period; EmployeeFilterProcessor&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;batch&period;processor&semi; &NewLine; &NewLine;import org&period;springframework&period;batch&period;item&period;ItemProcessor&semi; &NewLine; &NewLine;import com&period;doj&period;batch&period;bean&period;Employee&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class EmployeeFilterProcessor implements ItemProcessor&lt&semi;Employee&comma; Employee&gt&semi; &lbrace; &NewLine; &NewLine; &commat;Override &NewLine; public Employee process&lpar;Employee emp&rpar; throws Exception &lbrace; &NewLine; if&lpar;emp&period;getSalary&lpar;&rpar; &gt&semi; 70000&period;0&rpar;&lbrace; &NewLine; return emp&semi; &NewLine; &rcub;else&lbrace; &NewLine; return null&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub;<&sol;pre>&NewLine;<h2><b>Launching Batch Job-<&sol;b><&sol;h2>&NewLine;<p>Spring Batch comes with a simple utility class called CommandLineJobRunner which has a main&lpar;&rpar; method which accepts two arguments&period; First argument is the spring application context file containing job definition and the second is the name of the job to be executed&period;<&sol;p>&NewLine;<p><b>Now run as a java application with both two arguments&period;<&sol;b><br &sol;>&NewLine;<i><b>org&period;springframework&period;batch&period;core&period;launch&period;support&period;CommandLineJobRunner<&sol;b><&sol;i><br &sol;>&NewLine;<i><b>simple-job&period;xml simpleDojJob<&sol;b><&sol;i><&sol;p>&NewLine;<p>Output&period; The Spring Batch metadata tables are created&comma; and the content of employees&period;xml is inserted into mongodb database &&num;8220&semi;davdb&&num;8221&semi; collection &&num;8220&semi;EMPLOYEE&&num;8221&semi;&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2014&sol;03&sol;111-4&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<h2><b>Download Source Code with Jars<&sol;b><br &sol;>&NewLine;<b><a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;a&sol;dineshonjava&period;com&sol;dineshonjava&sol;dineshonjava&sol;SpringBatchXMLtoMongoDB&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener">SpringBatchXMLtoMongoDB&period;zip<&sol;a><&sol;b><&sol;h2>&NewLine;<div style&equals;"text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-csv-to-mysql-database&sol;">Previuos<&sol;a>&lt&semi;&lt&semi; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;">Index <&sol;a>&gt&semi;&gt&semi; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;multiresourceitemreader-in-spring-batch&sol;">Next<&sol;a>&gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-csv-to-mysql-database&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;multiresourceitemreader-in-spring-batch&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '222'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago