在quartz工作中使用应用程序上下文

时间:2011-12-28 12:41:21

标签: grails

我正在尝试阅读作业中的消息包(Grails 2.0,Quartz-0.4.2):

class Job1Job {
 def timeout = 5000l // execute job once in 5 seconds
 def grailsApplication

 def execute() {
   def ctx = grailsApplication.mainContext
   def bean = ctx.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
   println bean.message(code:'default.home.label')
 }
}

得到错误:

Error 2011-12-28 14:30:14,021 [quartzScheduler_Worker-5] ERROR listeners.ExceptionPrinterJobListener  - Exception occured in job: GRAILS_JOBS.testappcontext.Job1Job
Message: 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.

代码在控制器和服务中完美运行。我做错了什么? 我怀疑这个代码在grails 1.3.7中完美运行

1 个答案:

答案 0 :(得分:8)

你需要很长时间 - 只需使用taglib使用的内容,messageSource bean:

class Job1Job {
   long timeout = 5000 // execute job once in 5 seconds
   def messageSource

   void execute() {
      println messageSource.getMessage('default.home.label', null, Locale.default)
   }
}