Announcement Announcement Module
Collapse
No announcement yet.
quartz triggers... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • quartz triggers...

    Hi!

    I am not sure it is the accurate place to put this question but I am trying to use some cron for this reason: my program lets a user filling a form to create a person. This person needs to be stored in a database (its properties: name, email, username, password...etc). But I would like this person to be created later for example the 10th of September 2006 at 8am or in 2 weeks exactly. Then I will need a cron to create automatically this person in the database at the chosen date. Trying to understand how to do that with Spring I read http://www.springframework.org/docs/...cheduling.html and I was obtaining some xml file and code like this:

    <bean name="createPersonJob" class="org.springframework.scheduling.quartz.JobDe tailBean">
    <property name="jobClass" value="org.alfresco.web.bean.EthicFlow_Jobs.Create PersonJob"/>
    <property name="jobDataAsMap">
    <map>
    <entry key="record" value=""/> <!--the name of the record where I have the new person properties: stay empty and when we launch a cron we will set a value to record-->
    </map>
    </property>
    </bean>


    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.Simpl eTriggerBean">
    <!-- see the example of method invoking job above -->
    <property name="jobDetail" ref="jobDetail"/>
    <!-- 10 seconds -->
    <property name="startDelay" value="10000"/>
    <!-- repeat every 50 seconds -->
    <property name="repeatInterval" value="50000"/>
    </bean>

    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronT riggerBean">
    <property name="jobDetail" ref="exampleJob"/>
    <!-- run every morning at 6 AM -->
    <property name="cronExpression" value="0 0 6 * * ?"/>
    </bean>

    <bean class="org.springframework.scheduling.quartz.Sched ulerFactoryBean">
    <property name="triggers">
    <list>
    <ref bean="cronTrigger"/>
    <ref bean="simpleTrigger"/>
    </list>
    </property>
    </bean>

    public class EthicFlow_CreatePersonJob extends QuartzJobBean
    {

    private String record;

    /**
    * Setter called after the CreateJob is instantiated
    * with the value from the JobDetailBean (5)
    */
    public void setRecord(String record) {
    this.record = record;
    }

    protected void executeInternal(JobExecutionContext ctx)

    throws JobExecutionException {

    // do the actual work

    }
    }

    Is it correct? But then how does this cron is launched? EthicFlow_CreatePersonJob.executeInternal()??
    Did I miss something?

    Thanks for your help

  • #2
    The Quartz Scheduler has a thread that wakes up when a task should be scheduled. This scheduling-thread send the task to the ThreadPool (containing worker threads) and one of the worker-threads in the threadpool is selected to execute the task. This worker-thread executes the task by calling the 'execute' method on it and this method eventually calls your 'executeInternal' method.

    Is this what you want to know?
    Last edited by Alarmnummer; Aug 4th, 2006, 04:46 AM.

    Comment


    • #3
      Thanks a lot for your help. I think I am confused with using Quartz alone or using Quartz from Spring (I did not understand how this is working to declare these beans and use them to launch the job). Without Spring I was trying the following:

      SchedulerFactory sf = new StdSchedulerFactory();
      Scheduler sched = sf.getScheduler();
      // define the job and tie it to our EthicFlow_CreatePersonJob class
      JobDetail job = new JobDetail("EthicFlow_CreatePersonJob", "group1", EthicFlow_CreatePersonJob.class);
      Date runTime = TriggerUtils.getEvenMinuteDate(new Date());
      //Trigger the job to run on the next round minute
      SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1", runTime);
      sched.scheduleJob(job, trigger);
      sched.start();
      Thread.sleep(90L * 1000L);
      sched.shutdown(true);

      And my class EthicFlow_CreatePersonJob has a method "execute" that is writing a message in my log. And it seems to work taking some time but if I am using this following date that does not seem to work:

      Date runTime = new Date(2006,7,4,10,15,0); //year, month, date, hrs, min, and sec
      Months are from 0 to 11 so I guess august is 7.
      I would need to use a Calendar instead of a Date but I do not know how to define this calendar neither how to use my SimpleTrigger constructor with a Calendar (I am using jdk 5) Do you know how could I do?

      Thanks

      Comment


      • #4
        Instead of creating dates yourself, you also can play with cron expressions.

        Code:
        <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
          <property name="jobDetail" ref="exampleJob"/>
        	<!-- run every morning at 6 AM -->
          <property name="cronExpression" value="0 0 6 * * ?"/>
        </bean>
        Check the documentation for more information.

        Comment


        • #5
          But is it not a problem if my data changes? I will never have the same date for launching a job...

          How does this cron expression is called? I do not understand...

          Thanks

          Comment


          • #6
            Scheduling individual jobs for each user to-be-created isn't the best way to deal with this. You'd be better off scheduling a single Quartz job that checks the database every N minutes to see if there are any users who should be created at that time.
            Quartz *could* be used the way you are trying here, and if you go that route you'll want to look into the JDBCJobStore otherwise your jobs are going to be lost when your server restarts.

            Comment

            Working...
            X