How Soap supports asynchronous call while Rest does not? - java

I was going thru Soap vs Rest on net and found most of them says Soap supports asynchronous call while Rest does not but did not get any concrete example of that. Can anybody help me here?
Here is one of the resources i was referring to
http://web.archive.org/web/20120421084456/http://www.prescod.net/rest/rest_vs_soap_overview/
http://searchsoa.techtarget.com/tip/REST-vs-SOAP-How-to-choose-the-best-Web-service
http://seanmehan.globat.com/blog/2011/06/17/soap-vs-rest/
As per my understanding both should be synchronous. In both cases client makes a call to web service either thru soap or rest, client waits till response comes back from service. So how come soap supports asynchronous behaviour while rest does not?

REST is purely an HTTP transport based call and you will receive a response say 200 OK
on the other side,
SOAP uses two varieties,
Synchronous Messaging over HTTP
Asynchronous Messaging over HTTP
With synchronous messaging, the Requestor makes a request and the transport layer code blocks waiting for a response from the Provider. The Requestor receives the response on the same HTTP connection that the Requestor initially established to send the request. Synchronous exchange is usually easier to implement and requires that the Provider be able to generate a response in a short time, specifically in a time less than the HTTP timeout value(generally 120sec).
[A single HTTP Connection is used that itself behaves Synchronously]
With asynchronous messaging, the Requestor is able to release transport specific resources once the request is acknowledged by the responder, knowing that a response will eventually be received. When the Provider completes the processing of the message it sends a response back to the Requestor over a new HTTP connection.
[Here we utilize two HTTP Connections to implement an asynchronous messaging
first HTTP Connection is used that for sending the Request and receiving an acknowledgement HTTP Response 200/OK
second HTTP Connection is used for receiving the callback and responding HTTP Response 200/OK]
Rijoy
https://soascribbles.wordpress.com/

SOAP defines a reply approach that allows for asynchronous computing, like a callback mechanism. You can achieve the same with REST but there is no specification for it, so you would have to build it yourself.
Here is an example using JAX-WS 2.0 demonstrating the feature.
I found useful information in Wikipedia WS-Addressing, which has a link for this W3C Specification.
In the past I also developed in SAP ESB designer which allowed for asynchronous service interface methods. Although I never used that feature, that tool was fully compliant with SOAP specification and I am pretty sure it would work just like the Java example above, since the WSDL was used to generate JAX-WS server based. If I have time next week I will use that option to so see what happens and post it here.
You should also check this answer which address pertinent aspects of this approach.

I have created a REST service which actually calls an async method to interact with DB layer and made our service asynchronous. I feel it is possible to implement asynchronous service in REST API. But in that case you need to have a call back service which will check whether the process has been completed in regular interval.

Related

Rest API server fault tolerance

I have an API implemented with Jersey. An external service is making HTTP requests to my endpoint and expects a result into the response. Currently, I am using the Asynchronous Server API, in order to make all my business logic and then respond when ready. It’s working fine, but now I want to improve scalability and H/A.
My plan is to have multiple instances of my API behind an Nginx load balancer and all the requests accepted by any of my API instances will be forwarded to another microservice with RabbitMQ. Finally, I want to collect the results from the microservice and attach them into the response.
My question is if I can dynamically generate the response to the original HTTP request from any of my API instances. I want any of them, not only the one that originally got the webhook, to be able to send an HTTP response back to the caller. Somehow maybe to store/serialize the AsyncResponse object. In other words, this means that even if the instance that originally got the webhook request is stuck or down, another one can fill in and send the response.
Maybe this is not possible but still I would like to hear any other suggestions that can help.
Thanks.

External API async call with callback

