You can configure the asynchronous method with Spring to run in a separate thread.
@Service public class EmailAsyncService { ... @Autowired private MailService mailService; @Async public void sendEmail(User user, String email) { if (!user.getEmail().equals(email)) { user.setEmailTemp(email); Map map = new HashMap(); map.put("name", user.getName() + " " + user.getSurname()); map.put("url", "http://activationLink"); mailService.sendMail(map, "email-activation"); } } }
I made assumptions here on your model, but let me say that you can pass all the arguments needed to send this mail. If you configured correctly, this bean will be created as a proxy, and calling the annotated @Async method @Async execute it in another thread.
@Autowired private EmailAsyncService asyncService; ...
Spring's dock should be enough to help you set it up.
source share