返回语句后执行的代码

时间:2023-02-07 01:12:12

Just curious to improve the response time of my program with following idea, please help in executing it :

只是好奇地通过以下想法改善我的程序的响应时间,请帮助执行它:

@Controller   
public class SendData{  
   @RequestMapping(value = "/getEmailId", method = RequestMethod.GET)  
   public String getUserMail(String userId) {  
      //getting Email Id From Database
      String emailId = getEmailIdFromDatabase(userId); 

      //send mail
      sendNotificationMail(emailId);

      // send data to requestor
      return emailId;  
     }  
}

Possible idea: To send Mail after returning EmailId to requestor

可能的想法:在将EmailId返回给请求者后发送邮件

@Controller   
public class SendData{  
   @RequestMapping(value = "/getEmailId", method = RequestMethod.GET)  
   public String getUserMail(String userId) {  
      //getting Email Id From Database
      String emailId = getEmailIdFromDatabase(userId); 

      // send data to requestor
      return emailId;  

      //send mail
      sendNotificationMail(emailId);
     }  
}

As I am doing it in bit large scale (eg. I am getting list of emailIds) so I want requestor to first get the emailIds and remove the waiting time to send them notification mail.

因为我正在大规模地做这件事(例如,我正在获取emailIds列表)所以我希望请求者首先获取emailIds并删除发送通知邮件的等待时间。

  • Please tell the solution if it is achievable by threading.
  • 请告诉解决方案是否可以通过线程实现。

  • If something like this is possible in any language other than Java?
  • 如果使用除Java以外的任何语言都可以实现这样的功能吗?

  • I have tried using finally block, but observed finally block executes before return statement.
  • 我已经尝试过使用finally块,但是在return语句之前观察到finally块执行了。

3 个解决方案

#1


0  

One possible and naive solution is just to start a new thread for mail sending:

一种可能而且天真的解决方案就是为邮件发送启动一个新线程:

new Thread() {
    @Override
    public void run() {
        sendNotificationMail(emailId);
    }
}.start();

A more robust version is to split between "I want send a mail" and "I send a mail". For example, instead of sending a mail just insert that mail in a table. With @Scheduled get inserted mail from the database, send it and delete it after that. So you can improve your response time. That it's. Sure, it's just an idea and you can use message bus or like, but you get the point.

更强大的版本是在“我想发送邮件”和“我发送邮件”之间进行划分。例如,不要发送邮件,只需将该邮件插入表中即可。使用@Scheduled从数据库中插入邮件,然后发送并删除它。这样您就可以缩短响应时间。就是这样。当然,这只是一个想法,你可以使用消息总线或类似,但你明白了。

#2


2  

Hi the second block of code you proposed is not valid. The code after the return statement is dead code, it won't be invoked at all, I guess java won't even let it compile.

您提出的第二个代码块无效。返回语句后的代码是死代码,它根本不会被调用,我猜java甚至不会让它编译。

I saw you are using Spring, so one solution to your problem would be to create a service to handle the email requests and use JavaMailSender class. This class can be configured using properties.yml, you can find a good example on following tutorial: Sending HTML Mail with Spring Boot and Thymeleaf

我看到你使用的是Spring,因此解决问题的方法之一就是创建一个服务来处理电子邮件请求并使用JavaMailSender类。可以使用properties.yml配置此类,您可以在以下教程中找到一个很好的示例:使用Spring Boot和Thymeleaf发送HTML Mail

To prevent it from blocking you just need to annotate the method responsible to send the email inside the Service class with @Async. This is most probably the best solution for your simple application.

为了防止它被阻塞,您只需要注释负责使用@Async在Service类中发送电子邮件的方法。这很可能是您简单应用程序的最佳解决方案。

PS: This solutions has some drawbacks. If your application needs to deal with lots of emails, it won't scale. To solve this you should look for a pattern where you will add requests to a queue and have another component of your architecture(a worker) just to be consuming the queue and sending the emails. Please bear in mind that this approach should be followed only if you really need it.

