Spring Batch – Basic Application

Spring Batch – Basic Application ”; Previous Next This chapter shows you the basic Spring Batch application. It will simply execute a tasklet to displays a message. Our Spring Batch application contains the following files − Configuration file − This is an XML file where we define the Job and the steps of the job. (If the application involves readers and writers too, then the configuration of readers and writers is also included in this file.) Context.xml − In this file, we will define the beans like job repository, job launcher and transaction manager. Tasklet class − In this class, we will write the processing code job (In this case, it displays a simple message) Launcher class − in this class, we will launch the Batch Application by running the Job launcher. Create Project Create a new maven project as discussed in Spring Batch – Environment Chapter. pom.xml Following are the content of pom.xml file used in this maven project. <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> <groupId>com.tutorialspoint</groupId> <artifactId>SpringBatchSample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SpringBatchExample</name> <url>http://maven.apache.org</url> <properties> <jdk.version>21</jdk.version> <spring.version>5.3.14</spring.version> <spring.batch.version>4.3.4</spring.batch.version> <mysql.driver.version>5.1.25</mysql.driver.version> <junit.version>4.11</junit.version> </properties> <dependencies> <!– Spring Core –> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!– Spring jdbc, for database –> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!– Spring XML to/back object –> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <!– MySQL database driver –> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.driver.version}</version> </dependency> <!– Spring Batch dependencies –> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>${spring.batch.version}</version> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-infrastructure</artifactId> <version>${spring.batch.version}</version> </dependency> <!– Spring Batch unit test –> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <version>${spring.batch.version}</version> </dependency> <!– Junit –> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>spring-batch</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <downloadSources>true</downloadSources> <downloadJavadocs>false</downloadJavadocs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> </project> jobConfig.xml Following is the configuration file of our sample Spring Batch application. Create this file in src > main > resources folder of maven project. <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:batch = “http://www.springframework.org/schema/batch” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd “> <import resource=”context.xml” /> <!– Defining a bean –> <bean id = “tasklet” class = “MyTasklet” /> <!– Defining a job–> <batch:job id = “helloWorldJob”> <!– Defining a Step –> <batch:step id = “step1”> <tasklet ref = “tasklet”/> </batch:step> </batch:job> </beans> Context.xml Following is the context.xml of our Spring Batch application. Create this file in src > main > resources folder of maven project. <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd”> <bean id = “jobRepository” class=”org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean”> <property name = “transactionManager” ref = “transactionManager” /> </bean> <bean id = “transactionManager” class = “org.springframework.batch.support.transaction.ResourcelessTransactionManager” /> <bean id = “jobLauncher” class = “org.springframework.batch.core.launch.support.SimpleJobLauncher”> <property name = “jobRepository” ref = “jobRepository” /> </bean> </beans> Tasklet.java Following is the Tasklet class which displays a simple message. Create this class in src > main > java folder of maven project. import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.repeat.RepeatStatus; public class MyTasklet implements Tasklet { @Override public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception { System.out.println(“Hello This is a sample example of spring batch”); return RepeatStatus.FINISHED; } } App.java Following is the code which launces the batch process. Create this class in src > main > java folder of maven project. import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args)throws Exception { String[] springConfig = {“jobConfig.xml”}; // Creating the application context object ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); // Creating the job launcher JobLauncher jobLauncher = (JobLauncher) context.getBean(“jobLauncher”); // Creating the job Job job = (Job) context.getBean(“helloWorldJob”); // Executing the JOB JobExecution execution = jobLauncher.run(job, new JobParameters()); System.out.println(“Exit Status : ” + execution.getStatus()); } } Output Right click on the project in eclipse, select run as -> maven build . Set goals as clean package and run the project. You”ll see following output. [INFO] Scanning for projects… [INFO] [INFO] [1m—————-< [0;36mcom.tutorialspoint:SpringBatchSample[0;1m >—————-[m [INFO] [1mBuilding SpringBatchExample 1.0-SNAPSHOT[m [INFO] from pom.xml [INFO] [1m——————————–[ jar ]———————————[m [INFO] [INFO] [1m— [0;32mclean:3.2.0:clean[m [1m(default-clean)[m @ [36mSpringBatchSample[0;1m —[m [INFO] Deleting C:UsersTutorialspointeclipse-workspaceSpringBatchSampletarget [INFO] [INFO] [1m— [0;32mresources:3.3.1:resources[m [1m(default-resources)[m @ [36mSpringBatchSample[0;1m —[m [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 3 resources from srcmainresources to targetclasses [INFO] [INFO] [1m— [0;32mcompiler:2.3.2:compile[m [1m(default-compile)[m @ [36mSpringBatchSample[0;1m —[m [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 5 source files to C:UsersTutorialspointeclipse-workspaceSpringBatchSampletargetclasses [INFO] [INFO] [1m— [0;32mresources:3.3.1:testResources[m [1m(default-testResources)[m @ [36mSpringBatchSample[0;1m —[m [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource from srctestresources to targettest-classes [INFO] [INFO] [1m— [0;32mcompiler:2.3.2:testCompile[m [1m(default-testCompile)[m @ [36mSpringBatchSample[0;1m —[m [INFO] Nothing to compile – all classes are up to date [INFO] [INFO] [1m— [0;32msurefire:3.1.2:test[m [1m(default-test)[m @ [36mSpringBatchSample[0;1m —[m [INFO] [INFO] [1m— [0;32mjar:3.3.0:jar[m [1m(default-jar)[m @ [36mSpringBatchSample[0;1m —[m [INFO] Building jar: C:UsersTutorialspointeclipse-workspaceSpringBatchSampletargetspring-batch.jar [INFO] [1m————————————————————————[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m————————————————————————[m [INFO] Total time: 4.426 s [INFO] Finished at: 2024-07-30T11:10:59+05:30 [INFO] [1m———————————————————————— To check the output of the above SpringBatch program, right click on App.java class and select run as -> Java application. It will produce the following output − Jul 30, 2024 11:21:25 AM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet INFO: No TaskExecutor has been set, defaulting to synchronous executor. Jul 30, 2024 11:21:25 AM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run INFO: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}] Jul 30, 2024 11:21:25 AM org.springframework.batch.core.job.SimpleStepHandler handleStep INFO: Executing step: [step1] Hello This is a sample example of spring batch Jul 30, 2024 11:21:25 AM org.springframework.batch.core.step.AbstractStep execute INFO: Step: [step1] executed in 25ms Jul 30, 2024 11:21:25 AM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 47ms Exit Status : COMPLETED Print Page Previous Next Advertisements ”;

Spring Batch – Discussion

Discuss Spring Batch ”; Previous Next Spring Batch is a lightweight framework which is used to develop Batch Applications that are used in Enterprise Applications. This tutorial explains the fundamental concepts of Spring Batch and shows how you can use it in practical environment. Print Page Previous Next Advertisements ”;

Spring Batch – Environment

Spring Batch – Environment ”; Previous Next In this chapter, we will explain how to set Spring Batch environment in Eclipse IDE. Before proceeding with the installation, ensure that you have installed Eclipse in your system. If not, download and install Eclipse in your system. For more information on Eclipse, please refer our Eclipse Tutorial. Setting Spring Batch on Eclipse Follow the steps given below to set Spring Batch environment on Eclipse. Step 1 − Install Eclipse and open a New Project as shown in the following screenshot. Step 2 − Create a Sample Spring Batch project as shown below. Step 3 − Right-click on the project and convert it into a Maven project as shown below. Once you convert it into Maven project, it will give you a Pom.xml where you need to mention the required dependencies. Thereafter, the jar files of those will be automatically downloaded into your project. Step 4 − Now, in the pom.xml of the project, copy and paste the following content (dependencies for spring batch application) and refresh the project. <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> <groupId>com.tutorialspoint</groupId> <artifactId>SpringBatchSample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SpringBatchExample</name> <url>http://maven.apache.org</url> <properties> <jdk.version>1.8</jdk.version> <spring.version>5.3.14</spring.version> <spring.batch.version>4.3.4</spring.batch.version> <mysql.driver.version>5.1.25</mysql.driver.version> <junit.version>4.11</junit.version> </properties> <dependencies> <!– Spring Core –> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!– Spring jdbc, for database –> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!– Spring XML to/back object –> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <!– MySQL database driver –> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.driver.version}</version> </dependency> <!– Spring Batch dependencies –> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>${spring.batch.version}</version> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-infrastructure</artifactId> <version>${spring.batch.version}</version> </dependency> <!– Spring Batch unit test –> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <version>${spring.batch.version}</version> </dependency> <!– Junit –> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>spring-batch</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <downloadSources>true</downloadSources> <downloadJavadocs>false</downloadJavadocs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> </project> Print Page Previous Next Advertisements ”;

Spring Batch – Architecture

