首页 > 解决方案 > 通过发送电子邮件返回响应(http 状态)需要很长时间

问题描述

我有 REST 控制器,我在其中更新数据库,然后向用户发送电子邮件。在前端,接收响应状态的时间太长。如何解决?我可以在发送电子邮件之前以某种方式发送响应状态吗?

    @PutMapping(value = "")
        @Secured({("ROLE_USER"),("ROLE_SUPERIOR"), ("ROLE_ADMIN")})
        public ResponseEntity updateApprovals(OAuth2Authentication auth, @RequestBody Approval approvals){
            String email = auth.getName();
            long userId = userService.getUserByEmail(email).get().getId();
            int approval = Integer.parseInt(approvals.getApproval());
            long doc_id = approvals.getDoc_id();

            documentService.updateSharing(userId, doc_id, approval); // DB inserting and sending emails

            return new ResponseEntity(HttpStatus.OK);
        }

@Override
    public void sendSimpleMessage(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        emailSender.send(message);
    }

标签: springspring-bootjakarta-mail

解决方案


我认为像发送电子邮件这样的功能最好在单独的线程中工作。例如)thread pool,,message queue...


推荐阅读