Grails没有找到线程绑定的请求 [英] Grails No thread-bound request found

查看:262
本文介绍了Grails没有找到线程绑定的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Grails服务闭包来执行异步HTTP请求到另一个Web应用程序。使用 Grails 1.3.9 ,我在这里非常有限。代码在一个线程内运行。它是从一个计划任务和几个函数调用中调用的。我得到的错误如下:



错误:

  java。 lang.IllegalStateException:找不到线程绑定请求:您是否在实际Web请求之外引用请求属性,或在原始接收线程之外处理请求?如果实际上在Web请求中运行并仍然收到此消息,则代码可能在DispatcherServlet / DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter来公开当前请求。 

CODE:

  Thread.start {

def http = new HTTPBuilder(grailsApplication?.config?.smsConfig?.smsServiceAddress);

尝试{
http.request(POST){

body = bodyRequest;
requestContentType = ContentType.JSON

response.success = {resp - >
printlnSUCCESS!$ {resp.status}

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

if(user!= null){
if(termin){
termin.withTransaction {

try {
//做所需动作

} catch( e){
println(e);






response.failure = {resp - >
printlnFAILURE请求失败,状态为$ {resp.status},响应主体为[$ {resp.entity.content.text}]
System.out <<


$ b $ //在这里捕获错误
catch(e){
log.errorError:+ e;




$ b我已经尝试在web.xml中添加这个选项

 < listener> 
< listener-class> org.springframework.web.context.request.RequestContextListener< / listener-class>





以及WebXmlConfig。 groovy

  listener.add = true 
listener.classNames = [org.springframework.web.context.request。 RequestContextListener]

但都没有帮助

解决方案

这是没有很好的文档记录,或者你可以给你发送螺旋: 这个:

  / * 
*修复找不到线程绑定请求:您是指请求属性
* /
def webRequest = RequestContextHolder.getRequestAttributes()
if(!webRequest){
def servletContext = ServletContextHolder.getServletContext()
def applicationContext = WebApplicationContextUtils 。 getRequiredWebApplicationContext(servletContext)
webRequest = grails.util.GrailsWebMockUtil.bindMockWebRequest(applicationContext)
}

您需要在build.gradle中进行spring测试

  compile'org.springframework:spring-test:2.5' 

有一个grails 2分支,所以如果它是grails 2, / p>

I am using a Grails service closure to perform a async HTTP request to another web application. Using Grails 1.3.9, I am very limited here. The code runs inside a thread. It is called from a scheduled job and from several function calls. The error I get is below:

ERROR:

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.

CODE:

    Thread.start {

        def http = new HTTPBuilder(grailsApplication?.config?.smsConfig?.smsServiceAddress);

        try {
            http.request(POST) {

                body = bodyRequest;
                requestContentType = ContentType.JSON

                response.success = { resp ->
                    println "SUCCESS! ${resp.status}"

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

                    if (user != null) {
                        if (termin) {
                            termin.withTransaction {

                                try {
                                    // do desired action

                                } catch (e) {
                                    println(e);
                                }

                            }
                        }

                    }
                }
                response.failure = { resp ->
                    println "FAILURE request failed with status ${resp.status}, response body was [${resp.entity.content.text}]"
                    System.out << resp
                }
            }
        }
        //here error is caught
        catch (e) {
            log.error "Error: " + e;
        }
    }

I have tried adding this options to web.xml

    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

and also this to WebXmlConfig.groovy

    listener.add = true
listener.classNames = ["org.springframework.web.context.request.RequestContextListener"]

but both did not help

解决方案

This is something that isn't well documented or can you send you spirals:

Try this:

/*
* Fix for No thread-bound request found: Are you referring to request attributes
*/
def webRequest = RequestContextHolder.getRequestAttributes()
if(!webRequest) {
    def servletContext  = ServletContextHolder.getServletContext()
    def applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
    webRequest =  grails.util.GrailsWebMockUtil.bindMockWebRequest(applicationContext)
}

You will need spring test in your build.gradle

  compile 'org.springframework:spring-test:2.5'

There is a grails 2 branch so follow the same class in the other branch if it is grails 2

这篇关于Grails没有找到线程绑定的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