Spring Batch – Architecture ”; Previous Next Following is the diagrammatic representation of the architecture of Spring Batch. As depicted in the figure, the architecture contains three main components namely, Application, Batch Core, and Batch Infrastructure. Application − This component contains all the jobs and the code we write using the Spring Batch framework. Batch Core − This component contains all the API classes that are needed to control and launch a Batch Job. Batch Infrastructure − This component contains the readers, writers, and services used by both application and Batch core components. Components of Spring Batch The following illustration shows the different components of Spring Batch and how they are connected with each other. Job In a Spring Batch application, a job is the batch process that is to be executed. It runs from start to finish without interruption. This job is further divided into steps (or a job contains steps). We will configure a job in Spring Batch using an XML file or a Java class. Following is the XML configuration of a Job in Spring Batch. <job id = “jobid”> <step id = “step1” next = “step2″/> <step id = “step2” next = “step3″/> <step id = “step3″/> </job> A Batch job is configured within the tags <job></job>. It has an attribute named id. Within these tags, we define the definition and ordering of the steps. Restartable − In general, when a job is running and we try to start it again that is considered as restart and it will be started again. To avoid this, you need to set the restartable value to false as shown below. <job id = “jobid” restartable = “false” > </job> Step A step is an independent part of a job which contains the necessary information to define and execute the job (its part). As specified in the diagram, each step is composed of an ItemReader, ItemProcessor (optional) and an ItemWriter. A job may contain one or more steps. Readers, Writers, and Processors An item reader reads data into a Spring Batch application from a particular source, whereas an item writer writes data from the Spring Batch application to a particular destination. An Item processor is a class which contains the processing code which processes the data read into the spring batch. If the application reads “n” records, then the code in the processor will be executed on each record. When no reader and writer are given, a tasklet acts as a processor for SpringBatch. It processes only a single task. For example, if we are writing a job with a simple step in it where we read data from MySQL database and process it and write it to a file (flat), then our step uses − A reader which reads from MySQL database. A writer which writes to a flat file. A custom processor which processes the data as per our wish. <job id = “helloWorldJob”> <step id = “step1”> <tasklet> <chunk reader = “mysqlReader” writer = “fileWriter” processor = “CustomitemProcessor” ></chunk> </tasklet> </step> </ job> Spring Batch provides a long list of readers and writers. Using these predefined classes, we can define beans for them. We will discuss readers and writers in greater detail in the coming chapters. JobRepository A Job repository in Spring Batch provides Create, Retrieve, Update, and Delete (CRUD) operations for the JobLauncher, Job, and Step implementations. We will define a job repository in an XML file as shown below. <job-repository id = “jobRepository”/> In addition to id, there are some more options (optional) available. Following is the configuration of job repository with all the options and their default values. <job-repository id = “jobRepository” data-source = “dataSource” transaction-manager = “transactionManager” isolation-level-for-create = “SERIALIZABLE” table-prefix = “BATCH_” max-varchar-length = “1000”/> In-Memory Repository − In case you don’t want to persist the domain objects of the Spring Batch in the database, you can configure the in-memory version of the jobRepository as shown below. <bean id = “jobRepository” class = “org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean “> <property name = “transactionManager” ref = “transactionManager”/> </bean> JobLauncher JobLauncher is an interface which launces the Spring Batch job with the given set of parameters. SampleJoblauncher is the class which implements the JobLauncher interface. Following is the configuration of the JobLauncher. <bean id = “jobLauncher” class = “org.springframework.batch.core.launch.support.SimpleJobLauncher”> <property name = “jobRepository” ref = “jobRepository” /> </bean> JobInstance A JobInstance represents the logical run of a job; it is created when we run a job. Each job instance is differentiated by the name of the job and the parameters passed to it while running. If a JobInstance execution fails, the same JobInstance can be executed again. Hence, each JobInstance can have multiple job executions. JobExecution and StepExecution JobExecution and StepExecution are the representation of the execution of a job/step. They contain the run information of the job/step such as start time (of job/step), end time (of job/step). Print Page Previous Next Advertisements ”;

Spring Batch – XML to MySQL

