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

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

问题描述

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

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:

错误:

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.

代码:

    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;
        }
    }

我已经尝试将此选项添加到 web.xml

I have tried adding this options to web.xml

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

还有这个到 WebXmlConfig.groovy

and also this to WebXmlConfig.groovy

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

但两者都没有帮助

推荐答案

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

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

试试 这个:

/*
* 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)
}

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

You will need spring test in your build.gradle

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

有一个 grails 2 分支,所以如果它是 grails 2,请在另一个分支中遵循相同的类

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

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

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