Spring batch sample (Hello World) (3)

Source: Internet
Author: User

Use the previous two articles about spring batchArticleYou should have a preliminary idea about spring batch. This article will use "Hello world !" For example, we will discuss some basic configurations and implementations of spring batch. Let everyone have a real understanding of spring batch from the perspective of development.

Note: 1. This instance uses spring-Batch 2.1.8.

2. In this example, the itemreader, itemprocessor, and itemwriter are not configured as described above. Instead, the tasklet is called in step, and the tasklet completes "Hello World !" .

The engineering structure is as follows:

The joblunch. Java class is used to start bath and writetasklet. Java is used to complete the output. Application. XML is used to configure some spring information, and batch. XML is used to configure job information.

The configuration of the application. xml file is as follows:

 <? XML version = "1.0" encoding = "UTF-8"  ?> 
< Beans Xmlns = "Http://www.springframework.org/schema/beans"
Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance" Xmlns: P = "Http://www.springframework.org/schema/p"
Xmlns: TX = "Http://www.springframework.org/schema/tx" Xmlns: AOP = "Http://www.springframework.org/schema/aop"
Xmlns: Context = "Http://www.springframework.org/schema/context"
Xsi: schemalocation = "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd"
Default-autowire = "Byname" >

< Bean ID = "Jobluncher" Class = "Org. springframework. batch. Core. Launch. Support. simplejobluncher" >
< Property Name = "Jobrepository" Ref = "Jobrepository" />
</ Bean >

< Bean ID = "Jobrepository" Class = "Org. springframework. batch. Core. repository. Support. mapjobrepositoryfactorybean" >
</ Bean >

< Bean ID = "Transactionmanager"
Class = "Org. springframework. batch. Support. transaction. resourcelesstransactionmanager" />
</ Beans >

Jobluncher is responsible for batch startup, jobrepository is responsible for crud operations throughout the job running process, and transactionmanager is responsible for transaction management operations.

The configuration of the batch. xml file is as follows:

 <?  XML version = "1.0" encoding = "UTF-8" ?> 
< BEAN: Beans Xmlns = "Http://www.springframework.org/schema/batch"
Xmlns: Bean = "Http://www.springframework.org/schema/beans" Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns: P = "Http://www.springframework.org/schema/p" Xmlns: TX = "Http://www.springframework.org/schema/tx"
Xmlns: AOP = "Http://www.springframework.org/schema/aop" Xmlns: Context = "Http://www.springframework.org/schema/context"
Xsi: schemalocation = "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/batch
Http://www.springframework.org/schema/batch/spring-batch-2.1.xsd" >

< BEAN: Import Resource = "Applicationcontext. xml" />

< Job ID = "Helloworldjob" >
< Step ID = "Step_hello" Next = "Step_world" >
< Tasklet Ref = "Hello" Transaction-Manager = "Transactionmanager" > </ Tasklet >
</ Step >
< Step ID = "Step_world" >
< Tasklet Ref = "World" Transaction-Manager = "Transactionmanager" > </ Tasklet >
</ Step >
</ Job >

< BEAN: Bean ID = "Hello" Class = "Com. wanggc. springbatch. sample. helloworld. writetasklet" >
< BEAN: Property Name = "Message" Value = "Hello" > </ BEAN: Property >
</ BEAN: Bean >

< BEAN: Bean ID = "World" Class = "Com. wanggc. springbatch. sample. helloworld. writetasklet" >
< BEAN: Property Name = "Message" Value = "World! " > </ BEAN: Property >
</ BEAN: Bean >
</ BEAN: Beans >

A job with the ID helloworldjob is configured. This job has two steps: step_hello and step_world. The former outputs "hello", and the latter outputs "World !", After the first step is complete, execute the second step.

Writetasklet classCodeAs follows:

 Public   Class WritetaskletImplements Tasklet {

/** Message */
Private String message;

/**
* @ Param Message
* The message to set
*/
Public Void Setmessage (string message ){
This . Message = message;
}

@ Override
Public Repeatstatus execute (stepcontribution arg0, chunkcontext arg1)
Throws Exception {
System. Out. println (Message );
Return Repeatstatus. finished;
}

}

This class defines a message attribute and injects values into it through the "hello" and "world" beans of batch. xml. The execute method is inherited from the tasklet interface and is the place where tasklet implements business logic. This instance simply outputs the message and returns it directly.

The code for the startup class joblunch is as follows:

Public   Class Joblunch {

/**
* @ Param ARGs
*/
Public Static Void Main (string [] ARGs ){
Applicationcontext context = New Classpathxmlapplicationcontext (
"Batch. xml ");
Jobluncher launcher = (jobluncher) Context. getbean ("jobluncher ");
Job job = (job) Context. getbean ("helloworldjob ");

Try {
/* Run job */
Jobexecution result = launcher. Run (job, New Jobparameters ());
/* Processing is complete. The console prints the processing result. */
System. Out. println (result. tostring ());
}Catch (Exception e ){
E. printstacktrace ();
}
}
}

In this example, jobluncher and job object are obtained through spring configuration, and job is started by the run method of jobluncher. The jobparameters parameter indicates some parameters of the job. After processing, the console outputs the processing result.

The above is to run a "hello World" Through springbatch"ProgramThe basic configurations required. Its advantage is to process large volumes of data. Therefore, writing so many code and configuration files just to output "Hello World" does not seem clumsy, nor can it express its superiority.

Next time, we will read a CSV file, perform simple processing, and then write an instance of another CSV file to discuss the springbatch application with you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.