Spring Batch – XML to MySQL ”; Previous Next In this chapter, we will create a Spring Batch application which uses an XML Reader and a MySQL Writer. Reader − The reader we are using in the application is StaxEventItemReader to read data from XML documents. Following is the input XML document we are using in this application. This document holds data records which specify details like tutorial id, tutorial author, tutorial title, submission date, tutorial icon, and tutorial description. <?xml version=”1.0″ encoding=”UTF-8″?> <tutorials> <tutorial> <tutorial_id>1001</tutorial_id> <tutorial_author>Sanjay</tutorial_author> <tutorial_title>Learn Java</tutorial_title> <submission_date>06-05-2007</submission_date> <tutorial_icon>https://www.tutorialspoint.com/java/images/java-minilogo.jpg</tutorial_icon> <tutorial_description>Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms. This tutorial gives a complete understanding of Java.”);</tutorial_description> </tutorial> <tutorial> <tutorial_id>1002</tutorial_id> <tutorial_author>Abdul S</tutorial_author> <tutorial_title>Learn MySQL</tutorial_title> <submission_date>19-04-2007</submission_date> <tutorial_icon>https://www.tutorialspoint.com/mysql/images/mysql-minilogo.jpg</tutorial_icon> <tutorial_description>MySQL is the most popular Open Source Relational SQL database management system. MySQL is one of the best RDBMS being used for developing web-based software applications. This tutorial will give you quick start with MySQL and make you comfortable with MySQL programming.</tutorial_description> </tutorial> <tutorial> <tutorial_id>1003</tutorial_id> <tutorial_author>Krishna Kasyap</tutorial_author> <tutorial_title>Learn JavaFX</tutorial_title> <submission_date>06-07-2017</submission_date> <tutorial_icon>https://www.tutorialspoint.com/javafx/images/javafx-minilogo.jpg</tutorial_icon> <tutorial_description>JavaFX is a Java library used to build Rich Internet Applications. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc. This tutorial, discusses all the necessary elements of JavaFX that are required to develop effective Rich Internet Applications</tutorial_description> </tutorial> </tutorials> Writer − The writer we are using in the application is JdbcBatchItemWriter to write the data to MySQL database. Assume we have created a table in MySQL inside a database called “details”. CREATE TABLE details.TUTORIALS( tutorial_id int(10) NOT NULL, tutorial_author VARCHAR(20), tutorial_title VARCHAR(50), submission_date VARCHAR(20), tutorial_icon VARCHAR(200), tutorial_description VARCHAR(1000) ); Processor − The processor we are using in the application is a custom processor which writes the data of each record on the PDF document. In batch process, if “n” records or data elements were read, then for each record, it will read the data, process it, and write the data in the Writer. To process the data, it relays on the processor passed. In this case, in the custom processor class, we have written code to load a particular PDF document, create a new page, write the data item onto the PDF in a tabular format. Finally, if you execute this application, it reads all the data items from the XML document, stores them in the MySQL database, and prints them in the given PDF document in individual pages. jobConfig.xml Following is the configuration file of our sample Spring Batch application. In this file, we will define the Job and the steps. In addition to these, we also define the beans for ItemReader, ItemProcessor, and ItemWriter. (Here, we associate them with their respective classes and pass the values for the required properties to configure them.) <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:batch = “http://www.springframework.org/schema/batch” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xmlns:util = “http://www.springframework.org/schema/util” xsi:schemaLocation = “http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd “> <import resource = “../jobs/context.xml” /> <bean id = “itemProcessor” class = “CustomItemProcessor” /> <batch:job id = “helloWorldJob”> <batch:step id = “step1”> <batch:tasklet> <batch:chunk reader = “xmlItemReader” writer = “mysqlItemWriter” processor = “itemProcessor”> </batch:chunk> </batch:tasklet> </batch:step> </batch:job> <bean id = “xmlItemReader” class = “org.springframework.batch.item.xml.StaxEventItemReader”> <property name = “fragmentRootElementName” value = “tutorial” /> <property name = “resource” value = “classpath:resources/tutorial.xml” /> <property name = “unmarshaller” ref = “customUnMarshaller” /> </bean> <bean id = “customUnMarshaller” class = “org.springframework.oxm.xstream.XStreamMarshaller”> <property name = “aliases”> <util:map id = “aliases”> <entry key = “tutorial” value = “Tutorial” /> </util:map> </property> </bean> <bean id = “mysqlItemWriter” class = “org.springframework.batch.item.database.JdbcBatchItemWriter”> <property name = “dataSource” ref = “dataSource” /> <property name = “sql”> <value> <![CDATA[insert into details.tutorials (tutorial_id, tutorial_author, tutorial_title, submission_date, tutorial_icon, tutorial_description) values (:tutorial_id, :tutorial_author, :tutorial_title, :submission_date, :tutorial_icon, :tutorial_description);]]> </value> </property> <property name = “itemSqlParameterSourceProvider”> <bean class = “org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider” /> </property> </bean> </beans> Context.xml Following is the context.xml of our Spring Batch application. In this file, we will define the beans like job repository, job launcher, and transaction manager. <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:jdbc = “http://www.springframework.org/schema/jdbc” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd”> <!– stored job-meta in database –> <bean id = “jobRepository” class = “org.springframework.batch.core.repository.support.JobRepositoryFactoryBean”> <property name = “dataSource” ref = “dataSource” /> <property name = “transactionManager” ref = “transactionManager” /> <property name = “databaseType” value = “mysql” /> </bean> <bean id = “transactionManager” class = “org.springframework.batch.support.transaction.ResourcelessTransactionMana ger” /> <bean id = “jobLauncher” class = “org.springframework.batch.core.launch.support.SimpleJobLauncher”> <property name = “jobRepository” ref = “jobRepository” /> </bean> <!– connect to MySQL database –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.jdbc.Driver” /> <property name = “url” value = “jdbc:mysql://localhost:3306/details” /> <property name = “username” value = “myuser” /> <property name = “password” value = “password” /> </bean> <!– create job-meta tables automatically –> <jdbc:initialize-database data-source = “dataSource”> <jdbc:script location = “org/springframework/batch/core/schema-drop-mysql.sql”/> <jdbc:script location = “org/springframework/batch/core/schema-mysql.sql”/> </jdbc:initialize-database> </beans> CustomItemProcessor.java Following is the processor class. In this class, we write the code of processing in the application. Here, we are loading a PDF document, creating a new page, creating a table, and inserting the following values for each record: tutorial id, tutorial name, author, date of submission in the table. import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.springframework.batch.item.ItemProcessor; public class CustomItemProcessor implements ItemProcessor<Tutorial, Tutorial> { public static void drawTable(PDPage page, PDPageContentStream contentStream, float y, float margin, String[][] content) throws IOException { final int rows = content.length; final int cols = content[0].length; final float rowHeight = 50; final float tableWidth = page.getMediaBox().getWidth()-(2*margin); final float tableHeight = rowHeight * rows; final float colWidth = tableWidth/(float)cols; final float cellMargin=5f; // draw the rows float nexty = y ; for (int i = 0; i <= rows; i++) { contentStream.drawLine(margin,nexty,margin+tableWidth,nexty); nexty-= rowHeight; } //draw the columns float nextx = margin; for (int i = 0; i <= cols; i++) { contentStream.drawLine(nextx,y,nextx,y-tableHeight); nextx += colWidth; } // now add the text contentStream.setFont(PDType1Font.HELVETICA_BOLD,12); float textx = margin+cellMargin; float texty = y-15; for(int i = 0; i < content.length; i++){ for(int j

Spring Batch – MySQL to Flat File

Spring Batch – MySQL to Flat File ”; Previous Next In this chapter, we will create a Spring Batch application which uses an MySQL Reader and a Flatfile Writer (.txt ). Reader − The Reader we are using in the application is JdbcCursorItemReader to read data from MySQL database. Assume we have created a table in the MySQL database as shown below. CREATE TABLE details.xml_mysql( person_id int(10) NOT NULL, sales VARCHAR(20), qty int(3), staffName VARCHAR(20), date VARCHAR(20) ); Assume we have inserted the following records into it. mysql> select * from tutorialsdata; +————-+—————–+—————-+—————–+ | tutorial_id | tutorial_author | tutorial_title | submission_date | +————-+—————–+—————-+—————–+ | 101 | Sanjay | Learn Java | 06-05-2007 | | 102 | Abdul S | Learn MySQL | 19-04-2007 | | 103 | Krishna Kasyap | Learn JavaFX | 06-07-2017 | +————-+—————–+—————-+—————–+ 3 rows in set (0.00 sec) Writer − The Writer we are using in the application is FlatFileItemWriter to write the data to flatfile (.txt). Processor − The Processor we are using in the application is a custom processor which just prints the records read from the CSV file. jobConfig.xml Following is the configuration file of our sample Spring Batch application. In this file, we will define the Job and the Steps. In addition to these, we also define the beans for ItemReader, ItemProcessor, and ItemWriter. (Here, we associate them with respective classes and pass the values for the required properties to configure them.) <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:batch = “http://www.springframework.org/schema/batch” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xmlns:util = “http://www.springframework.org/schema/util” xsi:schemaLocation = “http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd”> <import resource = “../jobs/context.xml” /> <bean id = “tutorial” class = “Tutorial” scope = “prototype” /> <bean id = “itemProcessor” class = “CustomItemProcessor” /> <batch:job id = “helloWorldJob”> <batch:step id = “step1”> <batch:tasklet> <batch:chunk reader = “mysqlItemReader” writer = “flatFileItemWriter” processor = “itemProcessor” commit-interval = “10”> </batch:chunk> </batch:tasklet> </batch:step> </batch:job> <bean id = “mysqlItemReader” class = “org.springframework.batch.item.database.JdbcCursorItemReader” > <property name = “dataSource” ref = “dataSource” /> <property name = “sql” value = “select * from details.tutorialsdata” /> <property name = “rowMapper”> <bean class = “TutorialRowMapper” /> </property> </bean> <bean id = “flatFileItemWriter” class = ” org.springframework.batch.item.file.FlatFileItemWriter”> <property name = “resource” value = “file:target/outputfiles/employee_output.txt”/> <property name = “lineAggregator”> <bean class = ” org.springframework.batch.item.file.transform.PassThroughLineAggregator”/> </property> </bean> </beans> Context.xml Following is the context.xml of our Spring Batch application. In this file, we will define the beans like job repository, job launcher, and transaction manager. <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xmlns:jdbc = “http://www.springframework.org/schema/jdbc” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd “> <!– stored job-meta in database –> <bean id = “jobRepository” class = “org.springframework.batch.core.repository.support.JobRepositoryFactoryBean”> <property name = “dataSource” ref = “dataSource” /> <property name = “transactionManager” ref = “transactionManager” /> <property name = “databaseType” value = “mysql” /> </bean> <bean id = “transactionManager” class = “org.springframework.batch.support.transaction.ResourcelessTransactionManager” /> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.jdbc.Driver” /> <property name = “url” value = “jdbc:mysql://localhost:3306/details” /> <property name = “username” value = “myuser” /> <property name = “password” value = “password” /> </bean> <bean id = “jobLauncher” class = “org.springframework.batch.core.launch.support.SimpleJobLauncher”> <property name = “jobRepository” ref = “jobRepository” /> </bean> <!– create job-meta tables automatically –> <jdbc:initialize-database data-source = “dataSource”> <jdbc:script location = “org/springframework/batch/core/schema-drop-mysql.sql” /> <jdbc:script location = “org/springframework/batch/core/schema-mysql.sql” /> </jdbc:initialize-database> </beans> CustomItemProcessor.java Following is the Processor class. In this class, we write the code of processing in the application. Here, we are printing the contents of each record. import org.springframework.batch.item.ItemProcessor; // Implementing the ItemProcessor interface public class CustomItemProcessor implements ItemProcessor<Tutorial, Tutorial> { @Override public Tutorial process(Tutorial item) throws Exception { System.out.println(“Processing…” + item); return item; } } TutorialRowMapper.java Following is the TutorialRowMapper class which sets the data to the Tutorial class. public class TutorialRowMapper implements RowMapper<Tutorial> { @Override public Tutorial mapRow(ResultSet rs, int rowNum) throws SQLException { Tutorial tutorial = new Tutorial(); tutorial.setTutorial_id(rs.getInt(“tutorial_id”)); tutorial.setTutorial_title(rs.getString(“tutorial_title”)); tutorial.setTutorial_author(rs.getString(“tutorial_author”)); tutorial.setSubmission_date(rs.getString(“submission_date”)); return tutorial; } } Tutorial.java Following is the Tutorial class. It is a simple Java class with setter and getter methods. In this class, we are using annotations to associate the methods of this class with the tags of the XML file. public class Tutorial { private int tutorial_id; private String tutorial_title; private String tutorial_author; private String submission_date; public int getTutorial_id() { return tutorial_id; } public void setTutorial_id(int tutorial_id) { this.tutorial_id = tutorial_id; } public String getTutorial_title() { return tutorial_title; } public void setTutorial_title(String tutorial_title) { this.tutorial_title = tutorial_title; } public String getTutorial_author() { return tutorial_author; } public void setTutorial_author(String tutorial_author) { this.tutorial_author = tutorial_author; } public String getSubmission_date() { return submission_date; } public void setSubmission_date(String submission_date) { this.submission_date = submission_date; } @Override public String toString() { return ” [id=” + tutorial_id + “, title=” + tutorial_title + “, author=” + tutorial_author + “, date=” + submission_date + “]”; } } App.java Following is the code which launces the batch process. In this class, we will launch the Batch Application by running the JobLauncher. import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) throws Exception { String[] springConfig = { “jobs/job_hello_world.xml” }; // Creating the application context object ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); // Creating the job launcher JobLauncher jobLauncher = (JobLauncher) context.getBean(“jobLauncher”); // Creating the job Job job = (Job) context.getBean(“helloWorldJob”); // Executing the JOB JobExecution execution = jobLauncher.run(job, new JobParameters()); System.out.println(“Exit Status : ” + execution.getStatus()); } } On executing this application, it will produce the following output. May 09, 2017 5:44:48 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXml ApplicationContext@3d646c37: startup date [Tue May 09 17:44:48 IST 2017]; root of context hierarchy May 09, 2017 5:44:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions May 09, 2017 5:44:56 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run INFO: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}] May 09, 2017 5:44:56 PM org.springframework.batch.core.job.SimpleStepHandler handleStep INFO: Executing step: [step1] Processing…Report [id=101, title=Learn Java, author=Sanjay, date=06-05-2007] Processing…Report [id=102, title=Learn MySQL, author=Abdul S, date=19-04-2007] Processing…Report [id=103, title=Learn JavaFX, author=Krishna Kasyap, date=0607-2017] May 09, 2017 5:44:57 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following

