熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring整合Quartz定時發送郵件

2013-11-23 20:19:10  來源: Java開源技術 

  功能描述:剛開始接觸Quartz試著用Quartz整合spring實現每隔一分鐘發送一封郵件連續發送

  核心jar:

  郵件發送commonsemailjar mailjar(必須的)

  quartzquartzalljar quartzall/lib/下所有jar

  springspringcontextsupportajr(必須的)

  只貼出核心代碼:

  Email發送使用apache commonsemail跟mail

  package monsemail;

  import javautilArrayList;

  import javautilDate;

  import javautilList;

  import javaxmailinternetInternetAddress;

  import monsmailSimpleEmail;

  /** *//**

  * 功能描述:此代碼主要實現郵件發送功能

  * @author coder

  *

  */

  public class SendSimplEmail {

  public static void sendMail()throws Exception{

  List<InternetAddress> list=new ArrayList<InternetAddress>();

  listadd(new InternetAddress());

  listadd(new InternetAddress());

  SimpleEmail email=new SimpleEmail();

  emailsetFrom();

  emailsetCharset(utf);

  emailsetSentDate(new Date());

  emailsetSubject(測試Quartz);

  emailsetHostName();

  emailsetAuthentication(xxxx xxxx);

  emailsetTo(list);

  emailsetContent(<h>Hello把鳳姐許配給你你看咋樣?</h> text/html;charset=utf);

  emailsend();

  }

  }

  定義調度工作任務繼承自orgspringframeworkschedulingquartzQuartzJobBean次類在springcontextsupportjar中

  package comsshquantz;

  import orgquartzJobExecutionContext;

  import orgquartzJobExecutionException;

  import orgspringframeworkschedulingquartzQuartzJobBean;

  import monsemailSendSimplEmail;

  /** *//**

  * 發送email任務

  * @author coder

  *

  */

  public class SendEmailJob extends QuartzJobBean{

  @Override

  protected void executeInternal(JobExecutionContext context)

  throws JobExecutionException {

  try {

  //調用郵件發送代碼

  SendSimplEmailsendMail();

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }

  spring核心配置文件

  <! 定義調度工作任務 >

  <bean id=quantzjobBean class=orgspringframeworkschedulingquartzJobDetailBean>

  <property name=jobClass>

  <! 實現了orgspringframeworkschedulingquartzQuartzJobBean的JobBean >

  <value>comsshquantzSendEmailJob</value>

  </property>

  <! 調用業務邏輯 >

  <!

  <property name=jobDataAsMap>

  <map>

  <entry key=biz>

  <ref bean=users/>

  </entry>

  </map>

  </property>

  >

  </bean>

  <! 觸發任務條件 >

  <bean id=simpletriggerbean class=orgspringframeworkschedulingquartzSimpleTriggerBean>

  <property name=jobDetail ref=quantzjobBean></property>

  <! 延遲一分鐘啟動 >

  <property name=startDelay>

  <value></value>

  </property>

  <! 每隔分鐘調用一次 >

  <property name=repeatInterval>

  <value></value>

  </property>

  <! 執行>

  <property name=repeatCount>

  <value></value>

  </property>

  </bean>

  <! 啟動調度 >

  <bean id=startQuartz class=orgspringframeworkschedulingquartzSchedulerFactoryBean>

  <property name=triggers>

  <list>

  <ref bean=simpletriggerbean/>

  </list>

  </property>

  </bean>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28292.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.