Question

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.

Was it helpful?

Solution

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

Detailed documentation is here

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top