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