Page 1 of 1

Stack ScheduledTask Help

Posted: Mon Feb 07, 2011 4:39 pm
by tlmaravilla
We are still on Stack 2.2 RC5 and are using tomcat locally and deploying to WAS. At this point, we are using the stack ScheduledTask (due to issues with Spring/Quartz within WAS), our serviceBeanContext.xml:
...
<bean id="activationSvc" class="org.lds.ics.mtm.firewall.service.ActivationServiceImpl" scope="prototype">
</bean>
...
<util:list id="scheduledTasks">
<bean class="org.lds.stack.spring.scheduling.ScheduledTask">
<property name="period" value="10000" />
<property name="delay" value="10000" />
<property name="runnable">
<bean class="org.springframework.scheduling.support.MethodInvokingRunnable">
<property name="targetObject" ref="activationSvc" />
<property name="targetMethod" value="checkGpmLoadDate" />
</bean>
</property>
</bean>
</util:list>


Initially we thought that a simple delay would work for our needs, but now we are needing to run tasks at certain times, such as 23:50, such as a GPM DB daily refresh check, so what would be easier:

- Trying to get Quartz to run within WAS
- Have Quartz run as a stand-alone app on linux, which calls our methods within WAS
- Upgrade to stack 3.1 and replace WAS with Tomcat in prod

Is there a solution within stack ScheduledTask that I am missing?

Thoughts?

Posted: Mon Feb 07, 2011 5:02 pm
by YoungstromMJ
Yes there is a solution to this. You can do the following:

1. Extend org.lds.stack.spring.scheduling.ScheduledTask
2. Override the getDelay() method to dynamically determine when you want it to start. Use logic such as:
Time at 23:50 today - now.
This will tell the scheduler to delay running until 23:50 tonight.
3. Set the period to 24 hours.
4. Set the "fixedRate" property in ScheduledTask to "true".

That should allow you to create a task that runs every day at 23:50.

Mike

Posted: Tue Feb 08, 2011 3:07 pm
by tlmaravilla
Mike,

In your directions:
2. Override the getDelay() method to dynamically determine when you want it to start. Use logic such as:
Time at 23:50 today - now.
I am assuming that I need to override the getDelay(), then call super.setDelay(long delay), right?

Posted: Tue Feb 08, 2011 3:18 pm
by YoungstromMJ
Probably the more appropriate thing to do would be to Override the constructor and call setDelay and setFixedRate from there.

Posted: Tue Feb 08, 2011 3:32 pm
by tlmaravilla
After talking with Mike, he put together this class:

public class SpecificTimeScheduledTask extends ScheduledTask {

private static Logger logger = Logger.getLogger(SpecificTimeScheduledTask.class);

@PostConstruct
public void init() {
super.setDelay(calculateDelay());
super.setFixedRate(true);
super.setPeriod(86400000);
}

private long calculateDelay() {
Calendar loadDateCal = new GregorianCalendar();
loadDateCal.setTime(new Date());
loadDateCal.set(Calendar.HOUR_OF_DAY, 23);
loadDateCal.set(Calendar.MINUTE, 59);

long currTm = System.currentTimeMillis();
long delayTm = loadDateCal.getTimeInMillis() - currTm;

logger.info("loadDateCal time (ms): " + loadDateCal.getTimeInMillis() + ", currTm: : " + currTm + ", delayTm: "
+ delayTm);
return delayTm;
}
}

serivceBeanContext.xml:
<util:list id="scheduledTasks">
<bean class="org.lds.ics.mtm.firewall.service.SpecificTimeScheduledTask">
<property name="runnable">
<bean class="org.springframework.scheduling.support.MethodInvokingRunnable">
<property name="targetObject" ref="activationSvc" />
<property name="targetMethod" value="checkGpmLoadDate" />
</bean>
</property>
</bean>
</util:list>

Thanks Mike!