Kacu
Kacu

Reputation: 438

grails service with quartz

I wrote an app in Grails but I have some problems with quartz. I wanna fetch User from DB and after that fetch his servers as well. If there will be any server I wanna check PING command on each one, but I get a message like this:

" [quartzScheduler_Worker-1] ERROR listeners.ExceptionPrinterJobListener - Exception occurred in job: GRAILS_JOBS. Message: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. Line | Method ->> 96 | execute in grails.plugins.quartz.GrailsJobFactory$GrailsJob - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 216 | run in org.quartz.core.JobRunShell ^ 549 | run . . in org.quartz.simpl.SimpleThreadPool$WorkerThread
Caused by IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. ->> 131 | currentRequestAttributes in org.springframework.web.context.request.RequestContextHolder"

This is my code:

 def execute() {
     pingService.checkPing()
 }



 def checkPing = {
     User user = User.findByLogin(session.user.login) //get user
     def hostsToPing = importFromDB()
     if (!hostsToPing.isEmpty()) {
         hostsToPing.each {host ->
             doPing(host)
         }
     } else {
         //something else
     }
 }

 def importFromDB = {
     User user = User.findByLogin(session.user.login)
     def hostsList = Host.findAllByUser(user)
     hostsList
 }

 def doPing(Host host) {
     println "InetAdress: " + InetAddress.getByName(host.hostAdress)
     println "InetAdress is Rea: " + InetAddress.getLocalHost().isReachable(1000)
 }

There isn't this problem when is something like this:

def doPing(Host host) {
    println "InetAdress: " + InetAddress.getByName("www.google.com")
    println "InetAdress is Reachable : " + InetAddress.getLocalHost().isReachable(1000)
}

Does anyone know whats wrong?

Upvotes: 1

Views: 1099

Answers (1)

Victor Sergienko
Victor Sergienko

Reputation: 13475

It's because you're referring to session.user.login. There is no session in a Job, period. Imagine the job is started when no user is logged in - WHAT user are you referring then?

So either check every user in User.list() or make a singleton bean with a users queue.

Upvotes: 1

Related Questions