I use Angular with Spring for internal API communication. Now I need to call external api to get some data. That external API provides callback feature, so I trigger call from Angular, call Spring rest method which, at the end, calls external API.
On the other hand I get data on my callback methods (Spring rest also), but I dont know how to transfer those data back to Angular.
Is websocket the only option?
If internal API calls are longer than the allowable client timeout, then you will need to find a WebSocket or WebSocket-like alternative. The alternative to WebSockets is to use long-polling (good example with source code here), which is essentially having the client repeatedly make requests until the original task is completed and the data can be sent. Either way, you'll need to use a pub/sub mechanism of some sort to handle multiple users, which is where things can get complicated.
Pub/sub can be complicated and I don't have an example on-hand, but essentially you must (1) have the client subscribe to a channel using a unique identifier (you can do this with CometD via a service channel), (2) evaluate the response, (3) publish the response to the client's subscribed channel, and finally (4) close the channel when it's no longer in use.
I've had some luck with CometD as a library to simplify pub/sub channel management, and it provides a good abstraction for asynchronous communication, although I haven't tried it with Spring and it might be heavy for what you want to do.
Another option that I'm less familiar with is to use Spring with STOMP. This seems to come recommended by others. Spring does provide ways to send messages to single users using STOMP: Sending message to specific user on Spring Websocket
I'd recommend following the above STOMP example.
Additional STOMP resources:
Getting Started with WebSockets (Spring)
Enable STOMP Over WebSocket
Angular2 with Stomp.js
As a side note, throttling may be necessary here, too, as with any time that you can spawn long-running threads from clients.
If you choose not to use STOMP or CometD, then a lighter solution could be to roll your own pub/sub for this single case using Spring's DeferredResult (as in Roger Hughes example), tying the subscription request to the long-poll request via a UUID token, which might also be the session ID if you choose to disallow concurrent requests per user. On subscription, the system can associate the request with a UUID and return this UUID to the client. The client can then make long-poll requests (again, as in Roger Hughes example) but with the UUID attached. The server can wait until the request for the given UUID has completed and then return the result to the client via the client's active long-poll request.
Channel management (i.e., request/UUID-tracking) can be done by clearing the channel on DeferredResult result retrieval and removing orphan channels with a separate thread acting as a GC -- or, perhaps better yet, automatically clearing orphan channels by removing them on DeferredResult completion/timeout if no active listeners exist. If you opt for the latter option, you will want to make sure that the client won't have any delay between its long-poll requests so that the DeferredResult doesn't unintentionally complete with no listeners.

How to implement Synchronous web service on JMS / MOM system?

I am working on a project which have many legacy heterogeneous systems. We are planing to connect them using JMS/MOM/ESB but need Synchronous web service calls from client.
i.e Request/Response architecture using web service is a requirement.
Client will make calls and wait for response.
My question is how can we implement Request/ response system which internally work on JMS/MOM to connect desperate systems?
Second question : Do any existing JMS/MOM or ESB implementations support such synchronous architecture?
first about second question: All of them support such architecture.
second about first:-)
just a brief idea:
when you send a message to target JMS Queue you have to have a unique message ID (in headers as example) then target system must answer (response) to "replyTo" queue. Then you have to have listener on that replyTo queue with filter by that unique ID and your flow must wait for response from that listener.
Something like that...

how to implement asynchronous communcation architecture using ESB and MQ

We have a requirement to design and implement the asynchronous web services. Here asynchronous web service communication meaning that a client places a request and he don't wait for the response from service. Although, it can achieved using AJAX or some other form but that is not our requirement.
I am looking to implement it using Message Ques and JMS(needs to read messages from request que and post messages to response que ).
Request: client->Message que->Web Service
Response: webservice ->Message Que->Client
Here we are also trying to leverage Enterprise Service Bus for load balancing or any other product.
Is it the right way of implementing or any other way of implementing it. thanks
You can implement a service where a client can communicate with it asynchronously and provide a callback address to which the service can send response messages.To have this communication pattern work effectively you need to have proper message correlation mechanism between request and response.
This can be implemented simply with SOAP Webservice.
Client sends request with unique request id and call-back address
to the service provider.
Service Provider receives the request and immediately ack to
client. when response is ready it calls client on the callback address.
Service provider can implement queue to service multiple
requests/clients.
Complexity here is you need to manage queue and correlation logic.
Most of the ESB's support asynchronous communication pattern, you can check how it be achived with FUSE ESB

Java: HTTP asynchronous non-blocking remote logging server?

I'm looking for a way of implementing a simple API to receive log messages (for various events across various systems).
We've concluded an HTTP get request is the most open (lowest barrier to entry) for the different code bases and systems that would need to post to this server.
The server itself needs to provide an HTTP GET api where I would send a message e.g. logging.internal/?system=email&message=An email failed
However we'd like this to be non blocking so any application can throw information to this server and never have to wait (not slowing any production systems).
Does anyone know of any framework to implement this in Java, or an appropriate approach?
In java, for the server, you can use any implementation of JAX-RS for the Restful part, and when processing the message, just call an asynchronous EJB method ( http://docs.oracle.com/javaee/6/tutorial/doc/gkkqg.html ), which will do the longer processing.
This will allow the RESTful request to return as fast as possible.
In this case, the only blocking part will be the http request/response.
If you want to make it less blocking, issue the RESTful request from the client in an Async method as well (or a Message Driven Bean if using Java EE 5).

Categories

Resources