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

  • Dynamic Scheduling

    Hi,

    I'm developing an auction engine, Each auction has a startTime and endTime, Now I wonder how can I make jobs and schedule them based on startTime and endTime dynamically?

    Any help is appreciated .




  • #2
    there are many options. If you need to schedule jobs and they have variable runtimes, you might submit them to an Executor instance using the appropriate type in that hierarchy (you might look at TimerTaskExecutor, which is backed by a TimerTask). You could use Quartz and launch jobs manually. You could simply have @Scheduled on a method that runs every 5 sconds or some other granularity and then consults a database or something for any scheduled jobs to run in the next 5 seconds. You could simply let it be somebody else's problem, and get more reliability by using a JMS broker: ActiveMQ, HornetQ, Tibco, Webmethods, Oracle, etc all have custom Message Header properties that you can use to schedule the delivery of a message. Simply send a message to the broker that contains enough context for the job to be completed, and then when its delivered, do the work that needs to be done because itll be delivered when the work needs to be done. The first two options are open source and easy to use. I'm sure I've missed a fair number of other options, but I HTH.

    Comment


    • #3
      @navid,

      I actually thought about it a couple of weeks ago "scheduling tasks dynamically" using Spring scheduler.

      So I came up with my own "On Demand Scheduler" http://github.com/anatoly-polinsky/on-demand-scheduler which is Spring based of course.

      The idea is you configure your task as a bean, and the task implements Schedulable interface, which means you can call task.schedule() at any given (run) time you want.

      You can either reuse the approach, or let me know, I can add a (startTime, EndTime) task there as well.

      /Anatoly

      Comment


      • #4
        @joshlong
        Using JMS is nice idea but I prefer to do it with quartz and spring.

        I've used scheduled annotation with a cron expression on another part which is working fairly, but its static,
        What I need is to create jobs and triggers on run-time and schedule them base on startTime.
        ,It seems It can be done using quartz and spring. could you give me an example how can I do it?

        Also in another situation I need to get scheduled jobs(before reaching start time) and reschedule them.

        @litius
        It looks that is what I want, could you please post an example ?
        I'm really stuck at this part of app.

        Thank you both.
        Last edited by navid; Sep 29th, 2010, 08:48 PM. Reason: syntax error

        Comment


        • #5
          navid, consider this idea. There's a scheduling utility called cron4j that consists of a single jar. The way it works is you create a class that will do the work:

          Code:
          public class AuctionTimer implements Runnable {
               public void run() {
                    // I'll get to this
               }
          }
          And then in your start up code you create a cron4j scheduler, pass it an instance of AuctionTimer and configure the frequency at which to run, say "* * * * *", which means every minute. In the run() menu you would check a table that consists of the StartTime and EndTime of the items that are for sale and update the status appropriately. Also, have a table, say AuctionTimer, with a field Status whose values could be "Running", "NotRunning", so that you can check before you begin running whether there is already a thread doing the work.

          You do not want to schedule a dedicate thread for every item, that's a big no no. With the approach that I described you have a single thread doing all the work.

          Comment


          • #6
            Originally posted by navid View Post
            @litius
            It looks that is what I want, could you please post an example ?
            I'm really stuck at this part of app.
            I just created an example for you under the same project [link in above post]. So the idea is simple:

            1. Create a Trigger that will only allow scheduling between start and end time with certain interval
            2. Create a Runnable task that can be later passed in to the Spring's TaskScheduler to schedule( task, trigger )

            So I created a trigger for you ( DurationTrigger ), and a "RunnableTaskFactoryBean".

            Here is a geeky use case. "Whenever Apple less expects it, start shipping Ubuntu DVDs to its headquarters in 5 secods, every 2 secods until 15 seconds from now":

            First let's define what to do:

            Code:
                    
            <bean id="shipUbuntuDvdTask"
                          class="org.gitpod.scheduler.task.factory.RunnableTaskFactoryBean">
            
                          <property name="targetObject" ref="mailer"/>
                          <property name="targetMethod" value="ship"/>
                          <property name="arguments">
                              <util:list>
                                  <value>Ubuntu DVD</value>
                                  <value>
                                         Apple Computer Inc 
                                         1 Infinite Loop
                                         Cupertino, CA 95014</value>
                              </util:list>
                          </property>
            </bean>
            Now, having the trigger in place, we can start it:

            Code:
                    long now = Calendar.getInstance().getTime().getTime();
            
                    // Set a trigger to start in 5 second, run every 2 seconds for 15 seconds:
                    Date startTime =  new Date( now + 5000 );
                    Date endTime =  new Date( now + 15000 );
                    long period = 2000;
            
                    Trigger trigger = new DurationTrigger( startTime, endTime, period );
            
                    taskScheduler.schedule( packageDeliveryTask, trigger );
            and then test it (all these are from "SchedulingRunnableTaskWithTriggerIntegrationTest" ):

            Code:
                @Test
                public void shouldNotDeliverAnythingYet()
            
                @Test
                public void shouldDeliverSomethingAlready() 
            
                @Test
                public void shouldBeDoneWithAllDeliveries()
            
                @Test
                public void shouldNotDeliverAnyMorePackages()
            Good luck,
            /Anatoly

            Comment


            • #7
              @sbyul thanks I may use this approach if I couldn't find a way to create triggers dynamically at runtime.

              @litius I'll give it a try and let you know how it goes...
              Is "scheduler" in this example an instance of SchedulerFactoryBean ?
              I Also need to pass a parameter(auctionID) to the job, how can I make it possible in this approach?

              Thank you so much for the example.

              Comment


              • #8
                Originally posted by navid View Post
                Is "scheduler" in this example an instance of SchedulerFactoryBean ?
                I Also need to pass a parameter(auctionID) to the job, how can I make it possible in this approach?
                @navid,

                a "taskScheduler" is a regular Spring's "<task:scheduler>" and "org.gitpod.scheduler.trigger.DurationTrigger" is a custom trigger. You can simply grab it form sources on github.

                If you want to pass "auctionID" as a runtime parameter, you can just rebuild the Runnable on the fly using the info from the (e.g. "shipUbuntuDvdTask") bean.

                Check the sources / tests for more examples. Let me know if you need a JAR, or you can just grab the sources, that is fine too.

                /Anatoly

                Comment


                • #9
                  @litius

                  I've used schedule-on-demand source code, now auction jobs and triggers are working smoothly.

                  Now I wonder how can I catch currently running Jobs and reschedule them ?

                  Thank for your suggested solution

                  Comment


                  • #10
                    @navid,

                    Glad it worked out for you, I just wrote it as a "curiosity project"

                    In order to better address your next use case, can you give me an example of when / why and what tasks exactly you want to be able to reschedule?

                    /Anatoly

                    Comment


                    • #11
                      @litius

                      Sure,
                      We have a timer which is 3 minutes long on each auction, and counts down.

                      Now I want to reset the timer when users bid on auction.

                      Comment


                      • #12
                        @navid,

                        Whenever you use Spring's TaskScheduler to schedule a task it follows a "java.util.concurrent.ScheduledExecutorService " convention and returns you a "java.util.concurrent.ScheduledFuture", which you can cancel as any java Future object.

                        So if you keep a reference to this ScheduledFuture within the Auction / Selling Item / etc.. you can cancel it at any time. And then reschedule the task in the same way you have scheduled it in the first place.

                        /Anatoly

                        Comment


                        • #13
                          Thanks Anatoly, I just followed your solutions and it worked for me.

                          Regards

                          Comment


                          • #14
                            Is "scheduler" in this example an instance of SchedulerFactoryBean ?
                            I Also need to pass a parameter(auctionID) to the job, how can I make it possible in this approach?

                            Comment


                            • #15
                              How to Create Job Schedule in Spring annotation

                              Hi,

                              I tried job schedule in spring with Quarts tools. but i want to know whether to create job schedule in spring annotation without using quartz and also should achieve in new Programmatic way apply time, schedule class and others, not for bean config...........................

                              Comment

                              Working...
                              X