Answer the question
In order to leave comments, you need to log in
How to send email by determining the need to send at runtime?
Hello, I encountered the need to send an email notification with minimal user action.
The situation is as follows: conditionally, there is some field in the database that can change by itself (imitation of a sensor), as soon as the value of the temperature sensor becomes critical, the user receives a letter to the mail, they say go to the site, send instructions.
Ideally, this should be done without any user action.
I found 2 options
1) https://o7planning.org/ru/11145/spring-email-tutorialwith sending a letter when going to some url (which would be convenient given the secure context of the spring), that is, for an authorized user, we can always get a context by which we will already find the user in the database and pull out his mail, then we will send a letter to this mail.
But here, as you can see, the user needs to go to the site, which does not satisfy the requirement
2) https://www.baeldung.com/spring-email simple initialization of sending in the component, as I understand it, will be performed once, but if we constantly initialize this bean, then it can be performed with a certain frequency.
I encounter such a task for the first time and as I see it: while the application is running, a certain component continuously passes through all users, pulls out sensors for each user and checks the value for each sensor
sends a letter, if necessary, when it reaches the end, it falls asleep for a while and then again. But it sounds a little expensive, you have to spend a lot of memory to take it all out + a lot of time to get it all. In general, I think this is not the best way to affect performance.
But there are services that are able to send mail without user actions, so I ask you to explain on what basis they do this, thanks in advance
Answer the question
In order to leave comments, you need to log in
I'm not sure, but if I understand the task correctly, then in theory Spring Email should help you, @Scheduled
for example, for launching by cron, and @Async
for asynchronous sending. But perhaps there are better options.
Spring Integration will help with tracking changes in the database and sending email.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question