블로그 이미지
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

'batch'에 해당되는 글 2

  1. 2009.11.18 Spring Batch 2.1.0 예시
  2. 2008.08.26 [SpringJDBC] Batch
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.
2008.08.26 22:03 이전글(~2009)

SimpleJdbcTemplate를 이용해서 일괄처리를 할수 있는 메서드를 제공하는데 그것이 batchUpdate()메서드이다. 여기서 일괄처리는 여러개의 row를 insert,update,delete하는것을 말한다. 간단히 다음과 같은 예제를 통해서 이해할수 있다.

public class JdbcActorDao implements ActorDao {
  private SimpleJdbcTemplate simpleJdbcTemplate;
  public void setDataSource(DataSource dataSource) {
    this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
  }
  public int[] batchUpdate(final List<Actor> actors) {
    SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(actors.toArray());
    int[] updateCounts = simpleJdbcTemplate.batchUpdate(
    "update t_actor set first_name = :firstName, last_name = :lastName where id = :id",batch);
    return updateCounts;
}
// ... additional methods
}

SqlParameterSourceUtils.createBatch에 각기 다른 도메인 객체 List를 batch 변수에 저장하고 batchUpdate로 한꺼번에 업데이트하는 예제이다. 물론 아래와 같이 "?"를 이용해서 구현할수도 있다.

public class JdbcActorDao implements ActorDao {
private SimpleJdbcTemplate simpleJdbcTemplate;
public void setDataSource(DataSource dataSource) {
  this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
public int[] batchUpdate(final List<Actor> actors) {
  List<Object[]> batch = new ArrayList<Object[]>();
  for (Actor actor : actors) {
  Object[] values = new Object[] {
    actor.getFirstName(),
    actor.getLastName(),
    actor.getId()};
    batch.add(values);
  }
  int[] updateCounts = simpleJdbcTemplate.batchUpdate(
  "update t_actor set first_name = ?, last_name = ? where id = ?",batch);
  return updateCounts;
}

이것은 보통

while (iter.hasNext()) {
    jdbcTemplate.update(....);
}

형식일때 자원을 낭비하지 말고, 효과적으로 사용하라고 만들어 놓은것이다.

신고

'이전글(~2009)' 카테고리의 다른 글

[SpringJDBC] SimpleJdbcInsert  (0) 2008.08.26
[SpringJDBC] SimpleJdbcTemplate  (0) 2008.08.26
[SpringJDBC] Batch  (0) 2008.08.26
[SpringJDBC] NamedParameterJdbcTemplate  (0) 2008.08.26
[SpringJDBC] JdbcTemplate  (0) 2008.08.26
[SpringJDBC] 개요  (0) 2008.08.26
posted by Max.
prev 1 next

티스토리 툴바