• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need suggestion on Servlet's init()

 
Max Bean
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to initialize ThreadPool by the helping hands of init() method within the servlet. I don't even know whether it's a good idea or not. If I set that ThreadPool as static variable in the Servlet, is it easy to get access to it?

Isn't it hard to get something back from ServletContext as the caller is whatever class outside the Container's environment?

Many thanks
Max
[ November 12, 2007: Message edited by: Maximilian Boonyoung ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us a little more about the thread pool you're trying initialize?
How is it used and by what components; by just one servlet or by multiple components within your app?
 
Max Bean
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All would be done in single servlet. I have a scenario of using ThreadPool to store the user requests before persist to database if the number of threads is high enough. And then, at persist time, I will fetch those threads into the running processes.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Max Bean:
All would be done in single servlet. I have a scenario of using ThreadPool to store the user requests before persist to database if the number of threads is high enough. And then, at persist time, I will fetch those threads into the running processes.


When you say "store the request" you aren't talking about keeping a reference to the actual HttpServletRequest object passed to your servlet by the container, do you?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't it hard to get something back from ServletContext as the caller is whatever class outside the Container's environment?


Also, what, exactly will the 'caller' be?
 
Ryan Muster
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No! your logic is wrong... its already a multithreaded environment.
Unless you using a single thread servlet which is not recommended.

If you have long functions to perform, then launch a worker thread.
But I think you maybe wanting to set up a dB pool, which is not about threads its about a collection of dB connections, not the same thing.

Also I think you think that every thread gets a new servlet class, for the default servlet model that is not the case. You dont have to use static variables, because all the request threads are running through the SAME servlet instance.... remember that.

Yes INIT is made for you to set up the servlet, but be very careful will global variables... all those request threads come through that servlet.
There are db Pools on the web especially made for this purpose, or you can use the containers pool which normally comes in through JNDI.

There is already a thread pool for the request threads, and they dont queue, you could have 5 threads running through that servlet at the same time already... you do not have to try and help it, but you do need to watch thread safety and global variables are bad news...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan Muster:
No! your logic is wrong...


The original poster hasn't said enough about what he's doing for us to say whether his logic is wrong or not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!