aspose file tools*
The moose likes Web Services and the fly likes Can multiple users call a web services at the same time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Can multiple users call a web services at the same time?" Watch "Can multiple users call a web services at the same time?" New topic
Author

Can multiple users call a web services at the same time?

Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
Hey

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.

Thank you.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Assuming the web service is correctly written, sure. Its just like regular servlets, each HTTP request gets a separate thread.

The usual precautions - don't use instance objects, synchronize access to resources that only permit a single user, etc etc - apply.

Bill
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
Well there is the thing: I need to instance the same object various times (each time the web service is called) so how can I pass this limitacion?

I also connect to a db but I think I can bypass that in MySQL
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
If your present design has led you into an unavoidable limitation it is time to back off and redesign.

What does this object that every request needs access to actually do?

Bill


Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Maybe the object can be assigned to a static variable (in which case access to it would need to be synchronized if its contents can change at runtime).
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2408
    
  28

Thereotically, each web service request can be called in it's own thread, and from the web service developer POV, the container cannot guarantee that all the calls to web service will not be in parallel. The Web service developer has to assume that the call to his/her web service might occur concurrently with other calls to the same web service

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.
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
First, thank you for all the replies.

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.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2408
    
  28

Look at the example on their page. It's not difficult if you follow their example

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.
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
One of my questions is do I have to declare a pool of object for each and every object type or can I use a generic type even if they have nothing to do with each other???

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
Bartender

Joined: Jan 17, 2008
Posts: 2408
    
  28

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.
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
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.
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
I thought it was working but no...........for some reason it wasnt refreshing the web service.

With a "ObjectPool" it doesnt work. Fails on the first one (which is not complex at all)
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
A bit of code (simplified) so you can see:

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.
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
The error is this:

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2408
    
  28

Where are you you creating an instance on ObjectPool?
Koldo Urrutia
Ranch Hand

Joined: Dec 16, 2011
Posts: 85
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



None work.
 
Consider Paul's rocket mass heater.
 
subject: Can multiple users call a web services at the same time?