2012-01-04 139 views
6

我是Spring批處理新手,尋找一些開發的例子Spring批註與註釋的概念。Spring批註與註釋

這個鏈接(click)是關於Spring批處理,但是不是Spring批處理和註釋概念。正如在給定的鏈接文檔中所討論的不清楚。我正在使用最新的Spring框架。我想避免xml配置。

Spring批處理是非常好的批處理工具嗎?還是有更好的工具可用於批處理而不是Spring批處理?

Spring批處理有任何限制嗎?

+0

Spring批處理非常適合批處理:您可以使用它來執行任何操作。 「限制」具有非常廣泛的含義(性能限制?概念?API?集成?)。如果你的任務更精確,那麼你會得到更好的建議。 – 2012-01-04 11:43:23

回答

3

Spring批處理只支持您可以使用批註進行配置的有限功能。主要是聽衆回調,如@BeforeStep@AfterChunk。使用這些註釋爲您提供一個選擇,以實現相應的接口或使用註釋的方法。註釋的bean必須注入XML配置中的作業,步驟或塊(readerprocessor,writer,retry-policy,skip-policylisteners),這是您無法避免的。

1

你有沒有看http://www.joshlong.com/jl/blogPost/java_configuration_with_spring_batch.html

如果你定義的所有必要的「豆」的對象,在main方法,你可以讓他們在應用程序上下文。

ApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class); 
job = (Job) ctx.getBean("SOME JOB"); 
jobLauncher = (JobLauncher) ctx.getBean("jobLauncher"); 
jobLauncher.run(job, jobParams); 
0

我用這個例子以下技術: 春天批次3.0.5.RELEASE,

JDK 1.7,

Eclipse的火星版本(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:創建作業。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/

0

請看看我的tutorial連同github回購,也許它會對你有用。

Spring批處理與其他工具一樣有其侷限性,但仍然非常靈活。我在一個真正的項目中使用了1500個不同的作業,我認爲它非常好,並且在批處理應用程序中涵蓋了很多用例。

0

我在閱讀Spring批註時遇到了同樣的問題。您可以閱讀有關Spring註釋的最佳位置是the reference文檔。批處理的概念是相同的,不管是否使用XML或Java。

我建議按照以下步驟:關於批處理概念

  • 儘量讓簡單的批處理應用程序(從數據庫中讀取並保存到CSV)

    • 研究
    • 一旦與基本概念舒適,從Spring Batch Pro等書籍中學習。
    • 如果遇到問題,請在線查看代碼,然後與前面提到的Spring文檔進行交叉引用。