2012-12-26 119 views
0

嗯,即時通訊嘗試在Java中創建Web應用程序,每當有人在地址中發出http請求時啓動一個線程。 這是一個很好的實踐嗎?可以工作?優點還是缺點?工作是低於和使用彈簧例子,只是增加了,我想Web應用程序中的線程Java

PS線程:這是在Tomcat中

> HomeController.java 

    @Controller public class HomeController { 

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

    /** 
    * Simply selects the home view to render by returning its name. 
    */ 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model) { 
     logger.info("Welcome home! The client locale is {}.", locale); 

     Date date = new Date(); 
     DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 

     String formattedDate = dateFormat.format(date); 

     model.addAttribute("serverTime", formattedDate); 
     new Teste().start(); 
     return "home"; 
    }}class Teste extends Thread{ 

    @Override 
    public void run() { 

     while(true){ 
      System.out.println("im in thread"); 
      try { 
       Thread.sleep(5000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 
    } 

> web.xml 

    <?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5"xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/n/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

> servlet-context.xml 

    <?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="com.example.threadTestes" /> 



</beans:beans> 
+1

類似的問題可以在這裏找到(http://stackoverflow.com/q/3745905/221781)。 – izilotti

+0

我認爲這是一個問題http://codereview.stackexchange.com –

回答

2

運行在託管環境線程通常是一個壞主意。爲什麼不使用像JMS這樣的抽象來在每次有人發送請求時啓動後臺處理程序?這種方式可以控制活動線程的數量(jms池大小)

+0

爲什麼是一個壞主意?我需要通過http – user1866731

+1

來請求你的客戶/用戶發出的GET請求,這些請求由tomcat(在它自己的有限大小的線程池下)處理。如果你只是爲每個請求創建一個線程,並且你無法控制線程運行的時間長度,那麼你很容易受到各種各樣的濫用。它更好地排隊在一些完善的機制,可以照顧池大小,重試等工作,而不是重新發明這一切。此外,JMS將允許您通過在其他機器上添加事件處理程序來擴展未來 - 羣集 – radai

+0

並且如果我限制創建的線程數量?可以工作嗎? – user1866731

1

對於每個請求啓動線程在任何應用程序中都是可怕的想法,託管或非託管,並且#1原因是內存操作系統可以使用的內存量爲新線程分配堆棧是有限的;不僅它是有限的,它在框中的所有進程之間共享(儘管您的java進程可能只允許使用其中的一部分進程)。現在想象那些線程不會終止,或者不會因爲新的請求不斷進入而快速終止 - 操作系統最終將耗盡新線程的空間;取決於操作系統及其設置,結果可能與新Thread()中相當迷人的OutOfMemoryError有所不同,因爲您的java進程佔用了所有可用空間,並且完全不能創建新線程。所以:1)處理請求至少使用線程池(Executor);如果需要持久性,JMS是一個選項,但是您再次處理池中的請求 - 它只是從JMS 2中提供)用於某種維護任務,您可以在應用程序啓動期間啓動單獨的線程。在這兩種情況下,自行清理並在關機期間停止附加線程。

+0

就像我說的,我的意圖只是提出一個請求。所以我認爲是可能的。 。 – user1866731