tags:

views:

73

answers:

2

I'm developing a football manager website, but I can't figure out how to use properly the background-thread plugin and Quartz plugin (there is no much docs). My problem is.. I have a Controller of a Match class, with a function that I need to start at some time. With quartz I have tried to create a job but then I cannot call directly the function (and how many job should I create if I have more match to start?) or I don't know how to do it, and with background-thread I create the service class but then I have no idea how to implement it. Someone Can help me with this? Thanks

EDIT:

Solution in this post:

http://stackoverflow.com/questions/4088259/grails-thread-hibernateexception-no-hibernate-session-bound-to-thread

+1  A: 

Some times just need to keep tryng :) With quartz I do like this: class TestJob {

MatchController match = new MatchController()

static triggers = {
    /*
     * firstThread runs every minute from 8 to 9:59 on wednsday
     */
    cron name: 'firstThread', cronExpression: "0 0/1 8-9 ? * WED"
    cron name: 'secondThread', cronExpression: "0 0/5 8-9 ? * WED"
    cron name: 'thirdThread', cronExpression: "0 0/10 8-9 ? * WED"
    cron name: 'forthThread', cronExpression: "0 0/15 8-9 ? * WED"
 }

def group = "MyGroup"

def execute() {
    /*
     *  execute task
     *  call playMatch from match controller
     */
    println "run thread: " + String.format('%tH:%<tM:%<tS.%<tL',System.currentTimeMillis())
    match.playMatch()
}

}

Seems to work. But still wonder I to use background-thread

Ben
+1  A: 

Background-thread was designed as a simple plugin to just do as the name suggests - run some code in a background thread.

It doesn't give you a handle to the background thread (that I can recall) so it's just a 'set and forget' type of thing.

Quartz is the right choice for what you want to do. I probably wouldn't have the Job calling the controller though. The logic of 'playing a match' sounds like it should be in a service. You can get your services injected into Job classes the same as you can in controllers.

cheers

Lee

leebutts
you're right with service is much better, I didn't think about it, especially when you need to call a function in different controllers.
Ben
The service probably shouldn't be calling functions on the controller. Controllers are meant to handle different actions from incoming http requests. If your service "needs" to call a controller action, you may want to look into redesigning that code. Perhaps pull the code out of the controller and into a new service; then both the controller and any other services can call it.
Matt Lachman
now what I'm doing is for example:League class call on save, a LeagueService with a method that generates all the leauges I need.Am I doing it right?
Ben
@Ben Yes u are doing right.
fabien7474
This is my idea how to use quartzhttp://stackoverflow.com/questions/4042492/grails-quartz-thread, but I don't know how to create thread in the service, keep getting ExceptionPrinterJobListener and Could not find matching constructor for: java.lang.Thread(fproject.Match)
Ben