views:

38

answers:

2

I am currently coding a Java WebService using axis2. However, one particular request needs me to do constant polling a status in another server for around 3-10 seconds time.

I want to use Thread.sleep to do polling like every 500 mili for 3 seconds. Does it have any implication like performance issue or can anyone suggest a better idea?

EDIT My architecture is like this

Client <---> axis2 <---> server polling for 3-10 seconds

+1  A: 

Another pattern is to have the original caller return with a received_ok flag and then store the results on your server when they return from the second server. The first server would then reconnect and then you could either return the results or the fact that they are not back yet. The benefit of this is that you could delegate the polling to another instance and not tie up your original server.

Romain Hippeau
I don't quite understand. Could you explain a bit more indepth? Ps. I have edited my question.
henry
Does the call need to be synchronous ? If not have the client post the info the server return "received" the server does its thing and stores the data. The client at a later time reconnects and gets the data.
Romain Hippeau
Yes. It needs to be synchronous, so it will be transparent to the users/clients that connect me. Your suggestion crosses my mind also. But I would prefer to be one-time off connection. :) Just that, I am not sure if I approach it right.
henry
+1  A: 

There are a number of different options:

If you can change the client, then it may be a good idea to move the wait into the client. This means that the server doesn't have loads of threads hanging around.

So you would have two web services, one to initialise the request, the second to get the result. The client (not the axis server) would call the first web service, and then the second possibly multiple times.

This has the advantage that you don't have to do any work with threads in the server (which simplifies life a lot). The thread code is in the client.

If you do end up doing a sleep in your server, make sure you have enough threads, if you're using Tomcat, see http://stackoverflow.com/questions/105754/best-practices-for-configuring-apache-tomcat

MatthieuF
Is thread.sleep an evil practice If I stick to the sleep pattern?
henry
In general, it's considered bad practive to do sleeps in web servers if you can avoid it, because it ties up the thread, but I can't see any real problems with it. If you were doing a blocking read you'd be waiting, it's the same sort of thing.
MatthieuF
Got it. Thanks. The request will be very very rare.
henry