스프링 배치(Spring Batch)
활용예제
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>SpringBatch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.framework.version>3.1.2.RELEASE</spring.framework.version>
<spring.batch.version>2.2.0.RELEASE</spring.batch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
</dependencies>
</project>
---------------------------------------------------------------------------------------------
PrintJob1.class
// ref http://www.jroller.com/0xcafebabe/entry/spring_batch_hello_world_1
public class PrintJob1 implements Tasklet {
private String message;
// 메시지 설정을 위한 setter
public void setMessage(String message) {
this.message = message;
}
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
throws Exception {
System.out.println("############### JOB 1 ###############");
// 타입스탬프 출력
DateFormat dateFormat = new SimpleDateFormat
("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
// 메시지 출력
System.out.println(message);
System.out.println("############### END 1 ###############");
return RepeatStatus.FINISHED;
}
}
---------------------------------------------------------------------------------------------
PrintJob2.class
- PrintJob1.class 와 동일함
---------------------------------------------------------------------------------------------
PrintJob3.class
- PrintJob1.class 와 동일함
---------------------------------------------------------------------------------------------
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Job Launcher 설정 -->
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<!-- Job Repository 설정 -->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<!-- Transasction Manager 설정 -->
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
</bean>
<!-- 배치 작업 설정 -->
<bean id="helloJob1" class="com.teachd.myapp.PrintJob1">
<property name="message" value="!!!!! Hello !!!!!" />
</bean>
<bean id="helloJob2" class="com.teachd.myapp.PrintJob2">
<property name="message" value="!!!!! Spring !!!!!" />
</bean>
<bean id="helloJob3" class="com.teachd.myapp.PrintJob3">
<property name="message" value="!!!!! Batch !!!!!" />
</bean>
<!-- 스케줄링 설정 -->
<batch:job id="myJob">
<batch:step id="step1" next="step2">
<batch:tasklet ref="helloJob1" />
</batch:step>
<batch:step id="step2" next="step3">
<batch:tasklet ref="helloJob2" />
</batch:step>
<batch:step id="step3">
<batch:tasklet ref="helloJob3" />
</batch:step>
</batch:job>
</beans>
---------------------------------------------------------------------------------------------
터미널에서 실행하기
mvn package exec:
java "-Dexec.mainClass=
org.springframework.batch.core.launch.support.CommandLineJobRunner"
"-Dexec.args=applicationContext.xml myJob"
소스파일
https://github.com/Yongdae-Kim/spring_study/tree/master/SpringBatch
참고
조대협의 블로그 :: Spring Batch 개념 정리
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>SpringBatch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.framework.version>3.1.2.RELEASE</spring.framework.version>
<spring.batch.version>2.2.0.RELEASE</spring.batch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
</dependencies>
</project>
---------------------------------------------------------------------------------------------
PrintJob1.class
// ref http://www.jroller.com/0xcafebabe/entry/spring_batch_hello_world_1
public class PrintJob1 implements Tasklet {
private String message;
// 메시지 설정을 위한 setter
public void setMessage(String message) {
this.message = message;
}
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
throws Exception {
System.out.println("############### JOB 1 ###############");
// 타입스탬프 출력
DateFormat dateFormat = new SimpleDateFormat
("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
// 메시지 출력
System.out.println(message);
System.out.println("############### END 1 ###############");
return RepeatStatus.FINISHED;
}
}
---------------------------------------------------------------------------------------------
PrintJob2.class
- PrintJob1.class 와 동일함
PrintJob3.class
- PrintJob1.class 와 동일함
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Job Launcher 설정 -->
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<!-- Job Repository 설정 -->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<!-- Transasction Manager 설정 -->
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
</bean>
<!-- 배치 작업 설정 -->
<bean id="helloJob1" class="com.teachd.myapp.PrintJob1">
<property name="message" value="!!!!! Hello !!!!!" />
</bean>
<bean id="helloJob2" class="com.teachd.myapp.PrintJob2">
<property name="message" value="!!!!! Spring !!!!!" />
</bean>
<bean id="helloJob3" class="com.teachd.myapp.PrintJob3">
<property name="message" value="!!!!! Batch !!!!!" />
</bean>
<!-- 스케줄링 설정 -->
<batch:job id="myJob">
<batch:step id="step1" next="step2">
<batch:tasklet ref="helloJob1" />
</batch:step>
<batch:step id="step2" next="step3">
<batch:tasklet ref="helloJob2" />
</batch:step>
<batch:step id="step3">
<batch:tasklet ref="helloJob3" />
</batch:step>
</batch:job>
</beans>
---------------------------------------------------------------------------------------------
mvn package exec:
java "-Dexec.mainClass=
org.springframework.batch.core.launch.support.CommandLineJobRunner"
"-Dexec.args=applicationContext.xml myJob"
https://github.com/Yongdae-Kim/spring_study/tree/master/SpringBatch
참고
조대협의 블로그 :: Spring Batch 개념 정리
댓글 없음:
댓글 쓰기