-2

I am developing a spring boot based web service where the user can query for some data, the data has to be fetched from db lazily, and has to be mailed to the user once data is fetched from db.

Can I use spring batch for this? Can anyone suggest some sample

Odin
  • 580
  • 11
  • 24
  • 1
    Yes, you can use Spring batch for this. However, providing an example would be **too broad** for Stack Overflow to explain, as you'll need an `ItemReader` (reading from DB), an `ItemWriter` (write to a PDF/CSV/... to send?), `JobParameters` (to pass the email address), a `Tasklet` (to actually send the mail) and things like JDBC/JavaMail to actually connect to a DB/mail server. That's too much to cover, I suggest you read the docs, try them out and ask a new question when you're stuck, containing the code you've tried. – g00glen00b Nov 07 '18 at 09:02
  • What do you mean by "lazily"? Do you mean that the user requests the data through the webapp, this request goes for example in a "user_requests" table in a database, then a batch job handles those requests and send emails with the data to the user afterwards? Please elaborate on your use case to be able to help you. – Mahmoud Ben Hassine Nov 07 '18 at 09:06
  • @MahmoudBenHassine yes exactly, user need not wait for the response once data is ready it gets mailed to the user – Odin Nov 07 '18 at 09:09
  • ok thanks for the clarification. See my answer. Hope it helps. – Mahmoud Ben Hassine Nov 07 '18 at 09:17

1 Answers1

1

Yes, you can use Spring Batch for this task. Here is a possible way to do it:

  • From the web controller, accept user requests with all needed details and store them is a requests table for example
  • Create a batch job that reads from that table and send the requested data via email to each user. The SimpleMailMessageItemWriter can be used for this task.

Hope this helps.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Thanks @Mahmoud Ben Hassine for the details, I have already developed the web part, currently stuck creating the Batch configuration was bit confused with the configurations wa checking this https://stackoverflow.com/questions/38949030/spring-batch-execute-dynamically-generated-steps-in-a-tasklet/38949824#38949824 – Odin Nov 07 '18 at 09:24