SpaceNet
SpaceNet

Reputation: 193

Grails Quartz - How to conbine similar two jobs into one job?

I have to similar two jobs and want to conbine the two jobs into one job.

How can I conbine AJob and BJob into a new Job?

In other words, I want to refactor AJob.groovy and BJob.groovy into one class NewJob.groovy.

AJob.groovy

class AJob {
    def testService
    static triggers = {
      cron name: 'myTrigger', cronExpression: "0 0 6 * * ?"
    }
    def group = "MyGroup"
    def description = "A job with hello"
    def execute() {
        testService.hello("hello")
    }
}

BJob.groovy

class BJob {
    def testService
    static triggers = {
      cron name: 'myTrigger', cronExpression: "0 0 7 * * ?"
    }
    def group = "MyGroup"
    def description = "B job with goodbye"
    def execute() {
        testService.hello("goodbye")
    }
}

TestService.groovy

class TwitterService {
    def hello(message){
        print message
    }
}

Upvotes: 0

Views: 97

Answers (1)

Nathan Dunn
Nathan Dunn

Reputation: 447

I would assume that the only reason to do it this way (versus having Cron call both at the same time) would be to somehow synchronize them, in order to guarantee A is called after B. The most obvious solution would be as alluded to above:

class NewJob{
    def execute() {
        testService.hello("hello")
        testService.hello("goodbye")
    }
}

If for some reason these are asynchronous calls (and you still want to synchronize), you would have to use the callback on the thread or create separate threads and somehow inject the dependency. The only reason not to do this, would be if you somehow needed to record the dependent injection of the quartz job. More details?

Upvotes: 1

Related Questions