Building a Rest api to recover data from a base using spring batch

Asked

Viewed 49 times

1

I am trying to build a Rest api to recover data from a table using spring batch.

Below my service Rest:

@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
public List<Deposit> requestJob1() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, NoSuchJobException, JobParametersNotFoundException, UnexpectedJobExecutionException {
    
    jobLauncher.run(jdbcCursorReaderJob, new JobParametersBuilder().addLong("uniqueness", System.nanoTime()).toJobParameters());
    
    return ?

Down with my Reader:

@Bean
public JdbcCursorItemReader<Deposit> jdbcCursorReader(
        @Qualifier("datasource") DataSource dataSource) {
    
    return new JdbcCursorItemReaderBuilder<Deposit>()
            .name("jdbcCursorReader")
            .dataSource(dataSource)
            .sql("select * from depositos")
            .rowMapper(new BeanPropertyRowMapper<Deposit>(Deposit.class))
            .build();
}

Down with my Writer:

public class JdbcCursorWriterConfig implements ItemWriter<Deposit> {

    List<Deposit> output = TransactionAwareProxyFactory.createTransactionalList();

    public void write(List<? extends Deposit> items) throws Exception {
        output.addAll(items);
    }

    public List<Deposit> getOutput() {
       return output;
    }

}

My doubts are:

1 - I think my Writer is wrong. How exactly should I write my Writer? 2 - How should I manipulate my Writer return in the Rest requestJob1 method?

1 answer

2

There are some points that we need to take into account

1 - Spring Batch will not return to you the values it did the processing. It is worth remembering that batches processing were created to work with a large amount of data, depending on the amount of data is not very complicated to traffic with it on the network. I have worked with processes that worked with hundreds of millions of data several times a day.

2 - If you are only going to use the batch to make a query in the database and return the values, I would not use the batch. It is much better if you retrieve them directly from the bank and return in your reply.

3 - It is not legal for you to leave your request waiting for the end of the processing to return the processed records. The idea is you in requestJob1 return the status of the job, or that it was starting, let it running async and then go where you saved this data and recover.

Now if your idea is to transform this data, read from some file and save in database, synchronize between different banks, then it would be nice to use batch for this. Then you create a service that recovers this data for you.

Just to finish, I think we should use the batch by call Rest, only for job start.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.