스프링 배치 환경 설정하기

2018. 9. 19. 22:55Spring Batch

반응형
context-batch-job-launchar.xml

Job실행을 위해 EgovBatchRunner를 등록하여 준다.

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/batch
						http://www.springframework.org/schema/batch/spring-batch.xsd">
				
	<bean id="eGovBatchRunner" class="egovframework.rte.bat.core.launch.support.EgovBatchRunner">
		<constructor-arg ref="jobOperator" />
		<constructor-arg ref="jobExplorer" />
		<constructor-arg ref="jobRepository" />
	</bean>
	
	<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
		<property name="jobRepository" ref="jobRepository" />
	</bean>
	
	<bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
		<property name="jobRegistry" ref="jobRegistry" />
	</bean>
	
	<bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
		p:dataSource-ref="dataSource"
		p:transactionManager-ref="transactionManager"
		p:isolationLevelForCreate="ISOLATION_DEFAULT" />
		
	<bean id="jobOperator"
		class="org.springframework.batch.core.launch.support.SimpleJobOperator"
		p:jobLauncher-ref="jobLauncher"
		p:jobExplorer-ref="jobExplorer"
		p:jobRepository-ref="jobRepository"
		p:jobRegistry-ref="jobRegistry" />
		
	<bean id="jobExplorer"
		class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
		p:dataSource-ref="dataSource" />
	
	<bean id="jobRegistry" 
		class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
		
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
						
</beans>

EgovBatchRunner 소스 일부를 보면 생성자 주입을 위해 jobOperator, jobExplorer, jobRegistry 정보가 필요하다.

public class EgovBatchRunner {
	/**
	 * EgovBatchRunner 생성자
	 * JobOperator, JobExplorer, JobRepository를 설정한다.
	 * 
	 * @param jobOperator
	 * @param jobExplorer
	 * @param jobRepository
	 */
	public EgovBatchRunner(JobOperator jobOperator, JobExplorer jobExplorer,
			JobRepository jobRepository) {
		
		Assert.notNull(jobOperator, "The JobOperator is mandatory");
		Assert.notNull(jobExplorer, "The JobExplorer is mandatory");
		Assert.notNull(jobRepository, "The JobRepository is mandatory");
		
		this.jobOperator = jobOperator;
		this.jobExplorer = jobExplorer;
		this.jobRepository = jobRepository;
	}
}

※ 소스주소

https://github.com/eGovFrame/egovframework.rte.root/blob/master/Batch/egovframework.rte.bat.core/src/main/java/egovframework/rte/bat/core/launch/support/EgovBatchRunner.java

 

context-batch-datasource.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-3.2.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.xsd">

	<context:annotation-config />
	
	<bean id="egov.propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:/config/batch/properties/datasource.properties</value>
			</list>
		</property>
	</bean>
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${DriverClassName}" />
		<property name="url" value="${Url}" />
		<property name="username" value="${UserName}" />
		<property name="password" value="${Password}" />
		<property name="initialSize" value="2" />
		<property name="maxActive" value="100" />
	</bean>
	
</beans>

 

데이터베이스 속성 정보는 별도의 database.properties 관리

#------------------------------------------------------------------------------
# mySql Database
#------------------------------------------------------------------------------
DriverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
Url=jdbc:log4jdbc:mysql://localhost:3306/mysql?useUnicode=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Seoul&useSSL=false
UserName=root
Password=admin1234

 

context-batch-transaction.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/jdbct
						http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

	<context:annotation-config />
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
		<property name="dataSource" ref="dataSource" />
	</bean>
</beans>

 

context-base.xml

 

지금까지 작성한 파일 import 및 job 관련 정보 설정

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/batch
						http://www.springframework.org/schema/batch/spring-batch.xsd">
	
	<import resource="classpath:/config/batch/context-batch-job-launcher.xml" />					
	<import resource="classpath:/config/batch/context-batch-datasource.xml" />
	<import resource="classpath:/config/batch/context-batch-transaction.xml" />
	<import resource="classpath:/config/batch/job/*.xml" />
				
</beans>

 

데이터베이스 생성하기

배치실행시 배치실행관련 정보를 저장할 수 있다. 이와 관련하여서 테이블을 생성하여야 한다.

자동으로도 생성되게 할 수 도 있으나 수동으로 생성해본다. 테이블 생성 SQL 문은 spring-batch-core-4.0.1.RELEASE.jar 가 있을 것이다. 그 안에 schema-drop-mysql.sql, schema-mysql.sql 을 이용하여서 테이블 생성과 삭제를 할 수 있다.

 

log4j 설정하기
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<Configuration>
	<properties>
		<property name="pattern">%d %5p [%c] %m%n</property>
	</properties>
	<Appenders>
		<Console name="console" target="SYSTEM_OUT">
			<PatternLayout pattern="${pattern}" />
		</Console>
	</Appenders>
	<Loggers>
		<Logger name="com.framework" level="INFO" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		
		<Logger name="com.batchguide" level="INFO" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		
		<!-- log SQL with timing information, post execution -->
		<Logger name="java.sql" level="OFF" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<Logger name="jdbc.sqltiming" level="OFF" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<Logger name="jdbc.sqlonly" level="OFF" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<Logger name="jdbc.audit" level="OFF" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<Logger name="jdbc.resultset" level="OFF" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<Logger name="jdbc.resultsettable" level="OFF" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<Logger name="jdbc.connection" level="OFF" additivity="false">
			<AppenderRef ref="console" />
		</Logger>			
		
		<!-- Root level -->
		<Root level="ERROR">
			<AppenderRef ref="console" />
		</Root>
	</Loggers>
</Configuration>

 

반응형