I need my application to submit data to a web service and wait for 90 sec for the response. But if there’s a no response in 60 secs I need to redirect the user to a different page and continue to wait for the the response for another 30 secs if it come then process it.
I know I need to use thread for this but not sure how to integrate the treads in this case so threads can exchange data between themselves.
Any ideas?? I’m using JSF for UI.
The requirment is follows : The web service will send response in 90 secs (That’s the maximum response time for it). But the user will be given a response(A dummy response in case the response does not come within 60 sec) in 60 sec. So even if the user has been given a dummy response (after 60 sec) my application will continue to wait for another 30 sec for the response
Don’t know much about JSF, but it sounds like you want a timer, probably java.util.Timer. If the answer comes back before the timer goes off, shut down the timer. If the timer goes off, reset it for 30 seconds and redirect the user. The next time it goes off, give up waiting for the correct answer.
That much you seem to understand. But you’ve got at least two interacting threads threads here. How to communicate?
Just use instance fields. All references to them should be done with code in
synchronizedmethods or blocks. Do that and you should be fine. You’ll have to figure it out, but I would imagine you’d have an inttimerPhase, that would indicate the timer was not started, in the first 60 seconds, in the next 30, or timed out. Also a booleananswerReceived, something with the answer in it, and perhaps a few others.(Too much synchronization can slow your program down. I don’t think you will have this problem. But if you do, split the synchronize blocks up, with each field synchronized separately unless they interact. Remove synchronization and use the
volatilekeyword. Read up on multithreading. (Read up on thevolatilekeyword.) Think real hard about how parallel threads can interact. And prepare for things to get real weird.)