1

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

4
  • youtube.com/watch?v=378DHU9IsS4 Commented Apr 23, 2012 at 9:33
  • 1
    "need to redirect the user to a different page and continue to wait for the the response" - what it mean? Either you java-application may be redirected (so java app is a client), Or there is some communication over browser? Please clarify Commented Apr 23, 2012 at 9:39
  • You should probably refrain from using threads in a container-managed application, as thread management is typically carried out by the container. I don't really understand the use case here: wait 60 seconds, then redirect (why?? for user feedback?), wait 30 seconds more (and then what?). Can you elaborate a bit? Commented Apr 23, 2012 at 9:42
  • 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. Commented Apr 23, 2012 at 9:54

1 Answer 1

1

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 synchronized methods or blocks. Do that and you should be fine. You'll have to figure it out, but I would imagine you'd have an int timerPhase, that would indicate the timer was not started, in the first 60 seconds, in the next 30, or timed out. Also a boolean answerReceived, 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 volatile keyword. Read up on multithreading. (Read up on the volatile keyword.) Think real hard about how parallel threads can interact. And prepare for things to get real weird.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.