如何:通过 Java 使用 SOAP/REST Web 服务的异步回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4967413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to : Async Callbacks using SOAP/REST Web Services with Java
提问by anjanb
We have a Java API that needs to be supplemented/fronted with a SOAP/REST Web service Layer.
我们有一个 Java API,需要补充/前置一个 SOAP/REST Web 服务层。
What does it take to implement Async Calls across process/server boundaries using
a) SOAP Webservices
b) RESTful webservices
使用
a) SOAP Webservices
b) RESTful webservices跨进程/服务器边界实现异步调用需要什么
Some of the methods might need multiple calls to respond to the request.
某些方法可能需要多次调用才能响应请求。
We will be using Java/J2ee to implement the SOAP/restful service using a library like CXF or Axis or Jax-RS/WS.
我们将使用 Java/J2ee 使用 CXF 或 Axis 或 Jax-RS/WS 之类的库来实现 SOAP/restful 服务。
Any examples ? Gotchas ?
有什么例子吗?问题?
Thank you,
谢谢,
回答by ams
Your question is not clear. I am interpreting your question as you want your serverside code to call a remote REST web services in an Async manner. If so then your best bet is to use the Futures feature of java.util.concurrent it will do exactly what you want. If my interpretation of the question is wrong then please update your question with exactly where the async operations need to happen.
你的问题不清楚。我正在解释您的问题,因为您希望服务器端代码以异步方式调用远程 REST Web 服务。如果是这样,那么您最好的选择是使用 java.util.concurrent 的 Futures 功能,它会完全满足您的要求。如果我对问题的解释是错误的,那么请使用异步操作需要发生的确切位置更新您的问题。
回答by Felipe Oliveira
Akka http://akka.io/
Great framework, great performance - Here are their claims:
出色的框架,出色的性能 - 以下是他们的主张:
"""
"""
Simpler Concurrency
更简单的并发
Write simpler correct concurrent applications using Actors, STM & Transactors.
使用 Actors、STM 和 Transactor 编写更简单、正确的并发应用程序。
Event-driven Architecture
事件驱动架构
The perfect platform for asynchronous event-driven architectures. Never block.
异步事件驱动架构的完美平台。从不阻塞。
True Scalability
真正的可扩展性
Scale out on multi-core or multiple nodes using asynchronous message passing.
使用异步消息传递在多核或多节点上横向扩展。
Fault-tolerance
容错
Embrace failure. Write applications that self-heal using Erlang-style Actor supervisor hierarchies.
拥抱失败。使用 Erlang 风格的 Actor 监督层次结构编写自我修复的应用程序。
Transparent Remoting
透明远程
Remote Actors gives you a high-performance transparent distributed programming model.
Remote Actors 为您提供了一个高性能的透明分布式编程模型。
Scala & Java API
Scala 和 Java API
Scala and Java API as well as Spring and Guice integration. Deploy in your application server or run stand-alone. """
Scala 和 Java API 以及 Spring 和 Guice 集成。在您的应用程序服务器中部署或独立运行。"""
回答by Nik
The Async Http Clientis an open source library that was specifically designed for this type of problem. It utilizes futures and wraps up a lot of the detail and hassle out of making async calls.
的异步HTTP客户端是专为这种类型的问题设计了一个开源库。它利用期货并解决了进行异步调用的许多细节和麻烦。
The author has a very good getting started guideand there is an active discussion group. The author is a very talented developer and the project is under continuous development.
作者有一个非常好的入门指南,并且有一个活跃的讨论组。作者是一位非常有才华的开发人员,该项目正在不断开发中。
From the documentation:
从文档:
The library uses Java non blocking I/O for supporting asynchronous operations. The default asynchronous provider is build on top of Netty (http://www.jboss.org/netty), the Java NIO Client Server Socket Framework from JBoss, but the library exposes a configurable provider SPI which allows to easily plug in other frameworks.
该库使用 Java 非阻塞 I/O 来支持异步操作。默认的异步提供程序构建在 Netty (http://www.jboss.org/netty) 之上,Netty 是 JBoss 的 Java NIO 客户端服务器套接字框架,但该库公开了一个可配置的提供程序 SPI,它允许轻松插入其他框架.
回答by javadeveloper
@Vivek
@Vivek
GET is async and other HTTP methods are not.
GET 是异步的,而其他 HTTP 方法不是。
This isn't true. Please go ahead and read about AJAX :-)
这不是真的。请继续阅读 AJAX :-)
回答by vishal
One of the best ways to implement asynch ops is to use callbacks. For REST APIs, design of APIs and Rest client should support this. For instance , client class should pass itself or it's inner class as listner. Rest API on server side should maintain request id and call back listener in map . Once processing is done , it can call method on listener based on request id from map.
实现异步操作的最佳方法之一是使用回调。对于 REST API,API 和 Rest 客户端的设计应该支持这一点。例如,客户端类应该通过自身或它的内部类作为侦听器。服务器端的 Rest API 应该在 map 中维护请求 id 和回调侦听器。处理完成后,它可以根据来自地图的请求 id 调用侦听器上的方法。
回答by user3733649
Real question: why do you want to call it Async? Having looked at solutions for parallel processing on the Java EE side, it's not recommended that you spawn child threads within a container on your own.
真正的问题:为什么要称其为 Async?在查看了 Java EE 端的并行处理解决方案后,不建议您自己在容器内生成子线程。
In your case, it looks like the following:
1. you're looking to create a wrapper contract in WSDL (REST or SOAP or both) and if you clients are not just browsers (AJAX)(i mean you'd have adopters from the server-side), then, for JAX-WS -> you could create a @CallBack end-ponint (http://docs.oracle.com/cd/E15051_01/wls/docs103/webserv_adv/callback.html)
or
if it's REST end-point that requires being called from an adopter (server-side), use jAX_RS 2.0 feature
在您的情况下,它看起来如下所示: 1. 您希望在 WSDL(REST 或 SOAP 或两者)中创建包装器合同,并且如果您的客户不仅仅是浏览器(AJAX)(我的意思是您有来自服务器端),然后,对于 JAX-WS -> 你可以创建一个 @CallBack 端点(http://docs.oracle.com/cd/E15051_01/wls/docs103/webserv_adv/callback.html)或者
如果它是 REST 端点,需要从采用者(服务器端)调用,使用 jAX_RS 2.0 功能
Note: Above assumes it's point to point but in an Async way
注意:上面假设它是点对点但以异步方式
Here are a few options:
这里有几个选项:
- if you're looking to call REST Or SOAP or some other function asynchronously, you can use workManager API (JSR ) or
- use JMS and use a request-reply model if you need it (short running) and calling multiple end-points in parallel
- use WS-BPEL (only WSDL end-points) - this is a OASIS standard as well
- use SCA (any component with any technology) that can contain assemblies of WS-BPEL component (stateless or stateful) running in BPEL engine like Apache ODE or IBM process server and other components and collaborates. This is a standard
- 如果您希望异步调用 REST 或 SOAP 或其他一些函数,您可以使用 workManager API (JSR) 或
- 使用 JMS 并在需要时使用请求-回复模型(短期运行)并并行调用多个端点
- 使用 WS-BPEL(仅限 WSDL 端点)——这也是 OASIS 标准
- 使用可以包含在 BPEL 引擎(如 Apache ODE 或 IBM 流程服务器)和其他组件中运行的 WS-BPEL 组件(无状态或有状态)的组件的 SCA(具有任何技术的任何组件)并进行协作。这是一个标准
回答by Vivek
For REST web services (apart from GET) everything else (POST/PUT..) is Async, it returns you the HTTP status code of the opeeration.
对于 REST Web 服务(除了 GET),其他一切(POST/PUT..)都是异步的,它会返回操作的 HTTP 状态代码。
If you want to make GET also Async then you will have to use Threads, We did it once in Spring framework using @Async annotation (which internally spawns a thread).
如果你想让 GET 也异步,那么你将不得不使用线程,我们在 Spring 框架中使用 @Async 注释(它在内部产生一个线程)做过一次。
From get return the callback URL as the response and use threads to process the request asynchronously.
从 get 返回回调 URL 作为响应,并使用线程异步处理请求。
Apart from that, For both SOAP / REST once you get the request you can publish it on a JMS queue to make the service Async.
除此之外,对于 SOAP / REST,一旦您收到请求,您就可以将其发布在 JMS 队列上以使服务异步。