电子邮件代码使 java spring MVC 中的代码变慢

发布于 2024-10-21 23:28:46 字数 1202 浏览 1 评论 0原文

我已经添加了这是我的控制器

@RequestMapping(value = "/persons/add", method = RequestMethod.POST)
        public String add(@Valid @ModelAttribute("personAttribute") Person person,
                BindingResult result) {
        logger.debug("Received request to add new person");


        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setTo("[email protected]");
        mailMessage.setSubject("This is the test message for testing gmail smtp server using spring mail");
        mailMessage.setFrom("[email protected]");
        mailMessage.setText("This is the test message for testing gmail smtp server using spring mail. \n" +
                "Thanks \n Regards \n Saurabh ");
        mailSender.send(mailMessage);


        if (result.hasErrors())
                return "hibernate/addpage";
        else
        personService.add(person);

                return "hibernate/addedpage";
    }

现在按下添加按钮后需要 5-6 秒

I have added this is my controller

@RequestMapping(value = "/persons/add", method = RequestMethod.POST)
        public String add(@Valid @ModelAttribute("personAttribute") Person person,
                BindingResult result) {
        logger.debug("Received request to add new person");


        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setTo("[email protected]");
        mailMessage.setSubject("This is the test message for testing gmail smtp server using spring mail");
        mailMessage.setFrom("[email protected]");
        mailMessage.setText("This is the test message for testing gmail smtp server using spring mail. \n" +
                "Thanks \n Regards \n Saurabh ");
        mailSender.send(mailMessage);


        if (result.hasErrors())
                return "hibernate/addpage";
        else
        personService.add(person);

                return "hibernate/addedpage";
    }

NOw it takes 5-6 seconds after pressing add button

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断念 2024-10-28 23:28:46

正如 David 所说,使用异步 API。我不建议创建新线程。此处为每个请求创建一个线程可能意味着创建许多线程来服务并发请求。最好使用池大小有限的线程池执行器,并将执行邮件发送的作业排队。 Google java 执行器以及如何在 spring 中使用它们;有多种实现方式。这意味着您的请求不会被阻止,并且它们将像您根本没有发送邮件一样快速执行(几乎)。

或者,使用本地邮件服务器 - 通过在本地主机上运行的邮件服务器发送要快得多,但我建议使用异步方法。然而,如果采用异步路线,则需要考虑一些事情,例如如何处理邮件发送失败。在错误情况下执行流程不同是否至关重要,或者您可以安全地忽略它吗?

Like David said, use an async API. I don't recommend creating a new thread. Creating a thread per request here can potentially mean that many many threads get created to service concurrent requests. Better to use a thread pool executor with a limited pool size and enqueue jobs that perform the mail sending. Google java executors and how to use them within spring; there are various implementations. This would mean your requests don't block and they will execute as quickly as if you were not sending mail at all (pretty much).

Alternatively, use a local mail server - sending via a mail server running on localhost is much faster, but I'd recommend the async approach. There are things to consider if going down the async route however, such as how to handle mail send failure. Is it critical that your flow of execution is different in an error condition or can you safely ignore it?

川水往事 2024-10-28 23:28:46

这是一个问题吗?

您正在同步发送邮件 - 这可能需要几秒钟。有什么问题吗?

我认为 gmail 也可能有效......

is this a question?

you are sending the mail synchronously - this might take a few seconds. whats the problem?

i think its also likely that gmail works....

呆橘 2024-10-28 23:28:46

这可能是意料之中的。向邮件服务器提交邮件消息不是即时的。

如果这是一个问题;使用异步 API 发送消息(或启动线程来执行此操作)。

That's probably to be expected. Submitting a mail message to a mail server is not instantaneous.

If that's a problem; use an asynchronous API for sending the message (or start a Thread to do it).

Saygoodbye 2024-10-28 23:28:46

最好使用异步邮件服务。

用于邮件的 Spring TaskExecutor

Better to use Async mail service.

Spring TaskExecutor for Mail

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文