Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

java - How to make an make a web request in an async manner

I need to make a web request to a RESTful server from Java. I would like for my Java code to handle the response asynchronously. I am looking for a framework which handles the threading and callbacks of the request.

By the way, I took a look at FutureTask and it does not appear to be what I need because it requires the client to wait for it to complete at some point. I'm looking for a callback upon completion.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By the way, I took a look at FutureTask and it does not appear to be what I need because it requires the client to wait for it to complete at some point.

You don't have to call FutureTask.get() from the initiating thread in order to get the results of the task. You could just have the Callable passed to the FutureTask also handle communicating it's output to some other component. The get() methods are there so that you can get the results of the async computation, which may involve waiting for the computation to finish if it has not yet.

If you would prefer the callback style, you can simply have the Callable invoke a callback of your own construction or handle the result on it's own.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...