Steps to learn Spring Batch Service

Spring Batch Service

Spring Batch Service – How does it work?

Visió de conjunt :

The primavera Els serveis per lots permeten automatitzar el processament de diversos treballs per lots,,en,Spring ofereix una oportunitat per optimitzar els seus recursos i estalviar costos innecessaris, ja que els serveis de lots primaris són capaços de processar diversos treballs ràpidament,,en,Penseu en això: una empresa no necessita emprar mà d'obra per executar treballs repetitius,,en,els treballs es faran mitjançant un marc de codi obert que proporcioni API i serveis preparats i, a més, permeti la modificació dels serveis segons el requisit,,en,Què és el lot de primavera?,,en,Les empreses han de processar diverses operacions comercials, com ara els càlculs de final de mes,,en,aplicació periòdica,,en,regles de negoci complexes,,en,i la integració d’informació complexa rebuda de fonts externes processades repetidament a través de grans conjunts de dades a granel amb intervenció humana mínima o nul·la,,en. It is a framework that provides the APIs and services that can be reused to process one or more jobs in batches. From an enterprise viewpoint, Spring offers an opportunity to optimize its resources and save unnecessary costs because the Spring batch services is able to process several jobs quickly. Think of this – an enterprise does not need to employ manpower to execute repetitive jobs. Instead, the jobs will be done by an open source framework that provides ready made APIs and services and also allows the modification of the services as per requirement.








What is Spring batch?

Enterprises need to process a number of business operations such as month-end calculations, periodically applying complex business rules and integrating complex information received from external sources processed repetitively across very large data sets in bulk with minimal or no human intervention. El Spring Batch fa exactament això,,en,El lot de primavera,,en,que és part de la,,en,Cartera de primavera,,en,és un marc de codi obert que permet el desenvolupament d'aplicacions de processament per lots,,en,Spring Batch ofereix capacitats per processar operacions de negocis senzilles i complexes,,en,Tot i que ofereix funcions reutilitzables que poden processar grans volums de registres incloent la gestió de transaccions,,en,estadístiques de processament de treballs,,en,traça,,en,ometre,,en,reinici de treball i gestió de recursos,,en,També ofereix serveis tècnics i funcions que permeten treballs discogràfics de gran rendiment i altes prestacions mitjançant tècniques d'optimització i partició,,en,Cada component de l'arquitectura de lots de primavera té un paper específic a jugar,,en,El component d'aplicació per lots és l’aplicació que permet als desenvolupadors de programari processar per lots un o més treballs,,en. The Spring batch, which is a part of the Spring portfolio, is an open source framework that allows the development of batch-processing applications. Spring Batch offers capabilities for processing both simple and complex business operations. While it offers reusable functions that can process large volumes of records including transaction management, job processing statistics, logging/tracing, skip, job restart and resource management, it also offers technical services and features that enable high-volume and high performance batch jobs through optimization and partitioning techniques.

Each component in the Spring batch architecture has a specific role to play. The Batch Application component is the application that allows the software developers to batch process one or more jobs. El Spring Batch Core proporciona l'entorn d'execució per a l'aplicació per lots,,en,La infraestructura Spring Batch proporciona les classes útils per a la construcció i l'execució d'aplicacions per lots,,en,Des del punt de vista empresarial,,en,és desitjable que les operacions comercials o els treballs es completin amb la precisió esperada i amb el temps,,en,Les empreses volen completar tantes operacions com sigui possible amb la mínima inversió de recursos que poden ser temps i personal,,en,El servei de lots en primavera permet a l'empresa assolir aquest objectiu,,en,Seguiu les ordres que es mostren a continuació,,en,cd ExempleSpringBatch /,,en,eclipsi mvn,,en,eclipsi,,en,Declarar totes les dependències del projecte a pom.xml com es mostra a continuació,,en,Definició de pom.xml amb dependències,,en,maven.apache.org/maven-v4_0_0.xsd,,en,ExempleSpringBatch,,en,envasos,,en,SNAPSHOT,,en,maven.apache.org,,en,jdk.version,,en,spring.version,,en,spring.batch.version,,en,mysql.driver.version,,en. Lastly, the Spring Batch Infrastructure provides the classes useful for both building and running batch apps.

Must watch – Spring video tutorials

Advantages

From the enterprise viewpoint, it is desirable that business operations or jobs are completed with the expected accuracy and within time. Enterprises want to complete as many operations as quickly possible with the least investment of resources which can be time and personnel. The Spring batch service allows the enterprise to achieve this objective. The main advantages of the Spring batch services are given below.

  • Availability of reusable functions that can perform tasks such as processing large volumes of records that includes logging/tracing, transacció, and job processing statistics, job restart, skip, and resource management.
  • Availability of technical services that can perform complex tasks that involve high-volume and high-performance batch jobs.
  • At the moment, Spring batch services are probably the best available solution, as far as processing of batch jobs are concerned.









Creating Spring Batch Services

First create a Java project using Maven

$ mvn archetype:generate -DgroupId=com.techalpine -DartifactId=ExampleSpringBatch

-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Now, go to command prompt and convert it to eclipse project and import in Eclipse IDE. Follow the commands shown below.

cd ExampleSpringBatch/

$ mvn eclipse:eclipse

Declare all project dependencies in pom.xml as shown below.

Listing 1: Defining pom.xml with dependencies

<project xmlns=”http://maven.apache.org/POM/4.0.0″

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0

http://maven.apache.org/maven-v4_0_0.xsd”>

<modelVersion>4.0.0</modelVersion>

<groupId>com.techalpine</groupId>

<artifactId>ExampleSpringBatch</artifactId>

<packaging>jar</packaging>

<version>1.0-SNAPSHOT</version>

<nom>ExampleSpringBatch</nom>

<url>http://maven.apache.org</url>

<properties>

<jdk.version>1.6</jdk.version>

<spring.version>3.2.2.ALLIBERAMENT</spring.version>

<spring.batch.version>2.2.0.ALLIBERAMENT</spring.batch.version>

<mysql.driver.version>5.1.25</mysql.driver.version>

</properties>

<dependencies>

<!– Spring Core,,en,molla-nucli,,en,Spring jdbc,,en,per a la base de dades,,en,spring-jdbc,,en,Les dependències de Spring Batch,,en,org.springframework.batch,,en,molla-batch-core,,en,Infraestructura de lots de primavera,,en,Controlador de base de dades MySQL,,en,finalName,,bg,exemple-primavera-lot,,en,org.apache.maven.plugins,,en,maven-eclipse-plugin,,en,Fonts de descàrrega,,en,downloadJavadocs,,en,maven-compilador-plugin,,en,font,,en,definició de treballs per lots,,en,Aquesta secció defineix la seqüència de treballs per lots,,en,Els passos es mencionen com a "pas1,,en,'Step2,,en,A pocs passos,,en,tasklets defineixen una petita unitat de treball a realitzar,,en,Cada fragment de lot defineix l'obra,,en,Podem tenir diversos passos en un treball per lots,,en,Descriure els treballs per lots i els components,,en,lot =,,en,www.springframework.org/schema/batch,,en,tasca =,,en,www.springframework.org/schema/task,,en,www.springframework.org/schema/batch/spring-batch-2.2.xsd,,en,samplereport,,en,com.techalpine.spring.batch,,en,scope =,,en,prototip,,en,lot,,en,id treball =,,en,batchJobSample,,en,pas id =,,en,Pas 1,,en,tasklet,,en,lector de peces =,,en,ReaderOne,,en,escriptor =,,en,WriterOne,,en,processador =,,en –>

<dependència>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependència>

<!– Spring jdbc, for database –>

<dependència>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependència>

<!– Spring Batch dependencies –>

<dependència>

<groupId>org.springframework.batch</groupId>

<artifactId>spring-batch-core</artifactId>

<version>${spring.batch.version}</version>

</dependència>

<dependència>

<groupId>org.springframework.batch</groupId>

<artifactId>spring-batch-infrastructure</artifactId>

<version>${spring.batch.version}</version>

</dependència>

<!– MySQL database driver –>

<dependència>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>${mysql.driver.version}</version>

</dependència>

</dependencies>

<build>

<finalName>example-spring-batch</finalName>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-eclipse-plugin</artifactId>

<version>2.9</version>

<configuració>

<downloadSources>veritable</downloadSources>

<downloadJavadocs>false</downloadJavadocs>

</configuració>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.3.2</version>

<configuració>

<source>${jdk.version}</source>

<target>${jdk.version}</target>

</configuració>

</plugin>

</plugins>

</build>

</project>



Now, defining batch jobs. This section defines sequence of batch jobs. Steps are mentioned as ‘step1′,’step2’ etc. Within steps, tasklets define small unit of work to be performed. Each batch chunk defines the work. We can have multiple steps in a batch job.

Listing 2: Describing batch jobs and components

<beans xmlns=”http://www.springframework.org/schema/beans”

xmlns:batch=”http://www.springframework.org/schema/batch”

xmlns:task=”http://www.springframework.org/schema/task”

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”>

<bean id=”samplereport” class=”com.techalpine.spring.batch” scope=”prototype” />

<batch:job id = “batchJobSample”>

<batch:step id = “step1”>

<batch:tasklet>

<batch:chunk reader = “ReaderOne” writer = “WriterOne”

