我是Spring批处理的新手,正在寻找一些开发 Spring批处理注释概念的例子。
此链接(click)讨论Spring批处理,但不是带有注释概念的Spring批处理。正如在给定的链接文档中所讨论的那样,目我正在使用最新的Spring框架。我想避免xml配置。
Spring批处理是批处理的好工具吗?或者有没有更好的工具可用于批处理而不是Spring批处理?
Spring批次是否有任何限制?
答案 0 :(得分:3)
Spring批处理仅支持您可以使用注释配置的有限功能。主要是这些是监听器回调,例如@BeforeStep
或@AfterChunk
。使用这些注释可以选择实现相应的接口或使用带注释的方法。必须将带注释的bean注入作业,步骤或块(reader
,processor
,writer
,retry-policy
,skip-policy
或listeners
)中XML配置,这是你无法避免的。
答案 1 :(得分:2)
我与Spring Batch
合作了大约2。5年,我建议你使用xml文件来配置你的工作而不是注释。当然,您可以为许多配置使用注释,例如step
和job
定义,作业启动器和作业存储库定义,{{1} }和tasklet
,listeners
以及许多其他必要的组件。但遗憾的是,当您被迫使用deciders
时,我发现很难将组件连接在一起。在调试的情况下,您可以通过注释获得利润,但是在xml的情况下跟踪您的工作流程会更加容易,因为您在一个xml文件中拥有所有配置(如果您想要制作更多文件,则可能更多它更具可读性。)
答案 2 :(得分:1)
你看看http://www.joshlong.com/jl/blogPost/java_configuration_with_spring_batch.html
如果您定义了所有必需的“bean”对象,那么在main方法中,您可以在应用程序上下文中获取它们。
ApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
job = (Job) ctx.getBean("SOME JOB");
jobLauncher = (JobLauncher) ctx.getBean("jobLauncher");
jobLauncher.run(job, jobParams);
答案 3 :(得分:0)
我在这个例子中使用了以下技术: Spring批处理3.0.5.RELEASE,
JDK 1.7,
Eclipse Mars Release(4.5.0),
Maven 3.3.3,
Springframework 4.0.5.ReLEASE。
第1步:创建简单的POJO,Employee.java
public class Employee {
private String name;
private String empId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
@Override
public String toString() {
return "Employee [name=" + name + ", empId=" + empId + "]";
}
}
第2步:创建java类,ClassReader.java
package com.batch.main;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.batch.beans.Employee;
@Component("classReader")
public class ClassReader implements ItemReader<Employee> {
@Override
public Employee read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
Employee emp=new Employee();
//Set values in Employee object
emp.setEmpId("123456");
emp.setName("Manohar");
System.out.println("Inside ClassReader..." + emp);
return emp;
}
}
第3步:创建java类,ClassProcessor.java
package com.batch.main;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.batch.beans.Employee;
@Component("classProcesser")
public class ClassProcessor implements ItemProcessor<Employee, Employee>{
@Override
public Employee process(Employee emp) throws Exception {
System.out.println("Inside ClassProcessor..." + emp);
return emp;
}
}
第4步:创建java类,ClassWriter.java
package com.batch.main;
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.batch.beans.Employee;
@Component("classWriter")
public class ClassWriter implements ItemWriter<Employee> {
@Override
public void write(List<? extends Employee> arg0) throws Exception {
System.out.println("Inside ClassWriter..." + arg0);
}
}
第5步:创建context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.
ResourcelessTransactionMana ger" />
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.
MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
</beans>
第6步:创建job.xml
<?xml version="1.0" encoding="UTF-8"?>
<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:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:crypt="http://springcryptoutils.com/schema/crypt"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://springcryptoutils.com/schema/crypt http://springcryptoutils.com/schema/crypt.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<import resource="/context.xml" />
<context:component-scan base-package="com.batch" />
<batch:job id="loadJob" xmlns="http://www.springframework.org/schema/batch">
<batch:step id="step1" >
<batch:tasklet>
<batch:chunk reader="classReader" writer="classWriter"
processor="classProcesser" commit-interval="1" />
</batch:tasklet>
</batch:step>
</batch:job>
<!-- <bean id="classReader" class="com.batch.main.ClassReader" >
</bean>
<bean id="classWriter" class="com.batch.main.ClassWriter" ></bean>
<bean id="classProcesser" class="com.batch.main.ClassProcessor" > </bean>-->
第7步:最后创建Main.java
package com.batch.main;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource({"classpath:/com/spring/job/job.xml"})//load configuration file from classpath
public class Main {
public static void main(String[] args) throws JobExecutionAlreadyRunningException,
JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(Main.class);
context.refresh();
//load jobLauncher details from context.xml file
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
//load Job details from job.xml file
Job job = (Job) context.getBean("loadJob");
//run job
JobExecution execution = jobLauncher.run(job, new JobParameters());
System.out.println("Exit Status : " + execution.getStatus());
}
}
现在以Java应用程序的身份运行以上程序,您将在控制台上看到输出。
请参阅http://manohark.com/simple-spring-batch-example-using-annotations/了解完整详情。
答案 4 :(得分:0)
请与tutorial回购一起查看我的github,也许这对您有用。
Spring Batch与其他任何工具都有其自身的局限性,但仍然非常灵活。我在一个真正的项目中使用它,运行了1500个不同的工作,我认为它非常好,涵盖了批处理应用程序中的大量用例。
答案 5 :(得分:0)
当我阅读有关Spring批注释的文章时,我遇到了同样的问题。您可以阅读有关Spring注释的最佳位置是the reference文档。无论是否使用XML或Java,批处理的概念都是相同的。
我建议按照以下步骤操作: