File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Is It good to have thread under a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is It good to have thread under a servlet" Watch "Is It good to have thread under a servlet" New topic
Author

Is It good to have thread under a servlet

PankajKumar Mishra
Greenhorn

Joined: Nov 09, 2010
Posts: 10

Hello guys this Pankaj Kumar Mishra , Right now I am working on a model asked by my Project Manager. This Model have following description with its problem and requirement.

1- Problem - There a is an client server application, in which Client is sending request to server and server responding it back to client.
The server is using Tomcat Appache web Server and DBCP for connection pooling. It was not working well as The DBCP usually has lots of connection instance free but request are unprocessed!
2- Requirement Description of New System:
There should be a system which accept the coming HTTP requests ,parameters and type of activity required by request.
a). A front controller System receive requests and its parameter. Enqueue Them into a queue
b). there should be 10 thread having independent db connection - responsible for dequeue information from queue by checking them and process the request
b) Finally processed request send back to Front controller so, that information can be send to the client side.

3- Solution to the System: and My Architecture- 4- Classes as per Description
a) Servlet as Front Controller --------------------------RequestReciever.java
b) A Queue class based on Fundamental DataStrutcture -------------------------RequestQueue.java ==with==RequestObject capsule to insert into Queue --RequestObject.java
c) A Sole class to Handle database connectivity , datafetch and update ---- ----------------------CoreConnector
c.i)A ChachedRow Class to hold records and even after connection ends --CRSHolder.java
d) Thread Class Which activate the Connectivity and Process the busyness Logic. -------------------------- ProcessRequestFromQueue.java
e) A ThreadPooler responsible for creating ThreadPool and handle the above Thread --------------------------TestThreadPoolExecutor
f) A listener to load database driver -------------------------- LoadWebResources.java
g) a Global class having all - public static object objects of ThreadPooler, ThreadClass, Queue and database connector Classes --------------------------globalx.java
The system is using [oracle [Client Driver] ,
This is system is working OK. But not good for what it is designed. As it is for concurrent and high load request handler. It is also making lots of request process unattained at user end.

Please give yours valuable suggestions and If it is wrong then what should be for else.


P K M The Tech
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

It is not only not a good idea to have a thread under a servlet, it's an actual violation of the J2EE specification.

The only way that a servlet can do multi-threading properly is if the thread was spawned in the init() method. Threads spawned in actual get/post handlers can potentially corrupt the entire server.

I should note that once you get into serious database processing, you're generally better off using an ORM system such as Hibernate/JPA. These systems can optimize database handling at the application level instead of only at the per-request level and as things scale up, will actually outperform hand-managed JDBC requests by a significant margin. And they work just fine with pooled connections.

However, ORM or not, if you have long-running transactions, you also need to consider the transactions themselves. First, measure the processing to see which transactions are offending. Then consider recoding them to be more performant. That may include actually coding SQL stored procedures in some cases, although I don't normally recommend stored procedures.

Sometimes, of course, there simply isn't an option. In this case, you have 2 alternatives: 1 construct a schemed whereby the heavy processing may be done offline (and generally off-hours). 2 multi-thread.

As you can see, your first resort is my last resort. That's because it's one of the more complicated approaches.

While a servlet isn't itself a good place to spawn threads, you can construct a processing "engine" either in the init() method of a servlet or (recommended) in a servletcontextlistener's startup method. Engines such as these generally consist of a (synchronized) queueing system, a scheduler, and processing code. Because of the fast-response requirements of http, you'll also generally want a request status query function as well, so that clients can monitor to see when the request has finished processing.

Apache commons can be a big help here. There are Apache Commons libraries suitable for queue management and for thread pooling. In fact, some of these libraries are actually employed by J2EE servlets such as Tomcat.


Customer surveys are for companies who didn't pay proper attention to begin with.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Something in the magnitude of 10 database connection should be handled easily by a DBCP...

It was not working well as The DBCP usually has lots of connection instance free but request are unprocessed!

Neither does this make sense to me!!
You need to ask yourself more questions about what the problem is,
What is holding up the processing? If its the database then is there any other shared resource? How long are request unprocessed?

Spawning threads in a web-servlet isn't a great idea. Tim's already given a great explanation on that. Also writing your own front controller - when there are so many frameworks out there seems to be an overkill ...

A listener to load database drivers - that's also kind of different. The new database specification auto-loads database drivers and the old way of doing it would be in the DAO class ina static block ...
You should consider using a ORM (hibernate or even Spring DBUtils) to simplify architecture.

If you looking for some general advice I would say you architecture is pretty complex and a bit dated (why create a TestThreadPoolExecutor class when Java 5 provided so many in build pool executors?).
Unless you really understand the details of your architecture (which isn't expressed here) - I think you'll see that there a lot of 'issues' that you would have to take care of in your new proposed architecture.





Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
PankajKumar Mishra
Greenhorn

Joined: Nov 09, 2010
Posts: 10
Many thanks Sam Mercs and Tim Holloway for yours great I ideas,

I am not some on have good hand over the Architecture design.
My TestThreadPoolExecutor is class which is using java.util.concurrency.* api and handling A Thread responsible for database connectivity.

I am so good to Programming with Java Technology and The proposed system was given to me 3rd day of my joining the 1st organization as S/W Eng on Java. It was test to my ability as self learner!
I have posted this post after founding myself helpless..

All though the above architecture with bit Modification and using Socket Programing had solved by my Senior.
I am happy that I got answered. I will learn yours recommended Technologies ORM (hibernate or even Spring DBUtils).
-------------------------------------------
I request for yours suggestions of learning technologies as I am in Telecom organization, which handles CRBT(Caller Ring Back Tone), Voice Mail, Voice SMS and cards like Dialogic and Songoma.
-------------------------------------------
This is an small org and I have to learn a lot by self. I am very happy and I am sure that I will learn a lot from here

Thanking you all in advanced!
 
 
subject: Is It good to have thread under a servlet
 
Similar Threads
servletContext and database
Need help with OO design
Test 252: Mock exam
some java interview Questions
MVC (urgent)