user3808122
user3808122

Reputation: 823

Spring Batch - Copying file from remote location

I am new to Spring batch. I need to achieve the following: Step 1: Copy a file from remote location to local directory. Step 2: Process each line of the file. Step 3: Store the processed line into a database.

I am sure about the last two steps, but how can I achieve the first step?

Thanks for your help.

Upvotes: 3

Views: 3842

Answers (1)

Baji Shaik
Baji Shaik

Reputation: 1122

you can write a tasklet to achieve this this tasklet would be in separate step

<step id="movingFile" next="step2">
        <tasklet ref="voterImportFileMovingTasklet" />
        <listeners>
            <listener ref="stepLevelListener" />
        </listeners>
    </step>
<step id="step2" >
            <chunk reader="FileReader" processor="ItemProcessor" writer="ItemWriter" commit-interval="300"
                skip-limit="1000">
                <skippable-exception-classes>
                    <include class="java.lang.Exception" />
                    </skippable-exception-classes>
                <listeners>
                    <listener ref="voterImportListener" />
                </listeners>
            </chunk>
    </step>

Tasklet will be

public class FileMovingTasklet implements Tasklet, InitializingBean {
private Resource sourceDirectory;
private Resource targetDirectory;



private static final Log LOG = LogFactory.getLog(FileMovingTasklet.class);

public Resource getSourceDirectory() {
    return sourceDirectory;
}

public void setSourceDirectory(Resource sourceDirectory) {
    this.sourceDirectory = sourceDirectory;
}

public Resource getTargetDirectory() {
    return targetDirectory;
}

public void setTargetDirectory(Resource targetDirectory) {
    this.targetDirectory = targetDirectory;
}


@Override
public void afterPropertiesSet() throws Exception {
    Assert.notNull(sourceDirectory, "Source directory must be set");
    Assert.notNull(targetDirectory, "Target directory must be set");
}

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

    InputStream inStream = null;
    OutputStream outStream = null;
    File[] files;
    File dir = sourceDirectory.getFile();
    Assert.state(dir.isDirectory());
        files = dir.listFiles();
    File bfile = null;
    for (int i = 0; i < files.length; i++) {


            bfile = new File(targetDirectory.getURL().getPath() + File.separator + files[i].getName());

        inStream = new FileInputStream(files[i]);
        outStream = new FileOutputStream(bfile);

        byte[] buffer = new byte[1024];

        int length;
        // copy the file content in bytes
        while ((length = inStream.read(buffer)) > 0) {

            outStream.write(buffer, 0, length);

        }

        inStream.close();
        outStream.close();
    }
    return RepeatStatus.FINISHED;
}

Upvotes: 1

Related Questions