Spring Batch – Overview

Spring Batch – Overview ”; Previous Next Batch processing is a processing mode which involves execution of series of automated complex jobs without user interaction. A batch process handles bulk data and runs for a long time. Several Enterprise applications require to process huge data to perform operations involving − Time-based events such as periodic calculations. Periodic applications that are processed repetitively over large datasets. Applications that deals with processing and validation of the data available in a transactional manner. Therefore, batch processing is used in enterprise applications to perform such transactions. What is Spring Batch Spring batch is a lightweight framework which is used to develop Batch Applications that are used in Enterprise Applications. In addition to bulk processing, this framework provides functions for − Including logging and tracing Transaction management Job processing statistics Job restart Skip and Resource management You can also scale spring batch applications using its portioning techniques. Features of Spring Batch Following are the notable features of Spring Batch − Flexibility − Spring Batch applications are flexible. You simply need to change an XML file to alter the order of processing in an application. Maintainability − Spring Batch applications are easy to maintain. A Spring Batch job includes steps and each step can be decoupled, tested, and updated, without effecting the other steps. Scalability − Using the portioning techniques, you can scale the Spring Batch applications. These techniques allow you to − Execute the steps of a job in parallel. Execute a single thread in parallel. Reliability − In case of any failure, you can restart the job from exactly where it was stopped, by decoupling the steps. Support for multiple file formats − Spring Batch provides support for a large set of readers and writers such as XML, Flat file, CSV, MYSQL, Hibernate, JDBC, Mongo, Neo4j, etc. Multiple ways to launch a job − You can launch a Spring Batch job using web applications, Java programs, Command Line, etc. In addition to these, Spring Batch applications support − Automatic retry after failure. Tracking status and statistics during the batch execution and after completing the batch processing. To run concurrent jobs. Services such as logging, resource management, skip, and restarting the processing. Print Page Previous Next Advertisements ”;

