Search code examples
grailsquartz-schedulergrails-plugin

quartz grails multi-entity environment


I have a web-app (grails 2.3.5, quartz plugin) with multiple users. Now I want that my users can schedule jobs using quartz. I wonder what the best approach is to separate Triggers from one user from the triggers from another user.

e.g. provide a list of all scheduled tasks for a given user

Are there any recommendations how to make this differentiation?


Solution

  • Implementing some scheme for naming your triggers would likely be the best approach here. That way you can query the triggers for a job and filter them by some type of matching pattern.

    It's really up to you to decide how you want to manage the visibility and management of the triggers. Using the trigger name seems to be the most logical approach in my own opinion.

    Alternatively, you could build a framework (e.g. Domain model) that relates the triggers to a user.

    Update

    In light of the content of your comment I'd like to offer you a glimpse into how you can dynamically add a trigger to an existing job. This is only an example to help you get further down the path of accomplishing the goal you have.

    import org.quartz.CronScheduleBuilder
    import org.quartz.Trigger
    import org.quartz.TriggerBuilder
    ...
    def jobManagerService
    
    String cronExpression = ... // whatever the expression is
    
    Trigger trigger = TriggerBuilder.newTrigger()
        .withIdentity("UniqueNameOfYourTriggerHere-UserId")
        .withPriority(6)
        .forJob("com.example.package.JobClassNameJob", "groupName")
        .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
        .build()
    
    // if you need job parameters
    trigger.jobDataMap.putAll([param1: 'example'])
    
    jobManagerService.getQuartzScheduler().scheduleJob(trigger)