Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Create Quartz Jobs as Prototypes

  1. #1
    Join Date
    Feb 2008
    Posts
    9

    Default Create Quartz Jobs as Prototypes

    Hello.

    I would like to configure Spring so that it creates a new Quartz job instance every time a trigger fires (i.e. prototype instead of singleton). I'm using MethodInvokingJobDetailFactoryBean, CronTriggerBean, and SchedulerFactoryBean. I've looked in the forum, the API, and the code, but I couldn't find an example that demonstrates this. Does anyone know how to get Spring do this, or if it's possible?

    Thank you!

  2. #2
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    What is the reason you want this behavior?

  3. #3
    Join Date
    Feb 2008
    Posts
    9

    Default Create Quartz Jobs as Prototypes

    I want to make sure that my jobs are thread-safe, as Quartz ensures, and set Spring so that the second job will not have to wait until first job has finished, as does the "concurrent" property of MethodInvokingJobDetailFactoryBean.

  4. #4
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by rpepersack View Post
    I want to make sure that my jobs are thread-safe, as Quartz ensures, and set Spring so that the second job will not have to wait until first job has finished, as does the "concurrent" property of MethodInvokingJobDetailFactoryBean.
    I don't understand what you want.

    So by creating multiple jobs (each job executed by a different thread) you want to make it threadsafe? (isolation/confinement is a very valuable technique for concurrency control). But do you jobs have state?

    Personally I prefer making quartz (or any other threading engine) as stupid as possible. If I need to maintain some kind of context(state) for a Job, I make it part of the call.

    Code:
    class FireService{
       void fireThemAll(){
          FireThemAllContext context = new FireThemAllContext();
          ... now pass the context to the locations needed (could also use a threadlocal for this but making it explicit is less vague).
       }
    }
    And now the FireService.fireThemAll Service can be called by the same instance of the QuartzJob (MethodInvoking.... for example) by multiple Quartz-threads.
    Last edited by Alarmnummer; Mar 3rd, 2008 at 09:36 AM.

  5. #5
    Join Date
    Feb 2008
    Posts
    9

    Default

    Yes, my jobs have state. Some also take a long time to finish. That's why I want each job to have its own instance. I'm new to Spring, so please bear with me. Are you suggesting that I should create a factory class that creates a new Job instance?

  6. #6
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by rpepersack View Post
    Yes, my jobs have state. Some also take a long time to finish. That's why I want each job to have its own instance. I'm new to Spring, so please bear with me. Are you suggesting that I should create a factory class that creates a new Job instance?
    Nope.. suggest that you make the calling context (a location where you can store state) part of your design. See the example I have provided.

    If you make it part of your design, you don't need to add this logic to the QuartzJob..

    In essence you are moving the responsibility of creating 'a context per thread' from quarts to your service. Maybe the name context is a little bit confusing; maybe it would be better to call it job (the job contains the context of the call).

    Code:
    class FireService{
        void fireAll(){
            FireAllJob job = new FireAllJob();
            job.setStartTime(new Date()); //some data specific to this call
            ... now do calls and you can pass the job to these calls.
        }
    }

  7. #7
    Join Date
    Feb 2008
    Posts
    9

    Default

    So, would I configure my Spring XML file so that all of my job details would call FireService, which would, in turn, call FireAllJob? Would FireAllJob create instances of my jobs, or is it just for storing state?

  8. #8
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by rpepersack View Post
    So, would I configure my Spring XML file so that all of my job details would call FireService
    Correct.

    You just need to hook up a Quartz scheduler (or any other mechanism) to that fire method of the FireService.

    which would, in turn, call FireAllJob?
    Correct.

    Would FireAllJob create instances of my jobs, or is it just for storing state?
    The FireAllJob is the Job you are executing. So I don't think you need to create additional jobs (unless you are splitting up the FireAllJob in smaller Jobs.. but that depends on the situation again).

  9. #9
    Join Date
    Feb 2008
    Posts
    9

    Default

    I assume that the "service class" would be a singleton, and the jobs would be prototypes. If my "service" class acts as a "job instance creator", then how do I tell it which job to create? Or, does each "job prototype" have its own "singleton creator"?

  10. #10
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by rpepersack View Post
    I assume that the "service class" would be a singleton, and the jobs would be prototypes.
    Correct

    If my "service" class acts as a "job instance creator", then how do I tell it which job to create? Or, does each "job prototype" have its own "singleton creator"?
    That depends on your requirements/taste. I don't know anything about the requirements to help you in a good direction. I don't know how complex the tasks are.. if logic should be added to the job... or if the job should be a 'state container'.. it all depends. There are many different ways to design it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •