Call http REST api from Flink using Asynchronous I/O - apache-flink

i have to fetch data from Rest API from flink process element in every data fetch from the stream, how can i achive that, i couldnt find enough meterials to call the Rest service asynchronously. please help me with some sample articles.

All the job is happening inside the asyncInvoke of the RichAsyncFunction. So, to be able to call REST service, You need to use some async HTTP client (technically it could be a synchronous client but this doesn't make sense). An example of async http client usage can be found here.
So, when You execute the async request all You need to do, is to call resultFuture.complete in Your request handler, so that the result is passed downstream in Flink.

Related

What is the idiomatic way for an API to return part of the data now, and part of the data later?

Suppose my back-end is using two (legacy) APIs the responses of which I want to combine. One returns the majority of the data and is quite fast. The other is slow, and only returns one feature for the dataset. Also, the response of the first API is required for the request to the slow API to be meaningful.
What I'd like to do, is that the back-end would return the result of the fast API instantly, then wait for the slow API, and then combine the two into a second response also containing the missing feature. If I'm correct, this would be accomplished somehow with the asynchronous HTTP pattern. However, I'm having troubles conceiving a solution while still keeping my API stateless, like all REST APIs should be.
So far: the following is the best concept I have been able to come up with:
A client (browser) sends a request to my API.
My API retrieves the data from the fast API, and sends a response like: {dataFromFirstAPI: {...}, linkToPoll: "/api/whatever/xyxzzy"} with status 202.
My API sends a request to the slow API, waits for response, processes the response and combines it with the result from the fast API. After that it returns the data to a request at linkToPoll.
Meanwhile the client will poll linkToPoll until it receives an appropriate response and body.
I have a few questions about the above approach:
Is it an antipattern to also return some other data in a 202 response (point 2. above)?
Is there any convention on what the polling URL should be?
One approach would be for the client to send two requests, but this would create state for my API.

Prevent same requests from being sent simultaneously when using Angular resource

I have a few components in one page.
Each of them fetches the same data from the server.
As a result, when the page loads, these components send the same request multiple times.
Is there any way to prevent this? Like caching the promise of the first request and returning that to the next coming requests (before the promise resolved)?
In order to make sure that the request is sent only once, you can keep track of the first HttpPromise you create, and on subsequent calls of the function, return that same promise.
This SO link might be what you're looking for.
When calling the $http service you can additionally supply a cache object. If you do so any additional requests will use the cached value. If the same cache is used then additional requests made before the first is resolved will not call the server but wait for the response.
$http.get(url, {cache:cacheObj})
Where cacheObj is from $cacheFactory

Preventing similar POST requests with AngularJS

I am currently building a dashboard page with multiple widgets. Those widgets retrieve their data with REST calls ($resource). A few widgets make similar calls and I don't want to DDOS our server so I am looking for a way to make a call only once and resolve all similar requests with the same response.
Since I am restricted to using POST requests only, I cannot use the cache option that $resource offers. This seems to be doing exactly what I want but only for GET requests.
I was thinking along the lines of using a http interceptor to queue similar POST requests, fire only one of them and resolving them all when the first one gets its response.
However, I cannot seem to put the pieces together so any help is appreciated. I am open to other options.
Kind regards,
Tim
Services in AngularJS are singletons, so a solution would be to store the response in the service, as a variable. Then next time you'll do the request, previously check if the variable is null, if it's not you wrap it in a promise and returned it. If it's null, then you do the request, and store the response for the next call.
You can also either use this in your request service or in your interceptor service.
I hope I helped !
Refactor your widgets to depend on a service (singleton).
This service should either poll the server via XHR, or get server push via websocket for updates.
If polling, look into server side caching and etags.

With Angular js's promise defer implementation do we need WEB API's to be async?

Angular JS supports Promise Defer functionality which is asynchronous. If I have async support on the client side, do I still need to expose async methods via WEB-API?
Making the client async improves the user experience, because user doesn't have to wait for every request.
But using async methods in the server can make the server faster and able to manage more requests. It is the same as in the client, if you don't block the main thread you are able to do other things while the other operation is executing.
You can implement async methods in one or in both sides. In each side you can get different benefits.
In this other question there are more information that explais when it is important to use async methods: Why should I create async WebAPI operations instead of sync ones?

Calling WebSphere Web Service from Silverlight

We would like to call a websphere web service from silverlight.
If I have understood it correctly:
Silverlight only supports async web service calls
Websphere does not support async calls
Is this correct?
Is it possible to call websphere web services from silverlight?
A general answer to your first question: There is no need for a webservice server to support asynchronous calls. Because HTTP is stateless, the server handles one request in one thread.
Generally speaking, the client can choose whether to wait for the response (synchronous) or to let a new thread wait for the response and do other things meanwhile (asynchronous).
The decision of doing synchronous or asynchronous calls is therefore only part of the client.
It should be possible.
Silverlight is asynchronous only in that the HTTP Web Request (GET, POST) is not linked to the receipt of the the HTTP response. You send an HTTP Request which is one action and separately from the Request you receive and handle the HTTP Response, you don't send a request then wait on the same thread for a response.
On your web server it makes no difference how you receive the request and send the response, so it could be handled synchronously or asynchronously, the Silverlight app would be oblivious to that.
Saying that 'Silverlight only supports async web service calls' only means that it does not block the calling thread while waiting for a response. The request is sent on one thread, the response is received on another thread.

Resources