Videos
You have to build job with JobbuilderFactory:
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public SomeReader<Some> reader() {
// some reader configuration
return reader;
}
@Bean
public SomeProcessor processor() {
return new SomeProcessor();
}
@Bean
public SomeWriter<Person> writer() {
// some config
return writer;
}
@Bean
public Job someJob() {
return jobBuilderFactory.get("someJob")
.flow(step1())
.end()
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Some, Some> chunk(10)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
}
Start job in rest controller:
@RestController
@AllArgsConstructor
@Slf4j
public class BatchStartController {
JobLauncher jobLauncher;
Job job;
@GetMapping("/job")
public void startJob() {
//some parameters
Map<String, JobParameter> parameters = new HashMap<>();
JobExecution jobExecution = jobLauncher.run(job, new JobParameters(parameters));
} }
And one important detail - add in application.properties:
spring.batch.job.enabled=false
to prevent job self start.
Solved by myself, as suggested here: Spring Boot: Cannot access REST Controller on localhost (404)
@SpringBootApplication
@EnableBatchProcessing
@EnableScheduling
@ComponentScan(basePackageClasses = JobStatusApi.class)
public class UpdateInfoBatchApplication {
public static void main(String[] args) {
SpringApplication.run(UpdateInfoBatchApplication.class, args);
}
}
It is possible to create your own custom ItemWriter.
In your case, please add the spring-boot-starter-web dependency to either your pom.xml or build.gradle
Example:
package com.example.batch;
import lombok.extern.log4j.Log4j2;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@Log4j2
public class RestItemWriter implements ItemWriter<String> {
@Autowired
RestTemplate restTemplate;
public RestItemWriter(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Override
public void write(List<? extends String> items) throws Exception {
ResponseEntity<Users> users = restTemplate.getForEntity("https://jsonplaceholder.typicode.com/users/1", Users.class);
log.info("Status code is: " + users.getStatusCode());
}
}
package com.example.batch;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Users {
public String id;
public String name;
public String username;
public String email;
public String phone;
}
More information about custom item writers here
Of course! you can send the processed records to another system using REST call in ItemWriter.
Use the below code in your RestItemWriter class.
private RestTemplate restTemplate;
@Override
public void write(@NonNull Chunk<? extends Payload> chunk) throws Exception {
for((Payload payload : chunk){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "xx-tokenString-xx");
HttpEntity<Payload> requestEntity = new HttpEntity<>(payload, headers);
try{
ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, Object.class);
LOGGER.info("Request hits the server {}", response.getBody());
} catch(HttpClientErrorException e){
LOGGER.error("HttpClientErrorException occured during connection {}", e.getMessage());
} catch (Exception e) {
LOGGER.error("Exception occured during connection {}", e.getMessage());
}
}
}
To make HTTP Patch request using RestTemplate, below configurations are mandatory, for other HTTP calls you may ignore it.
@Bean
public RestTemplate restTemplate() {
LOGGER.info("restTemplate Bean has bean created");
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
return restTemplate;
}
You have to add the below dependency in pom.xml to use CloseableHttpClient, and HttpClients.
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>