2

We are using ScheduledTimerTask to manage the jobs(automatic) in our application.We are using the following code:

<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>

By using the above code runMethod is working fine for every one minute.But we would like to change the execution based on server time.For example,If time in server is 6 PM then we need to invoke the function.How to achieve it?

Help would be appreciated.

Kiran
  • 20,167
  • 11
  • 67
  • 99

3 Answers3

1

If you have to use only ScheduledTimerTask, then invoke the timer task once a minute. Now from within the timer task, check if the time is 6AM and run the necessary functionality. Else return.

Con: This task which is NO-OP will get executed for every minute(except for once it has to do some processing.

OR

Invoke the TimeTask only once from the spring. When the task is executed, you can get the current time and can compute the difference between current time and time to execute the actual task. (let this difference be called DELAY).

Now from with in this timer task, start a new Executor Executors.newScheduledThreadPool(1) and scheduleAtFixedRate with intial delay = DELAY and set the period as 1 day

Thiyanesh
  • 519
  • 1
  • 6
  • 14
1

You can use task trigger of Spring to invoke at a certian time period, you can use CronTrigger.

Detailed documentation is here

mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • Thanks for quick reply.But can we do the sam thing for ScheduledTimerTask.Because we need to change the code if we go for cronTrigger. – Kiran Dec 01 '11 at 07:03
  • You can make it trigger once a day by removing the delay and giving period of 24 hours, from then it will invoke everyday once, but exact timing I dont think is possible with TimerTask. – mprabhat Dec 01 '11 at 07:16
  • Also to note SpringTimerTask will use java.util.TimerTask and we dont have any say on the invocation time for that. Hence I fear we cannot specify exact timing in spring timer task too. – mprabhat Dec 01 '11 at 07:21
  • ok.Tahnks a lot,but which one we need to use to trigger instead of delay and period. – Kiran Dec 01 '11 at 07:29
  • Use CronTrigger it will give you exactly what you want.Some examples: 1. http://www.roseindia.net/quartz/example-of-cron-trigger.shtml 2. http://www.mkyong.com/spring/spring-quartz-scheduler-example/ – mprabhat Dec 01 '11 at 07:31
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.

Community
  • 1
  • 1
Yash
  • 9,250
  • 2
  • 69
  • 74