2

我们正在使用 ScheduledTimerTask 来管理我们应用程序中的作业(自动)。我们使用以下代码:

<bean id="SampleTask" 
  class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
    <property name="targetObject" ref="sampleScheduler" />
    <property name="targetMethod" value="runMethod" />
</bean>

<bean id="sampleScheduler" class="com.sample.SampleScheduler" />

<bean id="timerTask"
    class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="timerTask" ref="schedulerTask" />
    <property name="delay" value="1000" />
    <property name="period" value="60000" />
</bean>

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref local="timerTask" />
        </list>
    </property>
</bean>

通过使用上面的代码runMethod,每一分钟都可以正常工作。但是我们想根据服务器时间更改执行。例如,如果服务器时间是下午 6 点,那么我们需要调用该函数。如何实现?

帮助将不胜感激。

4

3 回答 3

1

您可以使用 Spring 的任务触发器在特定时间段调用,您可以使用CronTrigger.

详细文档在这里

于 2011-12-01T06:57:22.330 回答
1

如果您必须只使用 ScheduledTimerTask,则每分钟调用一次计时器任务。现在从计时器任务中检查时间是否为早上 6 点并运行必要的功能。否则返回。

缺点:这个没有操作的任务将每分钟执行一次(除非它必须进行一些处理。

或者

从春天开始只调用一次 TimeTask。当任务执行时,您可以获得当前时间,并可以计算当前时间与执行实际任务的时间之间的差值。(让这种差异称为延迟)。

现在从这个计时器任务开始,启动一个新的 Executor Executors.newScheduledThreadPool(1) 和 scheduleAtFixedRate,初始延迟 = DELAY 并将周期设置为 1 天

于 2012-09-07T03:09:53.083 回答
0

Spring Quartz SchedulerFactoryBean

applicationcontext.xml

<!-- quartz -->

<bean id="emial" class="quartzcore.QuartzEmail"/>

    <bean id="myTask" class="quartzcore.MyTask" >
        <property name="edao" ref="empdao"/>
        <property name="email" ref="emial"/>        
    </bean>
<!--
    <bean id="schedulerTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
        <property name="targetObject" ref="myTask" />
        <property name="targetMethod" value="sayHello" />
    </bean>

<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="timerTask" ref="schedulerTask" />
    <property name="delay" value="5000" />
    <property name="period" value="5000" />
</bean>

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref local="timerTask" />
        </list>
    </property>
</bean>
 -->

    <bean name="quartzJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myTask" />
        <property name="targetMethod" value="sayHello" />
    </bean>

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> 
        <property name="jobDetail" ref="quartzJob" />
        <property name="repeatInterval" value="1000" />
        <property name="startDelay" value="1000" />
    </bean>

    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="quartzJob" />
        <property name="cronExpression" value="0/15 * * * * ?" /> 
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="quartzJob" />
            </list>
        </property> 
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

    </beans>

method of perticular class to be executed as configured in xml.

public class MyTask {

    private EmpDao edao;        
    private QuartzEmail email;    
    public static int size = 10;

    public void sayHello() {
        System.out.println("Hello !!! ");

        int currSize = 0;
        if ((currSize = edao.emp_count()) != size) {
            size = currSize;
            System.out.println("####### Records Changed in DB : "+size);
            email.sendMail();               
        }else {
            System.out.println("Records not added/removed. "+currSize);
        }
         (or base on dates)
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        String today = dateFormat.format(new Date());
        System.out.println("Current Day : "+today);

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date()); // Current date
        int numOfDays = cal.get(Calendar.DAY_OF_YEAR);
        System.out.println("Day of Year : "+numOfDays);

        Date edate = dateFormat.parse("27/10/2015");
        System.out.println("End Date : "+edate);
        cal.setTime(edate);  // Future Date
        int numOfpdays = cal.get(Calendar.DAY_OF_YEAR);
        final int fireTrigger = numOfpdays - numOfDays;
        System.out.println("Shedlue : "+fireTrigger);
        //if(fireTrigger > 0 && fireTrigger < 10 ){ //send a mail.. }   
    //...
}
  • If trigger time is very less it may not execute method completely, because before getting the response next trigger may raise.

  • request passing to DBS but not geeting back and executing if{} code. can any one can fix this.

using annotations.

于 2015-07-07T14:28:23.010 回答