Spring Batch의 CommandLineJobRunner를 이용한 jar 를 실행하기 위한 스크립트를 공유한다.

http://static.springsource.org/spring-batch/reference/html/configureJob.html

 

Hudson 의 Execution Shell에 java command 쓰다가 하루 시간이 다 가므로.. 스크립트를 만들어서 쓰면 많이 편리하다. serivce project 이름, xml 파일 이름, define한 job 이름, 저장할 table의 prefix, 기타 덧붙일 스트링이 있으면 사용하기 편할 수 있다.

 

실제 사용 예)

/home/www/script/batch_start.pl -p batch_bot –j bot_collector

 

스크립트

#!/usr/bin/perl

use strict;
use Getopt::Std;

my %OPTS;
getopt('pxjtma', \%OPTS);

my $WWW_HOME = "$ENV{'WWW_HOME'}";

main();

sub main {

        if(scalar(%OPTS) < 2) {
        print STDERR "Usage : batch_start.pl  -p [project-name] -x [xmlfile] -j [jobname] -t [table_prefix]\n";
        print STDERR "ex) batch_start.pl -p batch_template -x batch_template_job.xml -j job1\n\n";
        print STDERR "Usage : batch_start.pl  -p [project-name] -j [jobname] -t [table_prefix] -a [append string]\n";
        print STDERR "ex) batch_start.pl -p batch_template -j job1 -a \"jobparam1=p1\"\n";

        exit 1;
    }

    my $project_name =  $OPTS{"p"};
    my $xml_name =      $OPTS{"x"};
    my $job_name =      $OPTS{"j"};
    my $prefix_name =   $OPTS{"t"};
    my $module_name =   $OPTS{"m"};
    my $appendstr =     $OPTS{"a"};

    # define 해야 할 곳~

    my $project_path =  "…"; 
    my $jar_name = `ls $project_path| grep $project_name`;
   
        if ($module_name) {
            $jar_name = $module_name.".jar";       
        }

        if($xml_name) {
                $xml_name = "$xml_name";
        } else {
                $xml_name = "$job_name";
        }

        chomp($jar_name);

        my @libList = `ls $project_path/lib`;
        my $classpath_prefix = "$project_path/lib";

        print "## project_name = $project_name"."\n";
        print "## xml_file = $xml_name"."\n";
        print "## job_name = $job_name"."\n";
        print "## jar_name = $jar_name"."\n";
        print "## prefix_name = $prefix_name"."\n";
        print "## appendstr = $appendstr"."\n";

        my $classpath = "./";

        foreach my $lib_jar (@libList) {
                chomp($lib_jar);
                $classpath .= ":$classpath_prefix/$lib_jar";
        }

        chomp($appendstr);

        print "java -jar $project_path/$jar_name $xml_name $job_name $prefix_name $appendstr -classpath$..\n";
        !system("java -jar $project_path/$jar_name $xml_name $job_name $prefix_name $appendstr -classpath$classpath") or die "failure";
}

__END__

 

<java 쪽 코드>

pom.xml에 추가할 코드- jar 파일을 바로 실행하게 한다.  

<build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
     <archive>
      <index>true</index>
      <manifest>
       <mainClass>com.google.batch.ParameterJobLuancher</mainClass>
       <addClasspath>true</addClasspath>
       <classpathPrefix>lib/</classpathPrefix>
      </manifest>
     </archive>
    </configuration>
   </plugin>

...
</build>

Spring Batch의 CommandLineJobRunner 처럼 클래스를 하나 만듬

(http://www.docjar.com/html/api/org/springframework/batch/core/launch/support/CommandLineJobRunner.java.html)

 

 

 

Posted by '김용환'
,