Job 구현

Job을 구현하는 방법은 2가지가 있습니다.

  1. GlueService 구현

  2. Java Class 구현

GlueService 구현

예약어 ServiceName 에 해당하는 job001-service 를 개발합니다.
설치파일( scheduler-server-<version>.zip )에 포함된 sample1.jar 을 참고하세요.

  • job001-service.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <service name="job001-service" initial="Sleep" xmlns="http://www.poscoict.com/glueframework/service">
        <activity name="Sleep" class="sample.activity.SleepActivity">
            <transition name="success" value="end"/>
            <property name="sleep-second" value="sleep-second"/>
        </activity>
    </service>
    
  • sample.activity.SleepActivity
    package sample.activity;
    
    import com.poscoict.glueframework.biz.activity.GlueActivity;
    import com.poscoict.glueframework.biz.control.GlueBizControlConstants;
    import com.poscoict.glueframework.context.GlueContext;
    
    public class SleepActivity extends GlueActivity<GlueContext>
    {
        @Override
        public String runActivity( GlueContext ctx )
        {
            this.logger.info( "{}", this.getProperty( "sleep-second" ) );
            this.logger.debug( "{}", this.getProperty( "sleep-second" ) );
            this.logger.trace( "{}", this.getProperty( "sleep-second" ) );
            this.logger.warn( "{}", this.getProperty( "sleep-second" ) );
            this.logger.error( "{}", this.getProperty( "sleep-second" ) );
            if ( this.getProperty( "sleep-second" ) != null )
            {
                int s = Integer.parseInt( ctx.get( this.getProperty( "sleep-second" ) ).toString() );
                for ( int i = s * 10; i > 0; i-- )
                {
                    this.logger.debug( "{}", i );
                    try
                    {
                        Thread.sleep( 100 );
                    } catch ( InterruptedException e )
                    {}
                }
            }
            return GlueBizControlConstants.SUCCESS;
        }
    }
    

Java Class 구현

예약어 className 에 해당하는 sample.job.SamplePgm 를 구현합니다.
설치파일( scheduler-server-<version>.zip )에 포함된 sample2.jar 을 참고하세요.

com.poscoict.app.job.GlueJob interface를 상속합니다. (com.posco.glue.cm.PoscoSchedulerJob 은 deprecated 되었습니다.)
runProgram method를 구현시 try~catch 구문 안에서 biz 로직이 수행되도록 합니다.
biz 로직 수행시 발생된 모든 Error는 catch 구문에서 ctx 에 담습니다.

  • sample.job.SamplePgm
    package sample.job;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.poscoict.app.job.GlueJob;
    import com.poscoict.app.job.JobConstants;
    import com.poscoict.glueframework.context.GlueContext;
    
    // 1. interface
    public class SamplePgm implements GlueJob
    {
        // 2. logger
        Logger logger = LoggerFactory.getLogger( SamplePgm.class );
    
        @Override
        public void runProgram( GlueContext ctx )
        {
            // 3. try~catch
            try
            {
                // 4. biz logic 시작~
                this.logger.trace( "{}", ctx.entrySet() );
                this.logger.trace( "REQUEST ID : {}", ctx.get( JobConstants.JOB_REQUEST_ID ) );
                for ( String key : ctx.keySet() )
                {
                    this.logger.trace( "{}={}", key, ctx.get( key ) );
                }
    
                this.logger.info( "Hello" );
                this.logger.debug( "Hello" );
                this.logger.trace( "Hello" );
                this.logger.error( "Hello" );
                this.logger.warn( "Hello" );
    
                if ( ctx.get( "sleep-second" ) != null )
                {
                    int s = Integer.parseInt( ctx.get( "sleep-second" ).toString() );
                    for ( int i = s * 10; i > 0; i-- )
                    {
                        this.logger.debug( "{}", i );
                        try
                        {
                            Thread.sleep( 100 );
                        } catch ( InterruptedException e )
                        {}
                    }
                }
                // 4. biz logic 끝~
            } catch ( Throwable t )
            {
                ctx.setException( t );
            }
        }
    }
    

applicationContext.xml

sample1.jar 에 포함된 applicationContxt.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="serviceManager" class="com.poscoict.glueframework.biz.control.GlueServiceManagerImpl" lazy-init="true">
        <property name="cacheManager" ref="cacheManager" />
        <property name="serviceLoader" ref="serviceLoader" />
    </bean>
    <bean id="cacheManager" class="com.poscoict.glueframework.cache.ehcache.GlueEhCacheManager" />
    <bean id="serviceLoader" class="com.poscoict.glueframework.biz.control.GlueServiceLoader"/>
</beans>

biz 로직에서 DB를 사용하는 경우 아래를 참고해서 bean을 추가합니다.
placeholderConfig bean을 통해 DB 연결정보 등은 user-application.properties 에 설정할 수 있습니다.

  • applicationContext.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="file:${CONFIG_PATH}/user-application.properties" />
            <property name="ignoreUnresolvablePlaceholders" value="true" />
        </bean>
        <bean id="serviceManager" class="com.poscoict.glueframework.biz.control.GlueServiceManagerImpl" lazy-init="true">
            <property name="cacheManager" ref="cacheManager" />
            <property name="serviceLoader" ref="serviceLoader" />
        </bean>
        <bean id="cacheManager" class="com.poscoict.glueframework.cache.ehcache.GlueEhCacheManager" />
        <bean id="serviceLoader" class="com.poscoict.glueframework.biz.control.GlueServiceLoader"/>
        <bean id="biz-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${biz-ds.driverClassName}" />
            <property name="url" value="${biz-ds.url}" />
            <property name="username" value="${biz-ds.username}" />
            <property name="password" value="${biz-ds.password}" />
            <property name="defaultAutoCommit" value="false" />
            <property name="minIdle" value="0" />
            <property name="maxActive" value="-1" />
            <property name="maxIdle" value="8" />
        </bean>
        <bean id="biz-dao" class="com.poscoict.glueframework.dao.jdbc.GlueJdbcDao">
            <property name="dataSource" ref="biz-ds"/>
            <property name="queryManager" ref="queryManager"/>
        </bean>
        <bean id="biz-tx" class="com.poscoict.glueframework.transaction.GlueDataSourceTransactionManager">
            <property name="dataSource" ref="biz-ds"/>
        </bean>
        <bean id="queryManager" class="com.poscoict.glueframework.dao.manager.GlueQueryManagerImpl">
            <property name="cacheManager" ref="cacheManager" />
            <property name="queryLoader" ref="queryLoader" />
        </bean>
        <bean id="queryLoader" class="com.poscoict.glueframework.dao.manager.GlueQueryLoader"/>
    </beans>
    
  • user-application.properties
    biz-ds.driverClassName=oracle.jdbc.driver.OracleDriver
    biz-ds.url=jdbc:oracle:thin:@127.0.0.1:1521:XE
    biz-ds.username=scott
    biz-ds.password=tiger
    

log configuration

로깅을 위해 다음 2가지 중 하나를 사용할 수 있습니다.
File Appender를 사용하고, file명은 log.file 이라는 변수로 지정합니다.

  1. logback 사용시

    logback-text.xml 파일은 다음과 같습니다.
    log.file 변수명은 예약어입니다.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration><!--  debug="true" -->
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <file>${log.file}</file>
            <append>true</append>
            <encoder>
                <pattern>%d %-5level %logger - %msg%n</pattern>
            </encoder>
        </appender>
    
        <logger name="org.springframework">
            <level value="info" />
        </logger>
        <logger name="org.apache">
            <level value="warn" />
        </logger>
        <logger name="net.sf.ehcache">
            <level value="warn" />
        </logger>
        <logger name="com.posco">
            <level value="trace" />
        </logger>
        <logger name="com.poscoict">
            <level value="debug" />
        </logger>
    
        <root level="info">
            <appender-ref ref="FILE" />
        </root>
    </configuration>
    
  2. log4j 사용시

    log4j.xml 파일은 다음과 같습니다.
    log.file 변수명은 예약어입니다.

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
        <appender name="FILE" class="org.apache.log4j.RollingFileAppender">
            <param name="append" value="true"/>
            <param name="file" value="${log.file}"/>
            <param name="maxFileSize" value="5MB"/>
            <param name="maxBackupIndex" value="20"/>
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value = "%d %-5p - [%t]%C||%m%n"/>
            </layout>
        </appender>
    
        <logger name="org.springframework">
            <level value="info" />
        </logger>
        <logger name="org.apache">
            <level value="warn" />
        </logger>
        <logger name="net.sf.ehcache">
            <level value="warn" />
        </logger>
        <logger name="com.posco">
            <level value="trace" />
        </logger>
        <logger name="com.poscoict">
            <level value="debug" />
        </logger>
    
        <root>
            <level value="warn" />
            <appender-ref ref="FILE" />
        </root>
    </log4j:configuration>
    

How to test

main method를 만들어 실행합니다.
Job Parameter는 GlueDefaultContext를 이용합니다.

    public static void main(String args[])
    {
        System.setProperty( "CONFIG_PATH", ... );
        System.setProperty( "log.file", ... );

        GlueContext ctx = null

        // 1번 테스트
        ctx = new GlueDefaultContext();
        ctx.put( GlueBizControlConstants.SERVICE_NAME, "job001-service" );
        ctx.put("sleep-second", "10");
        GlueBizProvider.getController().doAction( ctx );

        //2번 테스트
        ctx = new GlueDefaultContext();
        ctx.put("sleep-second", "5");
        new SamplePgm().runProgram(ctx);
    }