如何使用线程或执行程序服务在 servlet 中发送邮件?

How to send mail within a servlet using threads or executor service?

我想根据条件发送邮件

ServletMail.java

//somecodes

//this code should code should be executed in background(by threads or something)

if(cond1){

sendmail(firstcond)

}

else{

sendmail(secondcond)

}

//requestdispatcher

您可以使用 ExecutorService executorService = Executors.newFixedThreadPool(threadNumber); 。还有threadNumber就是并发使用线程。

  1. 如果你使用 IOC 那么你可以声明 bean
  2. 第二种方式简单executorService如何声明静态变量;

You can use it like this

   if(cond1){
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                sendmail(firstcond);
            }
        });
    } else{
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                sendmail(secondcond);
            }
        });
    }

不要忘记将此 executorService.shutdown(); 放入 Web 服务器关闭侦听器。