溫馨提示:您的每一次轉載,體現(xiàn)了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現(xiàn)了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
                  功能描述:剛開始接觸Quartz,試著用Quartz整合spring實現(xiàn)每隔一分鐘發(fā)送一封郵件連續(xù)發(fā)送10次
                  核心jar:
                              郵件發(fā)送:commons-email-1.2.jar mail.jar(必須的)
                              quartz:quartz-all-1.8.3.jar quartz-all-1.8.3/lib/下所有jar
                              spring:spring-context-support.ajr(必須的)
                  只貼出核心代碼:
                  Email發(fā)送:使用apache commons-email跟mail
          package com.ssh.commonsemail;

          import java.util.ArrayList;
          import java.util.Date;
          import java.util.List;

          import javax.mail.internet.InternetAddress;

          import org.apache.commons.mail.SimpleEmail;

          /**
           * 功能描述:此代碼主要實現(xiàn)郵件發(fā)送功能
           * 
          @author coder
           *
           
          */

          public class SendSimplEmail {
              
              
              
          public static void sendMail()throws Exception{
                  List
          <InternetAddress> list=new ArrayList<InternetAddress>();
                  list.add(
          new InternetAddress("313698683@qq.com"));
                  list.add(
          new InternetAddress("184675420@qq.com"));
                  SimpleEmail email
          =new SimpleEmail();
                  email.setFrom(
          "184675420@163.com");
                  email.setCharset(
          "utf-8");
                  email.setSentDate(
          new Date());
                  email.setSubject(
          "測試Quartz");
                  email.setHostName(
          "smtp.163.com");
                  email.setAuthentication(
          "xxxx""xxxx");
                  email.setTo(list);
                  email.setContent(
          "<h1>Hello,把鳳姐許配給你,你看咋樣?</h1>""text/html;charset=utf-8");
                  email.send();
              }

              
              
          }

                  定義調度工作任務:繼承自org.springframework.scheduling.quartz.QuartzJobBean次類在spring-context-support.jar中
          package com.ssh.quantz;

          import org.quartz.JobExecutionContext;
          import org.quartz.JobExecutionException;
          import org.springframework.scheduling.quartz.QuartzJobBean;

          import com.ssh.commonsemail.SendSimplEmail;

          /**
           * 發(fā)送email任務
           * 
          @author coder
           *
           
          */

          public class SendEmailJob extends QuartzJobBean{
              
              @Override
              
          protected void executeInternal(JobExecutionContext context)
                      
          throws JobExecutionException {
                  
          try {
                      
          //調用郵件發(fā)送代碼
                      SendSimplEmail.sendMail();
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              

          }

                  spring核心配置文件
          <!-- 定義調度工作任務 -->
              
          <bean id="quantzjobBean" class="org.springframework.scheduling.quartz.JobDetailBean">
                  
          <property name="jobClass">
                      
          <!-- 實現(xiàn)了org.springframework.scheduling.quartz.QuartzJobBean的JobBean -->
                      
          <value>com.ssh.quantz.SendEmailJob</value>
                  
          </property>
                  
          <!-- 調用業(yè)務邏輯 -->
                  
          <!--  
                  <property name="jobDataAsMap">
                      <map>
                          <entry key="biz">
                              <ref bean="users"/>
                          </entry>
                      </map>
                  </property>
                  
          -->
              
          </bean>

              
          <!-- 觸發(fā)任務條件 -->
              
          <bean id="simpletriggerbean" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
                  
          <property name="jobDetail" ref="quantzjobBean"></property>
                  
          <!-- 延遲一分鐘啟動 -->
                  
          <property name="startDelay">
                      
          <value>60000</value>
                  
          </property>
                  
          <!-- 每隔2分鐘調用一次 -->
                  
          <property name="repeatInterval">
                      
          <value>60000</value>
                  
          </property>
                  
          <!-- 執(zhí)行10次 -->
                  
          <property name="repeatCount">
                      
          <value>10</value>
                  
          </property>
                  
              
          </bean>
              
              
          <!-- 啟動調度 -->
              
          <bean id="startQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                  
          <property name="triggers">
                      
          <list>
                          
          <ref bean="simpletriggerbean"/>
                      
          </list>
                  
          </property>
              
          </bean>


                                  
          posted on 2010-07-13 18:03 雪山飛鵠 閱讀(6239) 評論(2)  編輯  收藏 所屬分類: spring

          Feedback

          # re: Spring整合Quartz定時發(fā)送郵件 2010-07-13 23:28 quartz
          學習了  回復  更多評論
            

          # re: Spring整合Quartz定時發(fā)送郵件 2010-07-19 13:20 roywong
          不錯。學習了
          org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
          +
          org.springframework.scheduling.quartz.CronTriggerBean
          +
          org.springframework.scheduling.quartz.SchedulerFactoryBean
          是不是更實用更靈活些

            回復  更多評論
            

          主站蜘蛛池模板: 凌云县| 辉南县| 彭山县| 西林县| 清流县| 兴安县| 临高县| 达孜县| 满城县| 塘沽区| 行唐县| 个旧市| 宜良县| 宜阳县| 东至县| 平和县| 牙克石市| 迁安市| 栾川县| 长治市| 黔江区| 海宁市| 天祝| 绥化市| 阿拉尔市| 福鼎市| 安阳县| 乐昌市| 嘉黎县| 托克托县| 郁南县| 志丹县| 昌黎县| 平谷区| 卓资县| 阿克陶县| 黎川县| 黄山市| 宁安市| 友谊县| 三江|