Spring Batch入门程序
精华
小牛编辑
161浏览
2023-03-14
本章向您演示如何编辑一个基本的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