Back Forum Reply New

quartz cronExpression from database??

Hi, I'm new on quartz, this tutorial sp...cheduling.html is really helpful for me, and it success implemented.

But now, I need to take the cron expression value that state the time from the database, how to do this???

Really thanks for the response.

Kahlil

Hello,

I got almost similar requirement, making the schedulers as DB driven, and handled it in the following way.

I created a new UI that takes start time amp; interval time and saves them in DB.  Then a method is called to refresh the given jobs/schedulers.  

Here is the snippet.Code:
try {
scheduler = (StdScheduler) context.getBean(schedulerVO.getSchedulerName());
triggerNames = new String[] {};

if (scheduler != null) {
try {
// throws SchedulerException
triggerNames = scheduler.getTriggerNames(quot;DEFAULTquot;);
triggerName = triggerNames.length gt; 0 ? triggerNames[0] : quot;quot;;
trigger = (CronTrigger) scheduler.getTrigger(triggerName, quot;DEFAULTquot;);
if (trigger != null) {

// throws ParseException
trigger.setCronExpression(getCronExpression(schedulerVO.getStartTime(), schedulerVO.getInterval()));

// throws SchedulerException
scheduler.rescheduleJob(triggerName, quot;DEFAULTquot;,trigger);
}
} catch (SchedulerException e) {
logger.error(e);
} catch (ParseException e) {
logger.error(e);
}
}
} catch (NoSuchBeanDefinitionException e) {
logger.error(e);
}
Computing the cronExpression with this method.Code:
private String getCronExpression(String startTime, String interval) {

String cronExpression = quot;quot;;

if (quot;0quot;.equals(startTime) || quot;0quot;.equals(interval)) {
// default trigger runs at 10AM amp; 10PM
cronExpression = quot;0 0 10/12 * * ?quot;;
} else {
cronExpression = quot;0 0 quot; + startTime + quot;/quot; + interval + quot; * * ?quot;;
}
return cronExpression;
}
As I need to run the job every day and not concerned about minutes the above approach worked for me.

Hope this gives an idea to you.

Regards
Siva Krishna.

I'm facing same issue. I have multiple schedules in DB on the basis of which I need to invoke scheduler.
Problem is for a single cron job I'm able to do that by
Code:

CronTriggerBean.javabut how to define for multiple scheduler? I can't modify context file.
Which class you had used to configure your cron job?

could you attach your code?, I still cannot get your point.

Thanks

I have written my own class ConfigurableCronTriggerBean.java with extends CronTriggerBean. I did this so that I can override the quot;cronExpressionquot; property by my set of values from DB.
I'm retrieving values from DB, looping thru list and creating new CronExpression in this class. Issue is the scheduler picks only last value. It ignores the other values.


Code:
lt;bean id=quot;cronJobTriggerquot; class=quot;org.rgc.springsample.scheduler.ConfigurableCronTriggerBeanquot; scope=quot;prototypequot;gt;
lt;property name=quot;jobDetailquot; ref=quot;fileTransferSchedulerRefquot; /gt;
lt;property name=quot;cronExpressionquot; value=quot;* * * ? * MON-FRIquot; /gt;
lt;/beangt;
lt;bean class=quot;org..scheduling.quartz.SchedulerFactoryBeanquot;gt;
lt;property name=quot;triggersquot;gt;
lt;listgt;
lt;ref bean=quot;cronJobTriggerquot; /gt;
lt;/listgt;
lt;/propertygt;
lt;/beangt;I have multiple schedules in DB.

Code:
SchId    DayOfWeek        StartTimes 1        mo,tu,we,th,fr    08:00, 22:00 2        mo,tu,we,th,fr    10:00,12:00,14:00,16:00,18:00
...
In context.xml, I have done settings like this

Code:
lt;bean id=quot;fileTransferSchedulerquot; class=quot;org.rgc.springsample.scheduler.FileTransferSchedulerquot; scope=quot;prototypequot;gt;
lt;property name=quot;fileTransferServicequot; ref=quot;fileTransferServicequot; /gt;
lt;/beangt;
lt;bean id=quot;fileTransferSchedulerRefquot; class=quot;org..scheduling.quartz.MethodInvokingJobDetailFactoryBeanquot; scope=quot;prototypequot;gt;
lt;property name=quot;targetObjectquot; ref=quot;fileTransferSchedulerquot; /gt;
lt;property name=quot;targetMethodquot; value=quot;executequot; /gt;
lt;property name=quot;argumentsquot; value=quot;truequot; /gt;
lt;property name=quot;concurrentquot; value=quot;falsequot; /gt;
lt;/beangt;Due to content lenght limit I had to split my reply in 3 parts. Please read next 3 replies

you need to define all your job on your context configuration, without forgetting append quot;schedulerNamequot; property.

try check out this thread showthread.php?t=40945amp;highlight=Multiple+Quartz+S  chedulerFactoryBean+instances

I think I did not explain properly. I have to retrieve schedules from database and set them in cronExpression to run multiple schedules. Problem is there could be N number of schedules defined in database. A separate cron job will retrieve these schedules from DB and for each schedule create a new CronExpression amp; execute them.

Hope I've explained my situation properly now.
¥
Back Forum Reply New