Announcement Announcement Module
Collapse
No announcement yet.
Scheduling Jobs with Quartz and Spring for stand alone java program Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scheduling Jobs with Quartz and Spring for stand alone java program

    I am newbi to Spring and Quartz..

    I developed a java Spring application (stand alone..no servers involved), which I need to start at 22.00 hrs everyday and stop at 6.00 in morning next day.

    I have seen this quartz job support for sping,

    I configured my Sping config file as below,

    Stupidly..I expected (with some magic) this program get starts on specified time ...unfortunately Thats doen't happen..

    So I am assuming some othe task/job has to kick off this spring schedular..am I wrong??

    My question 1: very dumbooo though

    How do these jobs get fired (for stand alone application)
    Do I need to wire this to any window's or unix schedulers??

    help pls..........




    ##############

    <!-- Quartz job scheduler --->

    <bean id="osndDatapump" class=" com.xchanging.repository.datapump.osnd.OSNDDataPum p"/>

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.Metho dInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="osndDatapump" />
    <property name="targetMethod" value="main" />
    <property name="concurrent" value="false" />
    </bean>

    <!--
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.Simpl eTriggerBean">
    <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="osndDatapump" />
    <!-- run every morning at 6 AM -->
    <property name="cronExpression" value="0 1 4 3 5 ?" />
    </bean>

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

    ################################################## #

  • #2
    Create a main class that loads the spring configuration and then your timers will get loaded. If you don't load the config (initialize the applicationcontext) nothing will happen.

    Comment


    • #3
      thats is the problem..I am scheduling main() method only...I want this application to kick off at 22.00 every night...

      I guess I need to do this using windows/unix schedular..right..

      Comment


      • #4
        If you want to trigger the execution of your application itself, you have to resort to OS mechanisms, indeed.
        Quartz does only help you, if your application is running and you want to start jobs from inside your application at given intervals/ times.

        Regards,
        Andreas

        Comment


        • #5
          You need to start your application... If you just put everything in a jar file, put it somewhere on your filesystem, nothing much is going to happen.

          You need a class which STARTS the whole thing so the scheduler gets kicked off.

          Code:
          ýmport org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          
          public final class SpringCommandLineStarter {
          
          	/**
          	 * Arguments passed in are assumed to be valid context locations
          	 * @param args
          	 */
              public static void main(final String[] args) {
                  if (args == null || args.length == 0) {
                      throw new RuntimeException("At least spring applicationcontext must be specified");
                  }
                  
                  Thread appThread = new Thread(new Runnable() {
                          public void run() {
                              ApplicationContext appCtx = new ClassPathXmlApplicationContext(args);               
                              while (true) {// run forever
                              }
                          }
                      }
                  );
                  appThread.setDaemon(true);
                  appThread.start();
              }
          }
          Start the class from the command line, or startup script whatever you want.

          java -cp spring.jar com.package.SpringCommandLineStarter applicationContext.xml

          Everything is untested and just from the top of my head. But it should give you a general idea.

          Comment


          • #6
            Thanks for suggestion. I just try to execute the code you have given above but unsuccessful.

            Only difference I made to one mentioned above was, I try to run this application as java application.

            Can you please point out to a small test program which indclude a config file.
            May be I understand better by seeing that....

            Comment


            • #7
              What is the difference between my example and your case? You should run it as a java application else it wouldn't start?! Also it was from the top of my head so no garantuees . The applicationcontext.xml should be on your classpath, our contain the whole path to the xml. Else it will not load.

              Small test programm. I guess look at the sample applications which come shipped with spring. Look at the imagedb that one contains a example of a standalone application, although run thru ant but you should be able to figure that out.

              Also check the spring reference guid, mainly the first chapters those also include examples of how to load a applicationcontext.

              Comment


              • #8
                Hi mate,

                It is working fine...... That great...thanks a lot.

                my requirement is, scheduler has to run the job during week days (Mon -Fri) between 22.00 till 06.00 next day with a time delay of 15 mins .

                I think cronTrigger is better for this. I configured the trigger properties like one I copied below. Unfortunately it is not working...

                Can anyone help me with correct trigger properties or with correct expression which do all this in one cron expression...


                ************************************************** ***


                <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronT riggerBean">
                <property name="jobDetail" ref="osndDatapump" />
                <!--run every morning at 10. 15 AM -->
                <property name="cronExpression" value="0 15 10 ? * MON-FRI" />
                <!-- start time -->
                <property name="timeOut" value="5000" />
                <property name="repeatInterval" value="10000" />
                <property name="startDelay" value="5000" />


                </bean>

                Comment

                Working...
                X