2017-09-05 1 views
0

RequestContextHolder.currentRequestAttributes()는 스프링 MVC 웹 응용 프로그램에 자녀 또는 다른 스레드에서 HttpRequest에 개체에 액세스하는 방법을

Exception in thread "Thread-4" 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. 
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) 
    at com.sbicapsec.managerImpl.LoginmanagerImp$1.run(LoginmanagerImp.java:36) 
    at java.lang.Thread.run(Unknown Source) 

내 코드

@Override 
    public void adminLogin(AdminModel model) { 

     Thread t=new Thread(new Runnable() { 

      @Override 
      public void run() { 
       final ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder 
         .currentRequestAttributes(); 
       final HttpServletRequest request = attr.getRequest(); 
      } 
     }); 
     t.start(); 
     HomePage.nc.AdminLogin(HomePage.nc, model.getUserName(), model.getPassword()); 
    } 

web.xml을

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 
    <display-name>loginproject</display-name> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <filter> 
    <filter-name>cors</filter-name> 
    <filter-class>com.sbicapsec.filter.SimpleCORSFIlter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>cors</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
<listener> 
    <display-name>RequestContextListener</display-name> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 

</web-app> 

내가 원하는 예외를 발생 내 관리자 클래스에서 포함 된 제 3 자 항아리의 메서드를 호출하기 때문에이 작업을 수행하십시오. 그러나 이러한 메서드는 다른 스레드에서 호출됩니다. 나는 세션 특성에서 이러한 메서드의 응답을 넣어서 내 스레드에서 사용할 수 있습니다. 이 당신의 "제 3 자 LIB는"당신이 단지에 대해 이야기되는 경우

@Override 
    public void adminLogin(AdminModel model) { 
// I assumed that this is in request handling thread 

     final ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder 
         .currentRequestAttribut 
     Thread t=new Thread(new Runnable() { 

      @Override 
      public void run() { 
       //now this might work 
       final HttpServletRequest request = attr.getRequest(); 
       //d o something with the request. 
      } 
     }); 
     t.start(); 
     HomePage.nc.AdminLogin(HomePage.nc, model.getUserName(), model.getPassword()); 
    } 

: 당신이 보여준 그 코드를 가정

+0

요청 처리 스레드에서 "manager class methods"를 호출하십시오. 만약 당신이 이미 그렇게한다면, 왜 당신은 runnable에 요청 객체를 전달하지 않을 것인가? digresion의 문제로, 당신은 오히려 새로운 스레드를 시작 스레드 풀 insteed를 사용해야합니다. 새로운 스레드에 – Antoniossss

+0

요청하지 않았습니다. 나는 당신이 새로운 스레드에서 그 작업을 할 수 없다고 생각한다. –

+0

@Antoniossss 그것들은 내 메소드가 아니다. 나는 그 메소드에 접근 할 수 없다. 나는이 메소드를 호출 할 때 내부적으로 새로운 스레드를 생성한다. –

답변

0

요청 처리 스레드에서 호출 코드가이 같은 일을해야 할 것입니다 그것이 작동 할 방법은 봄 워드 프로세서 (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/context/request/RequestContextHolder.html) 스레드 바인딩 RequestAttribut의 형태로 웹 요청을 노출

홀더 클래스에 따라 때문에 trueinheritable 스레드를 설정하는 것 es 객체. 상속 가능한 플래그가 으로 설정되어 있으면 자식 스레드 이 현재 스레드에서 생성 한 스레드에 의해 상속됩니다.

+0

이전에 말씀 드렸듯이 제 3 자 라이브러리에 액세스 할 수 없습니다. 위 코드는 예제 일뿐입니다. 실제 코드는 아닙니다. –

+0

그게 문제라는 것을 알았고 "예제 코드"를 주었고 "예제 솔루션"을 주었다. 보시다시피 SO 커뮤니티와 여러분을 위해 쓸모가 없습니다. – Antoniossss

+0

필수 매개 변수를 '상속 가능'으로 설정하면 트릭을 수행 할 수 있습니다. – Antoniossss

관련 문제