aspose file tools*
The moose likes Spring and the fly likes Spring quatrz dynamic cron expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring quatrz dynamic cron expression" Watch "Spring quatrz dynamic cron expression" New topic
Author

Spring quatrz dynamic cron expression

francis varkey
Ranch Hand

Joined: Sep 13, 2005
Posts: 169
Hi ,
I am trying to implement a scheduler using quartz in spring3. The requirement is need to take value of cron expression should read from database. While trying this I am getting following exception.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cronTrigger' defined in class path resource [Spring-Quartz.xml]: Cannot resolve reference to bean 'constructCronExpression' while setting bean property 'cronExpression'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'constructCronExpression' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)


Code.

1) Spring-Quartz.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="runMeTask" class="test.RunMeTask" />
<!-- <bean id="constructCronExpression" class="test.CustomCronExpression" /> -->

<!-- Spring Quartz -->
<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean">

<property name="jobClass" value="test.RunMeJob" />

<property name="jobDataAsMap">
<map>
<entry key="runMeTask" value-ref="runMeTask" />
</map>
</property>

</bean>

<!--
<bean id="runMeJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="runMeTask" />
<property name="targetMethod" value="printMe" />
</bean>
-->

<!-- Simple Trigger, run every 5 seconds -->
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">

<property name="jobDetail" ref="runMeJob" />
<property name="repeatInterval" value="5000" />
<property name="startDelay" value="1000" />

</bean>

<!-- Cron Trigger, run every 5 seconds -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail" ref="runMeJob" />
<!-- <property name="cronExpression" value="0/5 * * * * ?" /> -->
<property name="cronExpression" ref="constructCronExpression"/>

</bean>

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

<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>

</beans>

2)RunMeTask.java
package test;

public class RunMeTask {
public void printMe() {
System.out.println("Spring 3 + Quartz 1.8.6 ~");
}
}

3)RunMeJob.java
package test;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class RunMeJob extends QuartzJobBean {
private RunMeTask runMeTask;

public void setRunMeTask(RunMeTask runMeTask) {
this.runMeTask = runMeTask;
}

protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {

runMeTask.printMe();

}
}

4)App.java
package test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
public static void main( String[] args ) throws Exception
{
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
}
}

5)CustomCronExpression.java
package test;
import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration
public class CustomCronExpression{

@Bean
public String constructCronExpression() {
String cronExpression = "0/5 * * * * ?"; //Contruct Cron Expression from the request
return cronExpression ;
}

}





Thank You



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring quatrz dynamic cron expression