views:

631

answers:

2

Hi! good people. i'm trying to use quartz with spring in a very simple project managed by maven.So in the module in which the mycron job class is i included a java main class to it just to see the job output some text and new date. here is my spring config:

<!--Scheduling-->
<!--Job-->
    <bean id="projUpdater" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.myproject.utilscheduling.quartz.ProjUpdaterCronImpl" />
    </bean>
<!---End of Jobs-->
<!--Triggers-->
    <bean id="regularUpdateTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="projUpdater"/>
        <property name="cronExpression" value="30 1 * * * ?"/>
    </bean>
<!--End ofTriggers-->
<!--Scheduler Factory-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="regularUpdateTrigger"/>
        </list>
    </property>
</bean>
<!--End of Scheduler Factory-->
<!--End of Scheduling-->

and here is the job class

public class ProjUpdaterCronImpl extends QuartzJobBean {

public ProjUpdaterCronImpl() {
}


protected void executeInternal (JobExecutionContext ctx) throws JobExecutionException {
 System.out.println("[JOB] " + new Date() + "hello");
}

}

here is main class

public class NewMain {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
    System.out.println("starting job");
 }

}

so in my understanding the job will be started after 1mn30s and posted on the console.I'm wrong.i faced couple or errors that i solve so i can safely assume that there is no error in in spring configuration file since there is none when building and running.So what did i do wrong or what did i forget to do?

second concern since i'm forcing myself to go the test driven way how will i possibly test a the cron job class? thanks for reading

A: 

Try this for your job bean.

<bean id="projUpdater" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="registeredObject" />
        <property name="targetMethod" value="methodNameInObject" />
    </bean>

Where your registeredObject is a bean registered in the spring config somewhere. And the method is a method that exists in that object.

Zoidberg
i'll try your suggestion i've been seeing this form on the net a lot .by the way how to get what i did work actually unless i won't know how to use it that way.i mean for a beginner like me it's key to know what i did wrong.thanks
black sensei
+1  A: 

Your cron expression

<property name="cronExpression" value="30 1 * * * ?"/>

actually configures the trigger to fire once every hour at 1 minute 30 seconds after the hour.

Zoidberg's suggestion to use MethodInvokingJobDetailFactoryBean allows you to code your cron job as a POJO, making it easy to test.

Jim Huang
mmmh that's tricky. 60 0 0 * * ? will do every minute?
black sensei
No, "0 * * * * ?" fires every minute. Check out the documentation at http://www.opensymphony.com/quartz/wikidocs/CronTriggers%20Tutorial.html
Jim Huang
thanks dude i have to really sit down and read this stuffs thanks man
black sensei