Quartz定時組件

Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目,它可以與J2EE與J2SE應用程序相結合也可以單獨使用。


package xuyan.com;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MyJob {
  private static	Log logger = LogFactory.getLog(MyJob.class);
	 IdWorker worker2 = new IdWorker(2);
	 
	 
	 
	 List<String> list =new ArrayList<String>();
	 public void work()//方法名稱  需要寫在配置文件中
	    {
	         System.out.println("當前時間:"+new Date().toString()+":   quartz定時器開始執行");  

	    }
}

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   
    
    <!-- 工作的bean -->
    <bean id="myJob" class="xuyan.com.MyJob" />
    
     <!-- job的配置開始 -->
    <bean id="myJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="myJob" />
        </property>
        <property name="targetMethod">
            <value>work</value> <!-- MyJob中的方法名稱 -->
        </property>
    </bean>
    <!-- job的配置結束 -->
    
    <!-- 調度的配置開始 -->
    <bean id="crontestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="myJobDetail" />
        </property>
        <property name="cronExpression">
            <value>0/1 * * * * ?</value>
        </property>
    </bean>
    <!-- 調度的配置結束 -->
    
      <!-- 啓動觸發器的配置開始 -->
    <bean name="startQuertz" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger" />

            </list>
        </property>
    </bean>
    <!-- 啓動觸發器的配置結束 -->
    
 
</beans>

package xuyan.com;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * quartz 定時器
 * @author Administrator
 *
 */
public class main {

	public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
          //User user=(User) context.getBean("user");
         // user.getName();

	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章