Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

12.7. Action 中使用綫程

背景,在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;
	}