Spring Batch入門程序
本章向您演示如何編輯一個基本的Spring Batch應用程序。 它將簡單地執行一個tasklet來顯示一條消息。
這個Spring Batch應用程序包含以下文件 -
- 配置文件 - 這是一個XML文件,在這個文件定義作業和作業的步驟(如果應用程序也包括讀取器和寫入器,那麼讀取器和寫入器的配置也包含在這個文件中。)
- Context.xml - 在這個文件中,我們將定義像作業存儲庫,作業啓動器和事務管理器的bean。
- Tasklet類 - 在這個類中,編寫處理代碼作業(在這個示例中,它顯示一個簡單的消息)
- Launcher類 - 在這個類中,通過運行Job啓動器來啓動批處理應用程序。
完整的項目目錄結構如下所示 -
jobconfig.xml
以下是這個Spring Batch應用程序示例的配置文件。
<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 = "com.yiibai.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
以下是Spring Batch應用程序的context.xml
文件內容。
<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>
MyTasklet.java
以下是顯示簡單消息的Tasklet類。
package com.yiibai;
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
以下是啓動批處理過程的代碼。
package com.yiibai;
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 {
// System.out.println("hello");
String[] springConfig = {"context.xml", "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());
}
}
在執行時,上面的SpringBatch程序將產生以下輸出 -
四月 27, 2018 10:09:54 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@26a1ab54: startup date [Fri Apr 27 10:09:54 CST 2018]; root of context hierarchy
四月 27, 2018 10:09:54 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [context.xml]
四月 27, 2018 10:09:54 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [jobconfig.xml]
四月 27, 2018 10:09:54 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [context.xml]
四月 27, 2018 10:09:55 上午 org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
信息: No TaskExecutor has been set, defaulting to synchronous executor.
四月 27, 2018 10:09:55 上午 org.springframework.batch.core.launch.support.SimpleJobLauncher run
信息: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}]
四月 27, 2018 10:09:55 上午 org.springframework.batch.core.job.SimpleStepHandler handleStep
信息: Executing step: [step1]
Hello This is a sample example of Spring Batch
四月 27, 2018 10:09:55 上午 org.springframework.batch.core.launch.support.SimpleJobLauncher run
信息: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
Exit Status : COMPLETED
以下是糾正/補充內容:
context.xml的第二行約束少了左雙引號 應該改爲 提交時間:2019-08-16