知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
背景,在Action中發送郵件,阻塞程序繼續執行並返回500,使用 Thread 實現非同步發送,因為我們並不關心郵件是否到達,只需正常發送即可。
public String execute(){ ... ... try { // Send email Thread sendmail = new Thread(new Runnable() { @Override public void run() { try { log.info("sendEmail Begin"); sender.sendEmail(form.getEmail(), form.getText()); log.info("sendEmail End"); } catch (Exception e) { e.printStackTrace(); } } }); sendmail.setName("sendmail" + sendmail.getId() + "logingName:" + form.getLoginname()); sendmail.start(); } catch (Exception e) { e.printStackTrace(); log.info("sendEmail Error"); } ... ... log.info("CreateTrialAccount:" + form.toString()); return Action.SUCCESS; }