Spring Batch – Home

Spring Batch Tutorial PDF Version Quick Guide Resources Job Search Discussion Spring Batch is a lightweight framework which is used to develop Batch Applications that are used in Enterprise Applications. This tutorial explains the fundamental concepts of Spring Batch and shows how you can use it in practical environment. Audience This tutorial is particularly going to be useful for all those professionals who are required to process large volumes of records involving repetitive actions such as transaction management, job processing statistics, resource management, etc. Spring Batch is a very effective framework for processing high-volume batch jobs. Prerequisites Spring Batch has been built upon Spring Framework, therefore you should have prior exposure to the features and functions of Spring. In case you are not familiar with Spring Framework, then you can start here. Print Page Previous Next Advertisements ”;

Spring Batch – Useful Resources

Spring Batch – Useful Resources ”; Previous Next The following resources contain additional information on Spring Batch. Please use them to get more in-depth knowledge on this. Useful Video Courses Spring Framework Mastery Course Best Seller 103 Lectures 8 hours Karthikeya T More Detail JWT Role-Based Authorization With Spring Boot and Angular 8 54 Lectures 3.5 hours Senol Atac More Detail Learn Spring Core Framework the Easy Way! 82 Lectures 5 hours Karthikeya T More Detail Spring MVC Essentials: A Primary Course for Java Spring MVC 25 Lectures 3.5 hours TELCOMA Global More Detail Spring Core (Spring Framework): An In-Depth Hands-on Guide 28 Lectures 4.5 hours TELCOMA Global More Detail Java Spring Framework 5 – Build A Web App Step By Step Best Seller 122 Lectures 11 hours Damian Jedrzejak More Detail Print Page Previous Next Advertisements ”;

Spring Batch – MySQL to XML

