Programming & Development / April 19, 2025

Spring Batch Job with JPA Reader, API Call Processor, and JPA Writer

Spring Batch JPA Reader API Call JPA Writer ItemProcessor ItemReader Spring Boot Batch Processing RestTemplate JpaPagingItemReader JpaItemWriter API Integration

1. POM Dependencies

Ensure that you have the required dependencies in your pom.xml if you are using Maven.

xml

<dependencies>
    <!-- Spring Batch Core -->
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
    </dependency>

    <!-- Spring Boot Starter Data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- Spring Boot Starter Web (for RestTemplate) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- H2 Database (or your preferred database) -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- Spring Boot Starter Batch -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>

    <!-- Spring Boot Starter Test (for testing) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2. Entity Class

Define the YourEntity class that represents the data in your database.

java

@Entity
@Table(name = "your_entity")
public class YourEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String status;
    // Other fields

    // Getters and setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    // Other getters and setters
}

3. API Response Class

Define the ApiResponse class to map the response from the API call.

java

public class ApiResponse {
    private String status;
    // Other fields

    // Getters and setters
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    // Other getters and setters
}

4. Spring Batch Configuration

Create the configuration class for the Spring Batch job.

java

@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public JpaPagingItemReader<YourEntity> jpaReader() {
        JpaPagingItemReader<YourEntity> reader = new JpaPagingItemReader<>();
        reader.setEntityManagerFactory(entityManagerFactory);
        reader.setQueryString("SELECT e FROM YourEntity e WHERE e.status = :status");
        reader.setParameterValues(Collections.singletonMap("status", "PENDING"));
        reader.setPageSize(10); // Configure the page size
        return reader;
    }

    @Bean
    public ItemProcessor<YourEntity, YourEntity> apiProcessor() {
        return item -> {
            // Call your API here
            ApiResponse response = restTemplate.postForObject("https://api.example.com/endpoint", item, ApiResponse.class);
            
            // Update the entity with the response or status
            item.setStatus(response.getStatus());
            return item;
        };
    }

    @Bean
    public JpaItemWriter<YourEntity> jpaWriter() {
        JpaItemWriter<YourEntity> writer = new JpaItemWriter<>();
        writer.setEntityManagerFactory(entityManagerFactory);
        return writer;
    }

    @Bean
    public Step processStep() {
        return stepBuilderFactory.get("processStep")
                .<YourEntity, YourEntity>chunk(10)
                .reader(jpaReader())
                .processor(apiProcessor())
                .writer(jpaWriter())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("job")
                .incrementer(new RunIdIncrementer())
                .start(processStep())
                .build();
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

5. Application Class

Run the Spring Boot application.

java

@SpringBootApplication
public class BatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(BatchApplication.class, args);
    }
}

6. Database Configuration

Configure your application.properties or application.yml for database connectivity.

properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true

# Other database configurations

7. Run the Job

Once the application is set up, running the Spring Boot application will automatically trigger the batch job. The job will:

  1. Read entities from the database using the JpaPagingItemReader.
  2. Call an API for each entity to fetch or update its status.
  3. Write the updated status back to the database using JpaItemWriter.

This configuration is designed for a simple job processing logic. You can modify the entity, processor, and other configurations based on your specific requirements, such as handling errors, managing transactions, or integrating with different APIs.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex