Rajesh Kumar
Rajesh Kumar

Reputation: 417

Implement logic to send a reminder email, 7 business days post initial email

We have a workflow implemented in java with Spring boot. On successful initiation of a workflow, we send an email to the approvers. The email functionality works asynchronously in the flow and this is implemented with spring's JavaMailSender and @Async. Now my requirement to send a reminder email after 7 business days post 1st email if the workflow is still not approved.

I was going through spring boot documents and found that we can run scheduled task with @Schedule annotation by properly implementing it. Please help me understand can I achieve this with spring boot task scheduler. If yes, how do I write cron expression to trigger rmail after 7 business day. If no, please help with some solution

Upvotes: 1

Views: 985

Answers (1)

Nikolai  Shevchenko
Nikolai Shevchenko

Reputation: 7521

It's better to store such tasks in persistent storage like DB, Redis, File system, etc, otherwise if your application gets restarted all pending scheduled tasks will be lost.

  1. Save approver email and timestamp of 1st email into, say, email_tasks SQL table
  2. Create worker that periodically fetches entries from this table such that where distance between task's creation timestamp and current timestamp is >= 7 business days. If record is found - trigger sending 2nd email.
  3. Launch this worker with @Scheduled every N minutes (N is up to you)
  4. If the workflow is approved before 7 days lasted then remove the task from table, so worker will ignore it

Upvotes: 2

Related Questions