如何使使异步方式Web请求 [英] How to make an make a web request in an async manner

查看:123
本文介绍了如何使使异步方式Web请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个Web请求从Java REST式服务器。我想为我的Java code异步处理响应。我找了它处理请求的线程和回调的框架。

顺便说一句,我看了看FutureTask提供,它不会似乎是我所需要的,因为它需要客户端等待它在某个时候完成。我在寻找完成后的回调。


解决方案

  

顺便说一句,我看了看FutureTask提供,它不会似乎是我所需要的,因为它需要客户端等待它在某个时候完成。


您不要的有无以的调用从启动线程FutureTask.get(),以获得该任务的结果。你可以只具有可赎回传递给 FutureTask提供还处理通信其输出到其他组件。在的get()的方法有哪些,这样你可以得到异步计算,这可能需要等待计算完成,如果还没有结果。

如果您想preFER回调风格,你可以简单地有赎回调用自己构造的回调或处理它自己造成的。

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.

解决方案

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.

这篇关于如何使使异步方式Web请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