Spring中quartz的簡單例子

來源:互聯網
上載者:User

 資訊首頁要1個小時靜態化一次,之前做法很齷齪,今天用了spring中來配置quartz。看一下簡單例子吧:

 

 

  1. <!-- quartz配置 -->
  2.     <bean id="indexTask" class="com.tsts168.sunyanan.app.index.support.IndexTask"></bean>
  3.     <!-- 定義調用對象和調用對象的方法 -->
  4.     <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  5.         <!-- 調用的類 -->
  6.         <property name="targetObject">
  7.             <ref bean="indexTask"/>
  8.         </property>
  9.         <!-- 調用類中的方法 -->
  10.         <property name="targetMethod">
  11.             <value>run</value>
  12.         </property>
  13.         <property name="concurrent" value="false" />
  14.     </bean>
  15.     <!-- 定義觸發時間 -->
  16.     <bean id="doTime" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  17.         <property name="jobDetail">
  18.             <ref bean="jobtask"/>
  19.         </property>
  20.         <!-- repeat every 1小時 3600000 -->
  21.         <property name="repeatInterval" value="3600000" />
  22.     </bean>
  23.     <!-- 總管理類 如果將lazy-init='false'那麼容器啟動就會執行發送器  -->
  24.     <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  25.         <property name="triggers">
  26.             <list>
  27.                 <ref bean="doTime"/>
  28.             </list>
  29.         </property>
  30.     </bean>

 

  1. package com.tsts168.sunyanan.app.index.support;
  2. import java.util.Date;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. import com.tsts168.sunyanan.app.common.util.HtmlUtil;
  8. import com.tsts168.sunyanan.app.index.manager.IndexManager;
  9. /*
  10.  * 首頁2小時產生一次的調度
  11.  * 
  12.  */
  13. public class IndexTask {
  14.     private Log log = LogFactory.getLog(IndexTask.class);
  15.     
  16.     @SuppressWarnings("deprecation")
  17.     public void run() {
  18.         
  19.          // 產生靜態頁面
  20.         ApplicationContext scontext = new ClassPathXmlApplicationContext("applicationContext.xml");
  21.         IndexManager manager = (IndexManager) scontext.getBean("indexManager");
  22.         Object obj = manager.service(null);
  23.         HtmlUtil hutil = new HtmlUtil();
  24.         
  25.         hutil.writeIndexHtml(System.getProperty("tsts.root") + "/template", System.getProperty("tsts.root"), obj);
  26.         log.warn("產生首頁at: " + (new Date()).toLocaleString());
  27.     }
  28. }

就不多做介紹了,很明了

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.