Spring Batch – MySQL to XML ”; Previous Next In this chapter, we will create a Spring Batch application which uses a MySQL reader and an XML Writer. Reader − The reader we are using in the application is JdbcCursorItemReader to read data from MySQL database. Assume we have created a table in the MySQL database as shown below − CREATE TABLE details.xml_mysql( person_id int(10) NOT NULL, sales VARCHAR(20), qty int(3), staffName VARCHAR(20), date VARCHAR(20) ); Assume we have inserted the following records in to it. mysql> select * from tutorialsdata; +————-+—————–+—————-+—————–+ | tutorial_id | tutorial_author | tutorial_title | submission_date | +————-+—————–+—————-+—————–+ | 101 | Sanjay | Learn Java | 06-05-2007 | | 102 | Abdul S | Learn MySQL | 19-04-2007 | | 103 | Krishna Kasyap | Learn JavaFX | 06-07-2017 | +————-+—————–+—————-+—————–+ 3 rows in set (0.00 sec) Writer − The Writer we are using in the application is StaxEventItemWriter to write the data to the XML file. Processor − The Processor we are using in the application is a custom processor which just prints the records read from the CSV file. jobConfig.xml Following is the configuration file of our sample Spring Batch application. In this file, we will define the Job and the Steps. In addition to these, we also define the beans for ItemReader, ItemProcessor, and ItemWriter. (Here, we associate them with their respective classes and pass the values for the required properties to configure them.) <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:batch = “http://www.springframework.org/schema/batch” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xmlns:util = “http://www.springframework.org/schema/util” xsi:schemaLocation = ” http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd”> <import resource = “../jobs/context.xml” /> <bean id = “report” class = “Report” scope = “prototype” /> <bean id = “itemProcessor” class = “CustomItemProcessor” /> <batch:job id = “helloWorldJob”> <batch:step id = “step1”> <batch:tasklet> <batch:chunk reader = “dbItemReader” writer = “mysqlItemWriter” processor = “itemProcessor” commit-interval = “10”> </batch:chunk> </batch:tasklet> </batch:step> </batch:job> <bean id = “dbItemReader” class = “org.springframework.batch.item.database.JdbcCursorItemReader” scope = “step”> <property name = “dataSource” ref = “dataSource” /> <property name = “sql” value = “select * from tutorials_data” /> <property name = “rowMapper”> <bean class = “TutorialRowMapper” /> </property> </bean> <bean id = “mysqlItemWriter” class = “org.springframework.batch.item.xml.StaxEventItemWriter”> <property name = “resource” value = “file:xml/outputs/tutorials.xml” /> <property name = “marshaller” ref = “reportMarshaller” /> <property name = “rootTagName” value = “Tutorial” /> </bean> <bean id = “reportMarshaller” class = “org.springframework.oxm.jaxb.Jaxb2Marshaller”> <property name = “classesToBeBound”> <list> <value>Tutorial</value> </list> </property> </bean> </beans> Context.xml Following is the context.xml of our Spring Batch application. In this file, we will define the beans like job repository, job launcher, and transaction manager. <beans xmlns = ” http://www.springframework.org/schema/beans” xmlns:jdbc = “http://www.springframework.org/schema/jdbc” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd “> <!– stored job-meta in database –> <bean id = “jobRepository” class = “org.springframework.batch.core.repository.support.JobRepositoryFactoryBean”> <property name = “dataSource” ref = “dataSource” /> <property name = “transactionManager” ref = “transactionManager” /> <property name = “databaseType” value = “mysql” /> </bean> <bean id = “transactionManager” class = “org.springframework.batch.support.transaction.ResourcelessTransactionMana ger” /> <bean id = “jobLauncher” class = “org.springframework.batch.core.launch.support.SimpleJobLauncher”> <property name = “jobRepository” ref = “jobRepository” /> </bean> <!– connect to MySQL database –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.jdbc.Driver” /> <property name = “url” value = “jdbc:mysql://localhost:3306/details” /> <property name = “username” value = “myuser” /> <property name = “password” value = “password” /> </bean> <!– create job-meta tables automatically –> <jdbc:initialize-database data-source = “dataSource”> <jdbc:script location = “org/springframework/batch/core/schema-drop-mysql.sql” /> <jdbc:script location = “org/springframework/batch/core/schema-mysql.sql” /> </jdbc:initialize-database> </beans> CustomItemProcessor.java Following is the Processor class. In this class, we write the code of processing in the application. Here, we are printing the contents of each record. import org.springframework.batch.item.ItemProcessor; public class CustomItemProcessor implements ItemProcessor<Tutorial, Tutorial> { @Override public Tutorial process(Tutorial item) throws Exception { System.out.println(“Processing…” + item); return item; } } TutorialRowMapper.java Following is the TutorialRowMapper class which sets the data to the Tutorial class. import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class TutorialRowMapper implements RowMapper<Tutorial> { @Override public Tutorial mapRow(ResultSet rs, int rowNum) throws SQLException { Tutorial tutorial = new Tutorial(); tutorial.setTutorial_id(rs.getInt(“tutorial_id”)); tutorial.setTutorial_author(rs.getString(“tutorial_author”)); tutorial.setTutorial_title(rs.getString(“tutorial_title”)); tutorial.setSubmission_date(rs.getString(“submission_date”)); return tutorial; } } Tutorial.java Following is the Tutorial class. It is a simple Java class with setter and getter methods. In this class, we are using annotations to associate the methods of this class with the tags of the XML file. import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = “details”) public class Tutorial { int tutorial_id; String tutorial_author; String submission_date; @XmlAttribute(name = “tutorial_id”) public int getTutorial_id() { return tutorial_id; } public void setTutorial_id(int tutorial_id) { this.tutorial_id = tutorial_id; } @XmlElement(name = “tutorial_author”) public String getTutorial_author() { return tutorial_author; } public void setTutorial_author(String tutorial_author) { this.tutorial_author = tutorial_author; } @XmlElement(name = “tutorial_title”) public String getTutorial_title() { return tutorial_title; } public void setTutorial_title(String tutorial_title) { this.tutorial_title = tutorial_title; } @XmlElement(name = “submission_date”) public String getSubmission_date() { return submission_date; } public void setSubmission_date(String submission_date) { this.submission_date = submission_date; } public String toString() { return ” [Tutorial Id=” + tutorial_id + “, Tutorial Author =” + tutorial_author + “, Tutorial Title =” + tutorial_title + “, Submission Date =” + submission_date + “]”; } } App.java Following is the code which launces the batch process. In this class, we will launch the Batch Application by running the JobLauncher. import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) throws Exception { String[] springConfig = { “jobs/job_hello_world.xml” }; // Creating the application context object ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); // Creating the job launcher JobLauncher jobLauncher = (JobLauncher) context.getBean(“jobLauncher”); // Creating the job Job job = (Job) context.getBean(“helloWorldJob”); // Executing the JOB JobExecution execution = jobLauncher.run(job, new JobParameters()); System.out.println(“Exit Status : ” + execution.getStatus()); } } On executing this application, it will produce the following output. May 08, 2017 11:32:06 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3d646c37: startup date [Mon May 08 11:32:06 IST 2017]; root of context hierarchy May 08, 2017 11:32:06 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [jobs/job_hello_world.xml] May 08, 2017