How to send bulk emails using Spring Batch

Create project

The first thing we’ll need to do is head over to https://start.spring.io/ and create a new application. We’ll need the following dependencies:

  • MySQL driver
  • spring data JPA
  • java mail sender
  • Lombok — optional

application.properties

We’ll add the following properties to connect to our databases and email server

Configuring A Job

First, let’s create a class where we’ll configure our batch process. I’ve called mine BatchConfig. Then add these two annotations: @EnableBatchProcessing and @Configuration This is what the class looks like for now.

Reader

In the reader, we’ll read all the data from the orders table where emails have not yet been sent. We’ll use JdbcCursorItemReaderBuilder and a SQL query to fetch the data.

RowMapper

Here’s what the row mapper looks like. We map each column to its respective property in OrdersDTO.

Processor

In the processor, we’ll send emails for each order. We return a new instance of OrderProcessor where have the logic to send emails.

Email Sender service class

Email sender service is a component class to basically send emails. We use JavaMailSender.

Writer

Finally, we have a writer where we’ll save the updated data, ie the email_sent column has been set to true.

--

--

--

Software Engineer, London

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why we made our Reverse ETL solution Open Source

License to Freedom

Improving code testability with Swift protocols

Make Fake Virus Using Python

Is automation testing a silver bullet?

What happens inside a Coding Boot camp?

Longest Odd-Even Subsequence

STOCK PRICE TRACKER (python)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
theoneamin

theoneamin

Software Engineer, London

More from Medium

JWT authentication and role-based authorization in Springboot

Dynamic Multi Column Search with JPA Criteria.

Understanding LDAP through the Spring Boot

Multi-Module Micro-Service with Spring Boot (Part 3/4)

Hands typing on a laptop computer.