title: Grails No thread-bound request found
body: <p>I am using a Grails service closure to perform a <code>async HTTP request</code> to another web application. Using <code>Grails 1.3.9</code>, 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: </p>
  
  <p>ERROR: </p>
  
  <pre><code>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></pre>
  
  <p>CODE:</p>
  
  <pre><code>    Thread.start {
  
          def http = new HTTPBuilder(grailsApplication?.config?.smsConfig?.smsServiceAddress);
  
          try {
              http.request(POST) {
  
                  body = bodyRequest;
                  requestContentType = ContentType.JSON
  
                  response.success = { resp -&gt;
                      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 -&gt;
                      println "FAILURE request failed with status ${resp.status}, response body was [${resp.entity.content.text}]"
                      System.out &lt;&lt; resp
                  }
              }
          }
          //here error is caught
          catch (e) {
              log.error "Error: " + e;
          }
      }
  </code></pre>
  
  <p>I have tried adding this options to web.xml </p>
  
  <pre><code>    &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
  </code></pre>
  
  <p></p>
  
  <p>and also this to WebXmlConfig.groovy</p>
  
  <pre><code>    listener.add = true
  listener.classNames = ["org.springframework.web.context.request.RequestContextListener"]
  </code></pre>
  
  <p>but both did not help</p>