【发布时间】:2015-12-26 04:52:43
【问题描述】:
我使用 Grails 作业来启动使用 g.formatDate 的服务。直接从控制器使用服务时,我没有问题。当我使用作业中的服务时,出现以下错误:
ERROR listeners.ExceptionPrinterJobListener - Exception occurred in job: Grails Job
Message: java.lang.NullPointerException
Line | Method
->> 111 | execute in grails.plugins.quartz.GrailsJobFactory$GrailsJob
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 202 | run in org.quartz.core.JobRunShell
^ 573 | run . . in org.quartz.simpl.SimpleThreadPool$WorkerThread
Caused by NullPointerException: null
->> 245 | $tt__sendReportCompanyWeekly in Test.SendMailService$$EPPc274K
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 47 | doCall in Test.ReportCompanyWeeklyJob$_execute_closure1
| 31 | execute in Test.ReportCompanyWeeklyJob$$EPPc2HTU
| 104 | execute in grails.plugins.quartz.GrailsJobFactory$GrailsJob
| 202 | run . . in org.quartz.core.JobRunShell
^ 573 | run in org.quartz.simpl.SimpleThreadPool$WorkerThread
这是我的工作:
class ReportCompanyWeeklyJob {
SendMailService sendMailService
static triggers = {
cron name: 'scheduledReportCompanyWeeklyJob', cronExpression: "0 15 22 ? * *"
}
def execute() {
sendMailService.sendReportCompanyWeekly(company.id, user.id)
}
}
这是我的服务:
@Transactional
class SendMailService {
def gspTagLibraryLookup // being automatically injected by spring
def g
def sendReportCompanyWeekly(String companyId, String userId) {
g = gspTagLibraryLookup.lookupNamespaceDispatcher("g")
Date today = new Date()
Locale locale = // some locale
// Line 245
def test = g.formatDate(date: today, formatName: 'date.format.long.no.year', locale: locale)
}
}
编辑:我使用groovyPageRenderer.render(template: viewPathHTML, model: myModel) 在服务中呈现 GSP。
如何让 g.formatDate 从 Job 运行时工作?
【问题讨论】:
-
为什么要从服务中调用 gsp 标签?
-
因为我必须为电子邮件呈现 GSP 模板。
-
没有完全错误的答案。
-
我使用
groovyPageRenderer.render(template: viewPathHTML, model: myModel)在服务中呈现 GSP。你还有什么想法吗?