PS:这个解决方案有一些缺点。如果您的应用程序需要处理大量电子邮件,则无法扩展。要解决这个问题,您应该寻找一种模式,在这种模式中,您将向队列添加请求,并让您的架构的另一个组件(工作者)只是为了消耗队列并发送电子邮件。请记住,只有在您确实需要时才应遵循此方法。

#3


0  

Solution to the problem is to make your sendNotication service Asynchronous:

解决问题的方法是使sendNotication服务异步:

1. By using thread

new Thread() 
{
   @Override
   public void run() 
   {
      sendNotificationMail(emailId);
   }
}.start();

2. By using Java Messaging service

refer this URL : http://www.javatpoint.com/jms-tutorial

请参考以下URL:http://www.javatpoint.com/jms-tutorial

#1


0  

One possible and naive solution is just to start a new thread for mail sending:

一种可能而且天真的解决方案就是为邮件发送启动一个新线程:

new Thread() {
    @Override
    public void run() {
        sendNotificationMail(emailId);
    }
}.start();

A more robust version is to split between "I want send a mail" and "I send a mail". For example, instead of sending a mail just insert that mail in a table. With @Scheduled get inserted mail from the database, send it and delete it after that. So you can improve your response time. That it's. Sure, it's just an idea and you can use message bus or like, but you get the point.

更强大的版本是在“我想发送邮件”和“我发送邮件”之间进行划分。例如,不要发送邮件,只需将该邮件插入表中即可。使用@Scheduled从数据库中插入邮件,然后发送并删除它。这样您就可以缩短响应时间。就是这样。当然,这只是一个想法,你可以使用消息总线或类似,但你明白了。

#2


2  

Hi the second block of code you proposed is not valid. The code after the return statement is dead code, it won't be invoked at all, I guess java won't even let it compile.

您提出的第二个代码块无效。返回语句后的代码是死代码,它根本不会被调用,我猜java甚至不会让它编译。

I saw you are using Spring, so one solution to your problem would be to create a service to handle the email requests and use JavaMailSender class. This class can be configured using properties.yml, you can find a good example on following tutorial: Sending HTML Mail with Spring Boot and Thymeleaf

我看到你使用的是Spring,因此解决问题的方法之一就是创建一个服务来处理电子邮件请求并使用JavaMailSender类。可以使用properties.yml配置此类,您可以在以下教程中找到一个很好的示例:使用Spring Boot和Thymeleaf发送HTML Mail

To prevent it from blocking you just need to annotate the method responsible to send the email inside the Service class with @Async. This is most probably the best solution for your simple application.

为了防止它被阻塞,您只需要注释负责使用@Async在Service类中发送电子邮件的方法。这很可能是您简单应用程序的最佳解决方案。

PS: This solutions has some drawbacks. If your application needs to deal with lots of emails, it won't scale. To solve this you should look for a pattern where you will add requests to a queue and have another component of your architecture(a worker) just to be consuming the queue and sending the emails. Please bear in mind that this approach should be followed only if you really need it.

PS:这个解决方案有一些缺点。如果您的应用程序需要处理大量电子邮件,则无法扩展。要解决这个问题,您应该寻找一种模式,在这种模式中,您将向队列添加请求,并让您的架构的另一个组件(工作者)只是为了消耗队列并发送电子邮件。请记住,只有在您确实需要时才应遵循此方法。

#3


0  

Solution to the problem is to make your sendNotication service Asynchronous:

解决问题的方法是使sendNotication服务异步:

1. By using thread

new Thread() 
{
   @Override
   public void run() 
   {
      sendNotificationMail(emailId);
   }
}.start();

2. By using Java Messaging service

refer this URL : http://www.javatpoint.com/jms-tutorial

请参考以下URL:http://www.javatpoint.com/jms-tutorial