processor = “ProcessorOne,,en,commit-interval =,,en,tros,,en,step2,,en,ReaderTwo,,en,WriterTwo,,en,ProcessorTwo,,en,feina,,en,org.springframework.batch.item.file.FlatFileItemReader,,en,Lectura del fitxer,,en,cvs / sample.1csv,,en,lineMapper,,en,bean class =,,en,org.springframework.batch.item.file.mapping.DefaultLineMapper,,en,dividir-la,,en,lineTokenizer,,en,org.springframework.batch.item.file.transform.DelimitedLineTokenizer,,en,impressions,,en,clics,,en,guanyar,,en,fieldSetMapper,,en,org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper,,en,prototypeBeanName,,en,org.springframework.batch.item.xml.StaxEventItemWriter,,en,xml / outputs / sample1.xml,,en,marshaller,,en,ref =,,en,yyy,,en,rootTagName,,ml,cvs / sample2.csv,,en,xml / outputs / sample2.xml,,en,zzz,,en,mmm,,en,escriurem el programa principal per executar el treball per lots,,en,Programa principal per executar el treball per lots,,en,import org.springframework.batch.core.Job,,en,import org.springframework.batch.core.JobExecution,,en,import org.springframework.batch.core.JobParameters,,en,import org.springframework.batch.core.launch.JobLauncher,,en,classe pública BatchAppRun,,en,springAppConfig =,,en” commit-interval = “8”>

</batch:chunk>

</batch:tasklet>

</batch:pas>

<batch:step id = “step2”>

<batch:tasklet>

<batch:chunk reader = “ReaderTwo” writer = “WriterTwo”

processor = “ProcessorTwo” commit-interval = “8”>

</batch:chunk>

</batch:tasklet>

</batch:pas>

</batch:job>

<bean id=”ReaderOne” class=”org.springframework.batch.item.file.FlatFileItemReader”>

<!– Reading file–>

<property name=”resource” value=”classpath:cvs/sample.1csv” />

<property name=”lineMapper”>

<bean class=”org.springframework.batch.item.file.mapping.DefaultLineMapper”>

<!– splitting it –>

<property name=”lineTokenizer”>

<bean

class=”org.springframework.batch.item.file.transform.DelimitedLineTokenizer”>

<property name=”noms” value=”date,impressions,clicks,earning” />

</bean>

</propietat>

<property name=”fieldSetMapper”>

<bean

class=”org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper”>

<property name=”prototypeBeanName” value=”samplereport” />

</bean>

</propietat>

</bean>

</propietat>

</bean>

<bean id = “WriterOne”

class = “org.springframework.batch.item.xml.StaxEventItemWriter”>

<property name = “resource” valor = “expedient:xml/outputs/sample1.xml” />

<property name = “marshaller” ref = “yyy” />

<property name = “rootTagName” valor = “xxx” />

</bean>

<bean id=”ReaderTwo” class=”org.springframework.batch.item.file.FlatFileItemReader”>

<!– Reading file–>

<property name=”resource” value=”classpath:cvs/sample2.csv” />

<property name=”lineMapper”>

<bean class=”org.springframework.batch.item.file.mapping.DefaultLineMapper”>

<!– splitting it –>

<property name=”lineTokenizer”>

<bean

class=”org.springframework.batch.item.file.transform.DelimitedLineTokenizer”>

<property name=”noms” value=”date,impressions,clicks,earning” />

</bean>

</propietat>

<property name=”fieldSetMapper”>

<bean

class=”org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper”>

<property name=”prototypeBeanName” value=”samplereport” />

</bean>

</propietat>

</bean>

</propietat>

</bean>

<bean id = “WriterTwo”

class = “org.springframework.batch.item.xml.StaxEventItemWriter”>

<property name = “resource” valor = “expedient:xml/outputs/sample2.xml” />

<property name = “marshaller” ref = “zzz” />

<property name = “rootTagName” valor = “mmm” />

</bean>

</beans>

Now, we will be writing the main program to execute the batch job.

Listing 3: Main program to run the batch job.

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;

importació org.springframework.context.ApplicationContext;

importació org.springframework.context.support.ClassPathXmlApplicationContext;

public class BatchAppRun {

public static void principal(Cadena[] args) throws Exception {

Cadena[] springAppConfig = { “jobs / helloTechAlpine.xml,,en,Crea un context d'aplicació,,en,springAppConfig,,en,Creeu el llançador de treballs,,en,JobLauncher mostrajobLauncher =,,en,Llançador de treballs,,en,JobLauncher,,en,Creta a la feina,,en,Treball de mostra de feina =,,en,Treball,,en,Executeu JOB,,en,Execució de JobExecution = mostraunglauncher.run,,en,feina de mostra,,en,nous paràmetres de treball,,en,L’execució d’aquest fitxer iniciarà el treball per lots i realitzarà el processament de fitxers,,en” };

// Create application context

ApplicationContext context = new ClassPathXmlApplicationContext(springAppConfig);

// Create the job launcher

JobLauncher samplejobLauncher = (JobLauncher) context.getBean(“jobLauncher”);

// Crete the job

Job samplejob = (Job) context.getBean(“batchJobSample”);

// Execute the JOB

JobExecution execution = samplejobLauncher.run(samplejob, new JobParameters());

}

}

Executing this file will launch the batch job and do the file processing.

Must read – Interesting articles on Spring framework








Conclusió

The Spring Batch services solves huge business problems for enterprises because it helps them save resources and get things done quickly. Very importantly, it is open source and available for customization. Batch jobs are important when you want to write some tasks repeatedly and sequentially. You just need to define them once in the configuration file and then run it. That’s all about Spring batch services.


============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share