Urgent.News

What's breaking now, across thousands of outlets.

Tech

🚀 Building Spring by Example V2: Completing Module 07 - Spring Batch

When I started learning Spring, I noticed something about many of the tutorials I found. They often showed how to use a feature, but not always why it worked or how the different pieces fitted together in a real application. I learn best by building things and documenting what I discover, so I started a project called spring-by-example . The idea is simple: instead of building one large…

When learning about Spring, many tutorials demonstrated features without explaining the underlying reasons or how the components fit within a real-world application. To address this, I began a project called spring-by-example. The approach is straightforward: rather than constructing a single large application, I create a series of small, focused examples that each delve into a specific Spring concept, accompanied by clear explanations, tests, and documentation. I am pleased to announce that I have now completed Module 07 - Spring Batch.

This module progresses from the basics of batch processing to advanced topics such as job execution metadata, restartability, fault tolerance, and testing. The module encompasses:

Jobs and Steps: A Job represents the entire batch process, while Steps define the individual operations that comprise that process.

Job Parameters: Parameters enable values to be supplied to a job during runtime, allowing for runtime customization of job behavior.

Item Readers, Processors, and Writers: These constitute the cornerstone of chunk-oriented processing. An ItemReader retrieves input data, an ItemProcessor modifies or filters it, and an ItemWriter outputs the processed items. For instance, a processor might convert an order description to uppercase:

@Override

public ProcessedOrder process(Order order) {

return new ProcessedOrder(order.getId(), order.getDescription().toUpperCase(Locale.ROOT));

}

Chunk-Oriented Processing: This technique groups items into batches for processing and writing, enhancing transaction boundaries and controlling execution flow. For example, processing items in groups of three:

new ChunkOrientedStepBuilder(Order, ProcessedOrder) (processOrdersStep, jobRepository, 3)

.reader(orderItemReader)

.processor(orderItemProcessor)

.writer(orderItemWriter)

.build();

Tasklet Processing: Unlike chunk-oriented processing, a tasklet is suitable for a single operation or a small, self-contained task, such as archiving records or executing database operations.

Job Execution and Metadata: Spring Batch records information about job instances, job executions, and step executions within a JDBC-backed JobRepository, enabling the exposure of execution data via REST endpoints and understanding the outcome of a batch run.

Restartability: A key feature of Spring Batch, restartability allows a batch job to fail partway through execution without necessitating a complete restart. The job can leverage persisted execution metadata and execution contexts to resume processing from the point it left off.

Skip and Retry: Spring Batch offers policies for handling failures differently. Some records may be invalid and can be skipped, while temporary failures might be retried. For example:

.faultTolerant()

.skipPolicy(skipPolicy)

.retryPolicy(retryPolicy)

The module concludes with batch testing, utilizing @SpringBatchTest, JobOperatorTestUtils, and Spring Boot testing support. This covers testing individual components, executing jobs and steps, verifying execution metadata, and testing step-scoped components. It emphasizes that batch application testing requires more than just verifying the final output; it also entails checking execution status, read/write counts, commit counts, job parameters, and the behavior of individual steps.

The spring-by-example project is open source, and I continue to expand it as I learn more. You can find it on GitHub (https://github.com/dsAQrAU5) or GitLab (https://gitlab.com/gfDEyGH3).

Completing Module 07 - Spring Batch represents a significant milestone in my journey. The next module will be Spring Integration, where I will continue to explore the Spring ecosystem through small, focused, runnable examples. One focused example at a time.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

The Case of the Blank SharePoint Page (and the Guest Who Couldn't See It)

SPFx web part and application customizer not loading for guest or external users, and the Public CDN fix TL;DR: Guests can't read your SPFx bundles from the app catalog, so the page renders blank.

  • Guests using Gmail see blank SharePoint page due to SPFx bundles not loading
  • Error "CannotCreatePersonalSite" is red herring, not root cause
  • Enabling Public CDN with /CLIENTSIDEASSETS origin resolves issue

Cloud Bill Out of Control: Building a Reliable Budget Alert with OpenCost and Prometheus

You get the email on the 3rd of the month: last month's cloud invoice is 40% above forecast. You open the billing console, and it tells you exactly one useful thing — the account spent too much.

  • OpenCost provides near real-time cloud cost estimates for Kubernetes clusters
  • Exposes metrics like CPU, memory, volume consumption, and hourly costs per namespace
  • Enables per-namespace budget alerts with custom pricing for improved accuracy

More from Sunday 8 November →