本章向您演示如何編輯一個基本的Spring Batch應用程式。 它將簡單地執行一個tasklet來顯示一條訊息。
這個Spring Batch應用程式包含以下檔案 -
完整的專案目錄結構如下所示 -
以下是這個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>
以下是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>
以下是顯示簡單訊息的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;
}
}
以下是啟動批次處理過程的程式碼。
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