@BeforeStep annotated method, not callable

I am writing a spring batch job. But when this Archive class, which injects the task interface into the loadable one, the method under the @BeforeStep annotation is not called. Can anyone help me with this? Thank you.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;


    public class Archive implements Tasklet{
        @Override
        public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
                throws Exception {

            System.out.println("in execute method :)");
            return RepeatStatus.FINISHED;
        }

        @BeforeStep
        public void retrieveInterstepData(StepExecution stepExecution){
            JobExecution jobExecution = stepExecution.getJobExecution();
            ExecutionContext jobContext = jobExecution.getExecutionContext();

        }
    }

      

+3


source to share


2 answers


The first solution might be to extract ExecutionContext

from a execute

method where you have it ChunkContext

and do whatever you need with it.

ExecutionContext jobContext = chunkContext.getStepContext()
                                    .getStepExecution()
                                    .getJobExecution()
                                    .getExecutionContext();

      

The second solution is to implement StepExecutionListener

and override the method beforeStep

. You will have something like:



public class Archive implements Tasklet, StepExecutionListener{
    @Override
    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
                throws Exception {
        System.out.println("in execute method :)");
        return RepeatStatus.FINISHED;
    }

    @Override
    public void beforeStep(final StepExecution stepExecution) {
        JobExecution jobExecution = stepExecution.getJobExecution();
        ExecutionContext jobContext = jobExecution.getExecutionContext();
    }
}

      

I had a similar problem and we digested it this way. As to why @BeforeStep

is not called in tasklet, but it is inside readers

, processors

and writers

I'm not sure.

+4


source


third solution : you probably haven't registered your tasklet as a listener, so the annotated method is not called from the beginning. You can specify a link on the task as a listener in your xml definition of the task like this:

<job id="yourJob" > 
    <step id="step1">
            <tasklet ref="archive">
                <listeners>
                    <listener ref="archive" />
                </listeners>
            </
        </step>
</job> 

      



you also need to annotate you. Archive class:

@Component("archive")
@Scope("step")

      

+12


source







All Articles