2017-10-07 138 views
0

我試圖從服務器的文件系統中流式傳輸視頻。我使用Spring的StreamingResponseBody作爲響應返回。所以我不得不異步處理流媒體。 我做了很多關於如何配置我的web.xml和Spring MVC的研究,我遵循這個簡單的教程:http://shengwangi.blogspot.de/2015/09/asynchronous-spring-mvc-hello-world.html ,但不幸的是沒有成功! 我仍然收到異常告訴我:「必須在servlet上啓用異步支持,併爲異步請求處理中涉及的所有過濾器啓用異步支持。這可以在使用Servlet API的Java代碼中完成,或者在servlet和過濾器聲明中添加」true「 web.xml「儘管我認爲我做的一切都是正確的。 這裏是在web.xml我的servlet配置Servlet 3 Spring MVC的異步支持不起作用

<servlet> 
     <servlet-name>dispatcherServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/mvc-config.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
     <async-supported>true</async-supported> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcherServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/application-context.xml</param-value> 
    </context-param> 
    <context-param> 
     <param-name>spring.profiles.active</param-name> 
     <param-value>production</param-value> 
    </context-param> 
    <filter> 
     <filter-name>encodingFilter</filter-name> 
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </filter> 
    <filter-mapping> 
     <filter-name>encodingFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>REQUEST</dispatcher> 
     <dispatcher>ASYNC</dispatcher> 
    </filter-mapping> 
    <filter> 
     <display-name>springSecurityFilterChain</display-name> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>REQUEST</dispatcher> 
     <dispatcher>ASYNC</dispatcher> 
    </filter-mapping> 

的Spring MVC的配置的代碼snipt:

<mvc:annotation-driven> 
    <mvc:async-support default-timeout="30000" task-executor="taskExecutor"> 
    </mvc:async-support> 
</mvc:annotation-driven> 
<bean id="taskExecutor" 
     class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> 
    <property name="corePoolSize" value="5" /> 
    <property name="maxPoolSize" value="50" /> 
    <property name="queueCapacity" value="10" /> 
    <property name="keepAliveSeconds" value="120" /> 
</bean> 

這裏的請求處理程序:

@GetMapping("/videos/play") 
    public StreamingResponseBody stream(@RequestParam("code") String code, @RequestParam("locale") String locale) throws FileNotFoundException{ 
     File video = videoService.getVideo(code, locale); 
     final InputStream videoInputStream = new FileInputStream(video); 
     return (outputStream) -> { 
      copyToStream(videoInputStream, outputStream); 
     }; 
    } 
private void copyToStream(final InputStream is, OutputStream os) 
      throws IOException { 
     byte[] data = new byte[2048]; 
     int read = 0; 
     while ((read = is.read(data)) > 0) { 
      os.write(data, 0, read); 
     } 
     os.flush(); 
    } 

我感謝所有幫助,因爲我在3天前掙扎! 我正在使用Tomcat8.0和Spring 4.3.6

回答

0

我讀了許多線程,這表明它是Tomcat 7或8中的一個bug,但它不會。異常表明異步支持必須在所有過濾器中啓用。由於我試圖做到這一點,但問題是我如何在web.xml文件的過濾器中添加異步支持。 我所做的是以下

<filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>REQUEST</dispatcher> 
     <dispatcher>ASYNC</dispatcher> 
</filter-mapping> 

這實際上告訴映射接受異步請求(我猜),但它不能夠在過濾器本身的異步支持。 所以在每個過濾器定義,我們應該使異步支持,所以springSecurityFilterChain定義變爲:

<filter> 
    <display-name>springSecurityFilterChain</display-name> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    <async-supported>true</async-supported> 
</filter> 
+0

嗨 - 我面臨着類似的問題,並試圖您的加入過濾器的裝飾解決方案。但得到一個錯誤 - org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有名爲'springSecurityFilterChain'的bean可用你能請指導我嗎? – csharpnewbie