2012-01-04 31 views
6

Tôi mới làm quen với lô mùa xuân, tìm kiếm một số ví dụ được phát triển Lô mùa xuân với chú thích khái niệm.Lô mùa xuân với chú thích

Liên kết này (click) nói về lô mùa xuân, nhưng không phải lô mùa xuân có khái niệm chú thích. Như đã thảo luận trong tài liệu liên kết đã cho không rõ ràng. Tôi đang sử dụng khung công tác Spring mới nhất. Tôi muốn tránh cấu hình xml.

Lô xuân có phải là công cụ rất tốt để xử lý hàng loạt không? hoặc Có công cụ nào tốt hơn để xử lý hàng loạt thay vì hàng loạt Spring không?

Có bất kỳ hạn chế nào trong lô Xuân không?

+0

Lô mùa xuân rất tốt để xử lý theo lô: bạn có thể triển khai bất kỳ thứ gì với nó. "hạn chế" có ý nghĩa rất rộng (hạn chế hiệu suất? khái niệm? API? tích hợp?). Nếu bạn chính xác hơn về nhiệm vụ của mình thì bạn sẽ được tư vấn tốt hơn. –

Trả lời

3

Lô mùa xuân chỉ hỗ trợ chức năng giới hạn mà bạn có thể định cấu hình bằng chú thích. Chủ yếu là những người gọi lại người nghe, như @BeforeStep hoặc @AfterChunk. Sử dụng các chú thích này cung cấp cho bạn một lựa chọn để triển khai các giao diện tương ứng hoặc sử dụng các phương thức được chú thích. Đậu chú thích phải được tiêm vào công việc, bước hoặc đoạn (reader, processor, writer, retry-policy, skip-policy hoặc listeners) trong cấu hình XML mà bạn không thể tránh.

1

Các bạn đã nhìn vào http://www.joshlong.com/jl/blogPost/java_configuration_with_spring_batch.html

Nếu bạn định nghĩa tất cả các "đậu" đối tượng cần thiết, trong phương thức main, bạn chỉ có thể nhận được chúng trong bối cảnh ứng dụng.

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

tôi đã sử dụng công nghệ sau đây cho ví dụ này: mùa xuân hàng loạt 3.0.5.RELEASE,

JDK 1.7,

Eclipse Mars phát hành (4.5.0),

Maven 3.3 .3,

Springframework 4.0.5.ReLEASE.

Bước 1: Tạo POJO đơn giản, 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 + "]"; 
} 
} 

Bước 2: Tạo lớp 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; 
} 

} 

Bước 3: Tạo lớp 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; 
} 

} 

Bước 4: Tạo lớp 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); 

} 

} 

Bước 5: Tạo 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> 

Bước 6: Tạo công việc.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>--> 

Bước 7: Cuối cùng tạo 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()); 
     } 
} 

Bây giờ chạy trên chương trình như ứng dụng Java, bạn sẽ thấy những kết quả trên console.

Vui lòng tham khảo http://manohark.com/simple-spring-batch-example-using-annotations/ để biết chi tiết đầy đủ.

0

Vui lòng xem tutorial cùng với thông tin đăng ký github, có thể nó sẽ hữu ích cho bạn.

Spring Batch vì bất kỳ công cụ nào khác có giới hạn riêng nhưng vẫn khá linh hoạt. Tôi đang sử dụng nó trong một dự án thực sự với 1500 công việc khác nhau đang chạy và tôi nghĩ nó khá tốt và bao gồm rất nhiều trường hợp sử dụng trong các ứng dụng xử lý hàng loạt.

0

Tôi phải đối mặt với cùng một vấn đề khi tôi đọc về Chú thích hàng loạt mùa xuân. Nơi tốt nhất mà bạn có thể đọc về chú thích mùa xuân là tài liệu the reference. Các khái niệm giống nhau cho lô, dù XML hay Java được sử dụng.

tôi sẽ đề nghị làm theo các bước sau:

  • Nghiên cứu về các khái niệm hàng loạt
  • Cố gắng làm cho các ứng dụng hàng loạt đơn giản (đọc từ một cơ sở dữ liệu và tiết kiệm để CSV)
  • Khi confortable với các khái niệm cơ bản, nghiên cứu từ những cuốn sách như Spring Batch Pro.
  • Nếu bạn gặp sự cố, hãy tìm kiếm mã trực tuyến và sau đó tham khảo chéo với tài liệu Spring đã nêu ở trên.
Các vấn đề liên quan