Im wondering if various users can a web services at the same time and execute the same code to generate something. Does it do it at the same time on difference threads/objects? Is it FIFO (First in, first out)?
Questions like those.
Practically, however, most web containers have connection pools, and can take only so many connections at the same time. Once they have too many active requests, they put new requests on hold. Also, most web containers allow you to tune the size of it's pools
If your webservice requires a heayweight object, it is almost always a good idea to reuse the object between web service calls. Also, if you are doing some heavy processing in a web service call, it is almost always a good idea to limit the number of concurrent calls to the heavyweight webservice. Let's say a call to the web service usually takes 400MB of heap, and you are constrained to 2GB, and your web container uses 200MB of memory in idle conditions:- Well, 4 concurrent calls to your web service will use up 1.8GB of memory, and you really shouldn't allow a 5th call to go through. That 5th call will break your system by either causing too much GC or triggerring OOM. Practically, you should implement faillover by putting the 5th call on hold until one of the currently runnning request is complete
So, in these cases, where you have heavyweight objects that should be constrained and reused between calls, you should use a resource pool. Apache commons provides a good implementation of the pool. http://commons.apache.org/pool/ You can put an upper bound on the size of the pool.Between calls the heavyweight object will stay in the pool. When the size of the pool reaches max, the pool will block the request until an object is returned back to the pool.
Also, in cases like these, it might be useful to tune your web container to match your web service usage. Although, the actual tuning can get a little complicated because your have to take into account all the requests coming into your server, not just the ones that yield in calls to heavy web services.
I would call my web services "heavy" seeing as it does A LOT of math calculations which are stored/read in different arrays. I dont think the implementation can be done any other way....
The object in question is a two demensional array which I load with calulated values from another two demensional array (which, depending on what values, are loaded with values from a row in a database).
I dont mind that the virtual machince needs more RAM (not ideal I know), basically I want this to work: Optimization is something that can be seen as a long time goal.
Your idea Jayesah about letting say 5 calls thru and having the 6th wait, sounds ideal for the project. Unforchantly (sp?), Im pretty new to this so the Apache Commons resource pool isnt too much help as I have no idea how to implament it.
Are there any examples around that show how to work with this?
Again, thanks for all the replies.
The example shows how you can create a pool of StringBuffer objects. What you can do is create a class called MyWorker that takes inputs and generates your 2 dimensional result. Insitead of putting StringBuffer in the pool, put MyWorker.
To set a hard limit on size of pool, set MaxActive. Now, in your web service, borrow an instance of MyWorker from the pool, use it to do your calculation, and return it back to the pool. The pool will take care of putting requests on hold when all the workers are active.
ETA: Since your MyWorker instances will be reused you have to worry about MyWorker not keeping state between calls. I mean when one thread borrows an instance, you have to make sure it's initialized so any data from the previous thread doesn't spill over. If you can get this working, you can start worrying about how to reuse heavy data structures that MyWorker uses. For example, if it needs a temporary 2D array, it can create a 2D array and store it in a data member, so you don't create a 2D array for every call. After a service borrows the instance, it will have to make sure the 2D array is reinitialized.
Using the ObjectPool is pretty easy, but using it effectively needs a bit of thought and analysis.
I saw that example and more or less understand even though like I posted I have some doubts....
Thanks for the hel
Jayesh A Lalwani wrote:Apache provides a generic implementation of pool. You on't need to build your own. You shouldn't pool al objects. It will make your code complicated. You should pool only the biggest ones.
Well........I already pooled all the objects so........
Anyways it seems to work better: I only have a small problem which Im not sure if to resolve with MaxActive or some other way....
Each time my web service is called a unique but with the same name file is created and used, afterwards I delete it. When 2 seprete processes call the web service, it seems that the first one that created it, deletes it, and obviously because of small time issues it gets mixed up and/or overwrites it so the second one cannot access it......
Im not sure how to overcome this problem.
This is my main part of my program:
It doest get to the "set actives sets" println. It prints out a Java error.
Here is my Fecha class
As you see Fecha is not really complex.
Jayesh A Lalwani wrote:Where are you you creating an instance on ObjectPool?
Tried in several places.
First in the class of some class using:
private GenericObjectPool<Fecha> fechapool=new GenericObjectPool<Fecha>();
That didnt work so then I tried (with the above already in place