register
other register

Tuesday, July 21, 2009

Get Session in Grails Service

In Grails, session variable is not available in service. But

import org.springframework.web.context.request.RequestContextHolder

def user = RequestContextHolder.currentRequestAttributes().getSession()?.user


Note: the RequestContextHolder is genuinely a normal web request. If the request is a scheduled job (using Quartz plutin), not a web request, then there will be exceptions like below:

=============================
2009-07-21 00:00:00,269 INFO [core.JobRunShell] Job GRAILS_JOBS.ExpireJob threw a JobExecutionException:
org.quartz.JobExecutionException: 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.Ill
egalStateException: 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 st
ill receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use Req
uestContextListener or RequestContextFilter to expose the current request.]
at org.codehaus.groovy.grails.plugins.quartz.GrailsJobFactory$GrailsTaskClassJob.execute(GrailsJobFactory.java:81)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
Caused by: 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 wit
hin a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortle
t: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:12
2)
at org.springframework.web.context.request.RequestContextHolder$currentRequestAttributes.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:43)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at EmailerService.sendEmails(EmailerService.groovy:64)
at EmailerService$sendEmails$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:43)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
at ExpireJob$_execute_closure1.doCall(ExpireJob.groovy:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1061)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:910)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:892)
at groovy.lang.Closure.call(Closure.java:279)
at groovy.lang.Closure.call(Closure.java:292)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1165)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1141)
at org.codehaus.groovy.runtime.dgm$87.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSi
te.java:270)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
============================


If you are logging this scheduled job, then do:

try {
def user = RequestContextHolder.currentRequestAttributes().getSession()?.user
if (user != null) {
log.info("Email: ${mail.dump()}, action: sendEmails by ${user} on ${new Date()}")
}
else {
log.info("Email: ${mail.dump()}, action: sendEmails by system on ${new Date()}")
}
}
catch (java.lang.IllegalStateException e) {
log.info("Email: ${mail.dump()}, action: sendEmails by system on ${new Date()}")
}

No comments: