lcn.module.batch.web.guide.support.ErrorLogTasklet.java Source code

Java tutorial

Introduction

Here is the source code for lcn.module.batch.web.guide.support.ErrorLogTasklet.java

Source

/*
 * Copyright 2006-2007 the original author or authors. *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lcn.module.batch.web.guide.support;

import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.util.Assert;

/**
 * Data  Error ?  Skip ?   DB  ?
 * 
 * @author 
 * @since 2012. 07.30
 * @see <pre>
 * << ?(Modification Information) >>  
 * ?               ?                
 * ------      --------     --------------------------- 
 * 2012. 07.30       ?
 * </pre>
 */
public class ErrorLogTasklet implements Tasklet, StepExecutionListener {

    protected final Log logger = LogFactory.getLog(getClass());

    //simpleJdbcTemplate
    private SimpleJdbcTemplate simpleJdbcTemplate;

    //jobName
    private String jobName;

    //stepExecution
    private StepExecution stepExecution;

    private String stepName;

    /**
     * Error_Log ?? Skip Error ?   
     */
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        Assert.notNull(this.stepName, "Step name not set.  Either this class was not registered as a listener "
                + "or the key 'stepName' was not found in the Job's ExecutionContext.");
        this.simpleJdbcTemplate.update(
                "insert into ERROR_LOG values (?, ?, '" + getSkipCount() + " records were skipped!')", jobName,
                stepName);
        return RepeatStatus.FINISHED;
    }

    /**
     * skipCount  Execution ? 
     */
    private int getSkipCount() {
        if (stepExecution == null || stepName == null) {
            return 0;
        }
        for (StepExecution execution : stepExecution.getJobExecution().getStepExecutions()) {
            if (execution.getStepName().equals(stepName)) {
                return execution.getSkipCount();
            }
        }
        return 0;
    }

    /**
     * DataSource 
     */
    public void setDataSource(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

    /**
     * Step ? ? ?,  stepName ? JobExecution? Context?  
     */
    public void beforeStep(StepExecution stepExecution) {
        this.jobName = stepExecution.getJobExecution().getJobInstance().getJobName().trim();
        this.stepName = (String) stepExecution.getJobExecution().getExecutionContext().get("stepName");
        this.stepExecution = stepExecution;
        stepExecution.getJobExecution().getExecutionContext().remove("stepName");
    }

    /**
     * Step ? 
     */
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }

}