Spring Concurrency parallel request

akoayisangmahusaynaprogrammer

Im developing system using Spring MVC FrameWork then I've discovered that my app handles request one at a time. My app was deployed in tomcat server on linux.

To confirm this, I created a simple page that first get starttime then sleep for ten seconds then get end time, then display them on view. Then I access this page in my browser 3 times simultaneously. The result below confirms that the server only handle request one at a time.

Start : Thu Jun 02 17:01:24 CST 2016 End : Thu Jun 02 17:01:34 CST 2016 TimeLapsed : 10001

Start : Thu Jun 02 17:01:34 CST 2016 End : Thu Jun 02 17:01:44 CST 2016 TimeLapsed : 10001

Start : Thu Jun 02 17:01:44 CST 2016 End : Thu Jun 02 17:01:54 CST 2016 TimeLapsed : 10001

My controller code:

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Date;
@Controller
@RequestMapping("/")
public class ParallelTestController {
    @RequestMapping(value="/test", method = RequestMethod.GET)
    public String test(ModelMap model) {
        String startTimeStr = new Date().toString();
        long startTime = System.currentTimeMillis();
        try{
            Thread.sleep(10000);
        }catch(Exception e){
        }
        String endTimeStr = new Date().toString();
        long endTime   = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        model.addAttribute("startTime", startTimeStr);
        model.addAttribute("endTime", endTimeStr);
        model.addAttribute("totalTime", totalTime);
        return "welcome";
    }
}

My view code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HelloWorld page</title>
</head>
<body>
    Greeting : ${greeting}
    <br />
    Start : ${startTime}
    <br />
    End : ${endTime}
    <br />
    TimeLapsed : ${totalTime}
    <br />
</body>
</html>

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>EMS</display-name>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

How can I configure my app to handle multiple requests simultaneously? Or is is tomcat config?

codependent

Your application is working as expected, processing requests with different threads, so parallel processing is applied:

Thread : http-bio-8080-exec-23 Start : Thu Jun 02 17:49:51 CST 2016 End : Thu Jun 02 17:50:01 CST 2016 TimeLapsed : 10001 
Thread : http-bio-8080-exec-24 Start : Thu Jun 02 17:50:03 CST 2016 End : Thu Jun 02 17:50:13 CST 2016 TimeLapsed : 10000 
Thread : http-bio-8080-exec-28 Start : Thu Jun 02 17:50:13 CST 2016 End : Thu Jun 02 17:50:23 CST 2016 TimeLapsed : 10000 

Then why are the requests executed serially? What you are seeing is browser related, that is to say, you are launching requests from the same browser and it doesn't execute the next request until the previous one is finished. That's why when you try with two different browsers you see what you expect:

browser 1: Thread : http-bio-8080-exec-31 Start : Fri Jun 03 08:38:13 CST 2016 End : Fri Jun 03 08:38:23 CST 2016 TimeLapsed : 10000 – rbmeo 6 hours ago        
browser 2: Thread : http-bio-8080-exec-29 Start : Fri Jun 03 08:38:11 CST 2016 End : Fri Jun 03 08:38:21 CST 2016 TimeLapsed : 10000

so the frontcontroller process requests one per session? requests from same browser have the same cookieid, so the same session right?

No, it's not one request per session. If you make two AJAX calls you'll see that they belong to the same HttpSession and they are processed in parallel.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related