How is possible to handle timeouts in time consuming operations in a REST API. Let's say we have the following scenario as example:
I can think I a solution with a message broker to send orders to a queue and wait until they are solved.
Any other workaround?
EDIT 1:
Basically you can do it in 2 ways. The first one, is wrapping your REST Call in an action, and in this action you can create an error handler flow: This way you can have a specific message if the default error message or the error code is the one you want to handle.
One way we can implement a request timeout on database calls is to take advantage of Spring's @Transactional annotation. It has a timeout property that we can set. The default value for this property is -1, which is equivalent to not having any timeout at all.
If you really need to implement a timeout on the API side itself, I would recommend creating a thread to do your work in, and then cancelling it after a certain period. You could for example put it in a Task , create your 'timeout' task using Task. Wait and use Task. WaitAny for the first one to come back.
HTTP offers a set of properties for invoking certain methods. These are primarily safetiness
, idempotency
and cacheability
. While the first one guarantees a client that no data is modified, the 2nd one gives a promise whether a request can be reissued in regards to connection issues and the client not knowing whether the initial request succeeded or not and only the response got lost mid way. PUT
i.e. does provide such a property, i.e.
A simple POST
request to "insert" some data does not have any of these properties. A server receiving a POST
request furthermore processes the payload according to its own semantics. The client does not know beforehand whether a resource will be created or if the server just ignores the request. In case the server created a resource the server will inform the client via the Location
HTTP response header pointing to the actual location the client can retrieve information from.
PUT
is usually used only to "update" a resource, though according to the spec it can also be used in order to create a new resource if it does not yet exist. As with POST
on a successful resource creation the PUT
response should include such a Location
HTTP response header to inform the client that a resource was created.
The POST-PUT-Creation pattern separates the creation of the URI from the actual persistence of the representation by first firing off POST
requests to the server until a response is received containing a Location
HTTP response header. This header is used in a PUT
request to actually send the payload to the server. As PUT
is idempotent the server simply can reissue the request until it receives a valid response from the server.
On sending the initial POST
request to the server, a client can't be sure whether the request reached the server and only the response got lost, or the initial request didn't make it to the server. As the request is only used to create a new URI (without any content yet) the client may simply reissue the request and in worst case just create a new URI that points to nothing. The server may have a cleanup routine that frees unused URIs after a certain amount of time.
Once the client receives the URI, it simply can use PUT
to reliably send data to the server. As long as the client didn't receive a valid response, it can just reissue the request over and over until it receives a response.
I therefore do not see the need to use a message-oriented middleware (MOM) using brokers and queues in order to guarantee reliable messaging.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With