블로그 이미지
Max.

calendar

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

Notice

'2.0'에 해당되는 글 1

  1. 2009.11.18 Spring Batch 2.1.0 예시
2009.11.18 16:35 개발관련
tasklet를 이용한 단순한 Batch의 흐름을 이해할수 있다. Quartz 크론으로 Batch Rruncher를 실행하도록 되어 있다.

0. 환경

  JDK1.6 + Tomcat6.0 + Spring3.0 RC1 + Spring Roo1.0 RC4 + Spring Batch2.1.0.M2 + Quartz1.6.3

1. applicationContext-batch.xml 설정 
 <!--====================================================================================-->
 <!-- ##설명                   -->
 <!--       # job 파일을 분할하는 이유는 웹뿐만 아니라, 시스템상의 명령어 라인에서도 가능하게 하기 위함이다.-->
 <!--       # 본 파일은 특정 Job의 실행(Launcher)를 설정하는 파일이다.        -->
 <!--       # 본 설정은 Quartz 를 이용한 Cron Job을 지정한다.         -->
 <!--====================================================================================-->

<import resource="classpath:/META-INF/batch/batch-datasource.xml" />
<import resource="classpath:/META-INF/batch/batch-job-member.xml" />

<!-- ================================= job-member =============================  -->
 <bean name="memberJob" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass" value="kr.mz.mpros.batch.job.MemberMappingJob"/>
  <property name="jobDataAsMap">
   <map>
    <entry key="jobLauncher" value-ref="jobLauncher"/>
    <entry key="jobLocator" value-ref="jobRegistry"/>
    <entry key="jobName" value="myMemberJob"/>    
   </map>
  </property>  
 </bean>
  
 <bean id="memberTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="memberJob"/>
  <property name="cronExpression" value="0 3 16 * * ?"/>
 </bean> 

 <!-- =============================================================================  -->
 <!-- ================================= schedulerFactory ===========================  -->
 <!-- =============================================================================  -->
 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="memberTrigger"/>    
   </list>
  </property>
<!--  <property name="applicationContextSchedulerContextKey" value="applicationContext"/>  -->
 </bean>
 
 <!-- ================================= jobRegistry   =============================  -->
 <bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
  <property name="jobRegistry" ref="jobRegistry"/>
 </bean> 
 
<!-- <bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator">-->
<!--  <property name="jobLauncher" ref="jobLauncher"/>-->
<!--  <property name="jobExplorer" ref="jobExplorer"/>-->
<!--  <property name="jobRepository" ref="jobRepository"/>-->
<!--  <property name="jobRegistry" ref="jobRegistry"/>-->
<!-- </bean>-->
<!-- <bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">-->
<!--  <property name="dataSource" ref="dataSourceMysqlBatch"/>-->
<!-- </bean>-->
 
 <bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
 
 <!-- ================================= jobRepository =============================  -->
 <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
  <property name="dataSource" ref="dataSourceMysqlBatch"/>
  <property name="transactionManager" ref="transactionManagerBatch"/>
 </bean>

 <!-- ================================= jobLauncher ===============================  -->
 <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
  <property name="jobRepository" ref="jobRepository" />
  <property name="taskExecutor">
   <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
  </property>
 </bean> 

2. batch-datasource.xml 설정 

<!--====================================================================================-->
 <!-- ##설명                   -->
 <!--       # Batch에 사용될 dataSource 설정을 한다.            -->
 <!--       # Batch에 사용될 Transaction 설정을 한다.           -->
 <!--====================================================================================-->
 
 <bean id="dataSourceMysqlBatch" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/> 
    </bean>
   
 <bean id="transactionManagerBatch" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
  <property name="dataSource" ref="dataSourceMysqlBatch" />
 </bean>

3. batch-job-member.xml 설정 

 <!--====================================================================================-->
 <!-- ##설명                   -->
 <!--       # Batch에 사용될 Job 시나리오를 작성한다.           -->
 <!--       # Batch에 사용될 Step의 Bean(실제 작업)을 정의 한다.         -->
 <!--====================================================================================-->
 
 <job id="myMemberJob">
  <step id="step1">
   <tasklet ref="memberMappingService" />
  </step>
 </job>

 <beans:bean id="memberMappingService" class="kr.mz.mpros.batch.job.MemberMappingService">
  <beans:property name="memberDao" ref="memberDao"/>
 </beans:bean>



4. job class(MemberMappingJob) 설정

public class MemberMappingJob extends QuartzJobBean {

 static final String JOB_NAME = "jobName";
 private static Log log = LogFactory.getLog(MemberMappingJob.class);
 private JobLocator jobLocator;
 private JobLauncher jobLauncher;
 public void setJobLocator(JobLocator jobLocator) {
  this.jobLocator = jobLocator;
 }
 public void setJobLauncher(JobLauncher jobLauncher) {
  this.jobLauncher = jobLauncher;
 }

 @SuppressWarnings("unchecked")
 protected void executeInternal(JobExecutionContext context) {
  Map<String, Object> jobDataMap = context.getMergedJobDataMap();
  String jobName = (String) jobDataMap.get(JOB_NAME);
  log.info("Quartz trigger firing with Spring Batch jobName="+jobName);
  JobParameters jobParameters = getJobParametersFromJobMap(jobDataMap);
  try {
   jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
  } catch (JobExecutionAlreadyRunningException e) {
   log.error("Could not execute job.", e);
  } catch (JobRestartException e) {
   log.error("Could not execute job.", e);
  } catch (JobInstanceAlreadyCompleteException e) {
   log.error("Could not execute job.", e);
  } catch (NoSuchJobException e) {
   log.error("Could not execute job.", e);
  }   
 }

 private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) {
  JobParametersBuilder builder = new JobParametersBuilder();
  builder.addDate("date", new Date());
  return builder.toJobParameters();
 }
}

5. service class() 설정

public class MemberMappingService implements InitializingBean, Tasklet {
 
 private static Log log = LogFactory.getLog(MemberMappingService.class);
 private MemberDao memberDao;
 public void setMemberDao(MemberDao memberDao) {
  this.memberDao = memberDao;
 }

 @Override
 public void afterPropertiesSet() throws Exception {
  log.info("[JOB]" + this.getClass().getSimpleName() + "=============================");
 }

 @Override
 public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
  log.info("[JOB]" + this.getClass().getSimpleName() + "=============================");
  memberDao.transferByMembersToAccounts();
  return RepeatStatus.FINISHED;
 }
}


 

저작자 표시
신고

'개발관련' 카테고리의 다른 글

Spring Flex(Spring BlazeDB Integration) 예시(1)  (0) 2009.11.25
Spring Batch 관련 자료 링크  (0) 2009.11.20
Spring Batch 2.1.0 예시  (0) 2009.11.18
Spring Quartz 예시(고전방식)  (0) 2009.11.18
Spring Secutiry3.0 확장 예시  (0) 2009.11.18
lock in  (0) 2009.11.13
posted by Max.
prev 1 next