2012-01-04 10 views
6

के साथ स्प्रिंग बैच मैं स्प्रिंग बैच के लिए नया हूं, उदाहरण के लिए एनोटेशन अवधारणा के साथ स्प्रिंग बैच विकसित किया गया है।एनोटेशन

यह लिंक (click) वसंत बैच बारे में बात करती है, लेकिन एनोटेशन अवधारणा साथ बैच वसंत नहीं। जैसा कि दिए गए लिंक दस्तावेज में चर्चा की गई है, स्पष्ट नहीं है। मैं नवीनतम वसंत ढांचे का उपयोग कर रहा हूँ। मैं एक्सएमएल विन्यास से बचना चाहता हूँ।

क्या बैच प्रोसेसिंग के लिए स्प्रिंग बैच बहुत अच्छा टूल है? या स्प्रिंग बैच के बजाय बैच प्रसंस्करण के लिए कोई बेहतर उपकरण उपलब्ध है?

क्या स्प्रिंग बैच में कोई सीमाएं हैं?

+0

बैच प्रोसेसिंग के लिए स्प्रिंग बैच बहुत अच्छा है: आप इसके साथ कुछ भी कार्यान्वित कर सकते हैं। "सीमाएं" का व्यापक अर्थ है (प्रदर्शन सीमाएं? वैचारिक? एपीआई? एकीकरण?)। यदि आप अपने काम पर अधिक सटीक हैं, तो आपको बेहतर सलाह मिलती है। –

उत्तर

3

स्प्रिंग बैच केवल सीमित कार्यक्षमता का समर्थन करता है जिसे आप एनोटेशन का उपयोग करके कॉन्फ़िगर कर सकते हैं। मुख्य रूप से ये श्रोता कॉलबैक हैं, जैसे @BeforeStep या @AfterChunk। इन एनोटेशन का उपयोग करके आप संबंधित इंटरफेस को लागू करने या एनोटेटेड विधियों का उपयोग करने के लिए एक विकल्प प्रदान करते हैं। एनोटेटेड बीन को एक्सएमएल कॉन्फ़िगरेशन में नौकरी, चरण या खंड (reader, processor, writer, retry-policy, skip-policy या listeners) में इंजेक्शन दिया जाना चाहिए, जिसे आप टालना नहीं कर सकते हैं।

1

आप मुख्य विधि में http://www.joshlong.com/jl/blogPost/java_configuration_with_spring_batch.html

को देखो आप सभी आवश्यक "सेम" वस्तु परिभाषित करते हैं, तो है, तो आप सिर्फ उन्हें आवेदन के संदर्भ में प्राप्त कर सकते हैं।

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,

ग्रहण मंगल ग्रह रिलीज़ (4.5.0),

Maven 3.3 .3,

स्प्रिंगफ्रेमवर्क 4.0.5. रिलीज।

चरण 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: जावा वर्ग बनाएँ, 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: जावा वर्ग बनाएं , क्लासप्रोसेसर.जावा

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: जावा वर्ग बनाएँ, 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 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()); 
     } 
} 

अब जावा आवेदन के रूप में कार्यक्रम ऊपर भागो बनाते हैं, आप कंसोल पर उत्पादन देखेंगे।

कृपया पूर्ण विवरण के लिए http://manohark.com/simple-spring-batch-example-using-annotations/ देखें।

0

कृपया पर github रेपो के साथ एक साथ देखें, शायद यह आपके लिए उपयोगी होगा।

स्प्रिंग बैच किसी भी अन्य उपकरण के रूप में अपनी सीमा है लेकिन अभी भी काफी लचीला है। मैं इसे 1500 विभिन्न नौकरियों के साथ एक असली परियोजना में उपयोग कर रहा हूं और मुझे लगता है कि यह बहुत अच्छा है और बैच प्रसंस्करण अनुप्रयोगों में बहुत से उपयोग मामलों को शामिल करता है।

0

जब मैं स्प्रिंग बैच एनोटेशन के बारे में पढ़ रहा था तो मुझे एक ही समस्या का सामना करना पड़ा। स्प्रिंग एनोटेशन के बारे में आप सबसे अच्छी जगह पढ़ सकते हैं the reference दस्तावेज। बैच के लिए अवधारणाएं समान हैं, चाहे एक्सएमएल या जावा का उपयोग किया जाए या नहीं।

मैं इन चरणों का पालन सुझाव है:

  • बैच अवधारणाओं
  • सरल बैच अनुप्रयोगों (डेटाबेस से पढ़ने और सीएसवी के लिए बचत) बनाने की कोशिश करें के बारे में अध्ययन
  • एक बार बुनियादी अवधारणाओं के साथ आरामदायक, स्प्रिंग बैच प्रो जैसी किताबों से अध्ययन।
  • यदि आपको कोई समस्या आती है, तो कोड के लिए ऑनलाइन देखें और फिर उपरोक्त स्प्रिंग दस्तावेज़ों के साथ क्रॉस-रेफरेंस।