1. 程式人生 > >spring+springmvc+mybatis寫quartz定時任務

spring+springmvc+mybatis寫quartz定時任務

1:pom.xml裡面加上quartz包的配置

		<dependency>
		    <groupId>org.quartz-scheduler</groupId>
		    <artifactId>quartz</artifactId>
		    <version>2.2.1</version>
		</dependency>

2:spring-mvc.xml裡面配置quartz定時任務的一些資訊

	<!-- 寫定時任務的類建立一個bean -->
    <bean id="senUserUnsubQuery" class="com.ssm.utils.SenUserUnsubQuery">
    </bean>
	<!-- 指定定時任務的類和方法 -->
    <bean id="senUserQueryJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="senUserUnsubQuery"/>
        <property name="targetMethod" value="senUserQuery"/>
        <!--false 代表不執行併發任務-->
        <property name="concurrent" value="false"/>
    </bean>
	<!-- 配置定時任務執行的時間 -->
    <bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="senUserQueryJobDetail"/>
        <property name="cronExpression" value="0/3 * * * * ? "/>
    </bean>
	<!-- 啟動排程 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
        <property name="jobDetails" ref="senUserQueryJobDetail"/>
        <property name="triggers" ref="CronTriggerBean"/>
    </bean>

3:建立定時任務類:SenUserUnsubQuery.java,然後寫定時任務 配置檔案裡設定的是每隔3秒執行一次 :0/3 * * * * ?

	protected void senUserQuery(){
		System.out.println("開始");
		System.out.println("結束");
	}