0

As per clarification section(from MarkR) in link1

I felt that, Any server has single port on which server's listen() call is blocked(say tomcat) on which any client(say browser) would place connect() request, for instance, I assume, tomcat server does below(correct me)

//original/main single process does below

socket();bind();listen(on default port 80);


loop{ 

  accept(); 

//app layer logic(i.e., Below activity is done on seperate thread from a threadpool in main process)
//reads in all of the HTTP request and converts it into an object called an HttpServletRequest. That object is then passed in to my servlet by Tomcat via a method call. 

}

But As per link2,

I see that tomcat can listen on multiple ports, so how is the design of tomcat server for multiple/parallel listen() calls? Is tomcat having multiple processes where listen() is blocked on different ports?

Community
  • 1
  • 1
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Your very last question is correct. – Elliott Frisch Mar 14 '14 at 04:39
  • Having one thread per `ServerSocket` would be the easiest way to implement it. – Jason C Mar 14 '14 at 04:41
  • 1
    @ElliottFrisch So, How does the design look like? looks like tomcat server is fork+thread model, not single process/multithread model. I need more info here. – overexchange Mar 14 '14 at 04:41
  • 1
    @user3317808 It's one process, multiple threads. You can add Connectors in the server.xml file; and they all run in the same server instance. Note that each Connector has its own thread pool for processing requests (see [maxThreads](https://tomcat.apache.org/tomcat-7.0-doc/config/http.html) description). – Jason C Mar 14 '14 at 04:43
  • Its either single threaded with a selector or multithreaded. – Brian Roach Mar 14 '14 at 04:46
  • 1
    The big question is, why do you want to know all this? It shouldn't affect web application design in the least unless you're doing something very unsavory. – Jason C Mar 14 '14 at 04:48
  • 1
    @JasonC Knowing the behaviour/performance of tomcat by listening on multiple ports in real time is necessary before taking the decision to introduce multiple ports. and i feel, This can be done by understanding the design of tomcat server(high level). Yes i know that server.xml can be used as i mentioned in my query (link2), But Does tomcat server have two main processes listening on 2 different ports after i add one extra port number in this xml – overexchange Mar 14 '14 at 04:56
  • @user3317808 No. Knowing your actual performance requirements and whether or not they are being met in practice is necessary before doing *any* investigation into single vs. multiple ports on Tomcat. Determine your requirements, test, then optimize. If, at the end of the day, you are having performance issues, I'd give it about a 99.99% chance of *not* being related to Tomcat's internal implementation. You need to spend your time concentrating on more relevant things instead of optimizing prematurely. – Jason C Mar 14 '14 at 05:04
  • (Not to mention that proper application design will let you modify Tomcat's port configurations without requiring any changes to your code, which lets you analyze and test the impact *later* if your requirements are not being met by the current configuration.) – Jason C Mar 14 '14 at 05:05
  • I'd say: Ignore the tomcat software when it comes to thinking about multiple ports. Instead, use a proxy software like [nginx](http://en.wikipedia.org/wiki/Category:Free_proxy_servers) that forwards to tomcat. This also gives you a scalability advantage, most of the time. – Sascha Wedler Mar 14 '14 at 05:20
  • @user3317808 Tomcat follows the servlet specification, it handles each HTTP request in its own thread. In addition to that, it handles accepting new connections, in which tomcat has 3 different variants. 1. Using 1 thread per server socket. 2. Using NIO multiplexing in 1 thread. 3. Using the native apache APR library (This is for all I/O not just accepting connections, I'm not sure how the hative library does things). This is configured with the [http connector](https://tomcat.apache.org/tomcat-7.0-doc/config/http.html) – nos Mar 14 '14 at 13:14

1 Answers1

0

Tomcat uses one or more acceptor threads for each <Connector> (which binds to a specific port, so yes: Tomcat has multiple "processes" (threads) where accept (not listen) is blocked in each.

Note that Tomcat is completely Open Source, so you can go look at the code if you want to.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • Schultz My query refers, whether tomcat maintain multiple threads to maintain multiple listen ports? or multiple processes to maintain multiple listen ports? – overexchange Apr 05 '14 at 10:00
  • Apache Tomcat is in no way multi-process. If you are asking for a comparison between Tomcat and, say, httpd, then it comes down to the difference between the MPM you choose in httpd and the connector you choose in Tomcat. Tomcat uses separate "acceptor" threads to call `accept()` on each socket. – Christopher Schultz Apr 06 '14 at 14:52