Java定时quartz(spring)

applicationContext-beans-task.xml

<?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-2.0.xsd">

    <bean id="TestTimerController" class="com.wsn.cy.boss.controller.back.command.TestTimerController" />
    
    <bean id="testdingshiJob"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="TestTimerController"/>
        </property>
        <property name="targetMethod">
            <value>dingshi</value>
        </property>
        <!--将并发设置为false-->
         <property name="concurrent" value="false" />
    </bean>

    <bean id="crondingshi" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="testdingshiJob" />
        </property>
        <property name="cronExpression">
            <value>0/10 * * * * ?</value>
        </property>
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref local="crondingshi" />
            </list>
        </property>
    </bean>

</beans>
View Code

 

TestTimerController.java

package com.wsn.cy.boss.controller.back.command;

import java.util.Date;

import org.springframework.stereotype.Controller;

@Controller
public class TestTimerController {

      static int count = 0;
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }
    
    
    public void dingshi(){
           ++count;
           System.out.println("时间=" + new Date() + " 执行了" + count + "次"); // 1次
    }

}
View Code

 

posted on 2016-06-17 14:19  Galloper  阅读(205)  评论(0编辑  收藏  举报

导航