'This job run every 20s but noting saved in database, on the first time after first run the batch work but after that although the job is running
package com.wombatrack.configBatch;
import com.wombatrack.domain.entites.UserInstagram;
import com.wombatrack.service.UserInstagramService;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Bean
ItemReader<UserInstagram> restUserInstagramReader(UserInstagramService userInstagramService) {
return new RESTUserInstagramReader(userInstagramService);
}
@Bean
ItemProcessor<UserInstagram, UserInstagram> restUserInstagramProcessor() {
return new LoggingUserInstagramProcessor();
}
@Bean
ItemWriter<UserInstagram> restUserInstagramWriter() {
return new LoggingUserInstagramWriter();
}
@Bean
Step restUserInstagramStep(ItemReader<UserInstagram> restUserInstagramReader,
ItemProcessor<UserInstagram, UserInstagram> restUserInstagramProcessor,
ItemWriter<UserInstagram> restUserInstagramWriter,
StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("restUserInstagramStep")
.<UserInstagram, UserInstagram>chunk(5)
.reader(restUserInstagramReader)
.processor(restUserInstagramProcessor)
.writer(restUserInstagramWriter)
.allowStartIfComplete(true)
.build();
}
@Bean
Job restUserInstagramJob(JobBuilderFactory jobBuilderFactory,
@Qualifier("restUserInstagramStep") Step restUserInstagramStep) {
return jobBuilderFactory.get("restUserInstagramJob")
.incrementer(new RunIdIncrementer())
.flow(restUserInstagramStep)
.end()
.build();
}
}
I have a probleme with this peace of code my job run first time, but after that return user null that mean busness ligique of job run one time whene the application start first time
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BatchLauncher {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public BatchStatus run() throws JobParametersInvalidException, JobExecutionAlreadyRunningException,
JobRestartException, JobInstanceAlreadyCompleteException {
JobParameters parameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
.toJobParameters();
JobExecution jobExecution = jobLauncher.run(job, parameters);
return jobExecution.getStatus();
}
}
** I put a schedular that will run every 20 seconds the job runs after every 20 seconds as expected, but the problem is that the business, i.e. the backup at the database level does not work no effect. the job does its role only the first time after the application is started in the first time, but after that the scheduler makes the job run every 20 seconds but the information is not saved, I have a return every time the job with status COMPLETED but the job does nothing in terms of saving new data**
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class Schedular {
@Autowired
private BatchLauncher batchLauncher;
@Scheduled(fixedDelay = 20000)
public void perform() throws Exception {
log.info("Batch programmé pour tourner toutes les 20 secondes");
batchLauncher.run();
}
}
**I don't have any idea how to do it to fix this problem, i debug application but I did not find any solution Please if someone can help me **
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
