简体   繁体   中英

grails call taglib from quartz job

I have a quartz job that calls the joda taglib (but could be any other).

I do it like this:

def joda = grailsApplication.mainContext.getBean('grails.plugin.jodatime.taglib.FormattingTagLib')

def formatDate = joda.format(value:event.startDate, style:"SS", locale:user.locale, zone:user.timeZone)

But I am gettng:

org.quartz.JobExecutionException: 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. [See nested exception: 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.]

Which absolutely makes sense because a quartz job is not request bound as it is triggered by time and not by request.

I searched around but could not find a propper sollution. Can somebdy give me a hint?

The problem is joda.format() attempts to get the current request, which as you noted, is not possible.

But more fundamentally, taglibs are for GSPs. Calling them outside of GSPs is not a good practice because it is outside the use case of taglibs. Fortunately, the solution is simple: use the joda time formatter directly:

import org.joda.time.format.DateTimeFormat

def formatter = DateTimeFormat.forStyle('SS').withLocale(user.locale).withZone(user.timeZone)
def formatDate = formatter.print(event.startDate)

https://github.com/gpc/joda-time/blob/grails2/grails-app/taglib/grails/plugin/jodatime/taglib/FormattingTagLib.groovy

The answer to how to call taglib from quartz job is not to do it at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM