-1

I want to send mail based on if a condition

ServletMail.java

//somecodes

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

if(cond1){

sendmail(firstcond)

}

else{

sendmail(secondcond)

}

//requestdispatcher
Shashwat
  • 2,342
  • 1
  • 17
  • 33
  • This site is for narrowly-focused questions on a specific technical issue. This is much too broad. Servlet, mail, and executors have been covered in many other questions and answers. – Basil Bourque Aug 13 '18 at 09:10

1 Answers1

2

You can use ExecutorService executorService = Executors.newFixedThreadPool(threadNumber); . There threadNumber is concurrent use threads.

  1. If you use IOC then you can declaration bean
  2. Second way simple executorService declaration how static variable;

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);
            }
        });
    }

Do not forget put this executorService.shutdown(); to web server shutdown listener.