2013년 8월 6일 화요일

Custom Tasklet - with Java

import org.apache.hadoop.util.ToolRunner;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class MyTasklet implements Tasklet{
private String 변수1;
private String 변수2;
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
// TODO Auto-generated method stub
int exitCode=0;
String paths[] = 변수지정;
exitCode = ToolRunner.run(new 하둡의드라이버(), paths);
System.exit(exitCode);
return null;
}
public MyTasklet(String 변수1, String 변수2) {
        this.변수1 = 변수1;
        this.변수2 = 변수2;
    }
}

----------------------------------------------------------------------------------------------------------------
이렇게 custom tasklet을 만들고 해당 클래스를 bean으로 만들어서
배치작업에서 사용하면 된다.

빈 설정
<bean id="myCustomTasklet"
  class="com.custom.test.MyTasklet">
    <constructor-arg type="String" value="변수1" />
      <constructor-arg type="String" value="변수2" />
</bean>

배치 작업 설정
<batch:job id="job1">
<batch:step id="step1">
<batch:tasklet ref="myCustomTasklet" />
</batch:step>
</batch:job>

----------------------------------------------------------------------------------------------------------------------
테스트를 위해 자바로 배치 작업을 실행한다면
import java.util.Date;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class RunJob {
public static void main(String[] args) throws Throwable {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("배치작업있는.xml");
ctx.start();
JobLauncher jobLauncher = (JobLauncher) ctx.getBean("jobLauncher");
Job dbJob = (Job) ctx.getBean("job1");
JobExecution jobExecution = jobLauncher.run(dbJob, new JobParametersBuilder().addDate("date", new Date()).toJobParameters());
//JobParameter를 지정해야 작업이 실행된다. 여기서는 날짜를 변수로 넣었고 해당 변수는 사용하지 않았다
JobInstance jobInstance = jobExecution.getJobInstance();
System.out.println("job instance Id: " + jobInstance.getId());
//테스트를 위해 작업 번호를 출력
BatchStatus batchStatus = jobExecution.getStatus();
while(batchStatus.isRunning()) {
System.out.println("Strill running...");
Thread.sleep(10*1000);//10sec
//작업이 진행중임을 확인하기 위한 예제
}
}
}

참고: http://static.springsource.org/spring-hadoop/docs/current/reference/html/fs.html
http://static.springsource.org/spring-hadoop/docs/current/reference/html/hadoop.html#hadoop:tool-runner
http://static.springsource.org/spring-batch/reference/html/configureJob.html#runningJobsFromCommandLine
http://www.mkyong.com/spring-batch/run-spring-batch-job-with-commandlinejobrunner/

댓글 없음:

댓글 쓰기