简体   繁体   中英

multithreaded http client and tomcat 7

I am confused about how should I handle http sessions in a standalone java app. Here are the details :

The java client connects to 3 tomcat 7 servlets. When the client boots up, it starts 2 scheduled threads /downloader and uploader/ polling 2 of the servlets every 3mins. They both retrieve and store the jsessionid cookie in private fields in their respective classes. This results in 2 sessions in tomcat reused for the lifetime of the webapp. So far so good.

There is a 3rd service /connected to the 3rd servlet/ using multiple instances of a threaded "WebDispather" class which retrieves and stores the session similarly to the above mentioned threads but this time - in a private static field. The dispatcher is heavily used, there might be as many as 150 instances running concurrently depending on the load. Dispatcher threads hit the servlet ever second or so.

Making the dispatcher sessionid field non static creates a session per instance - not good.

What are the implications of having all dispatcher threads bound to the same tomcat http session?

Thank you


EDIT: although dispather threads a bound to same session the session itself doesn't hold any information. Servlet processes only the request params. Ie dispatcher 1:

localhost/messagecontrol?id=123&state

Dispatcher thread 2: localhost/messagecontrol?id=123&state=finished

//Servlet processes and forgets id and state

As far as I can see the implications are all client threads will share the same session information, if any information not meant to be shared this will be a bug on your code

IF you're worried about the number of threads created (performance-wise), consider implementing a thread pool in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM