Long running period web service

Asked

Viewed 406 times

1

I am creating a webservice that makes consult in a DW these queries for having functions of aggregations and everything else take a little more time than normal (something around 2 - 5 minutes), however the client does not wait for this query to end it understands as timeout the response due to time. would like to know if have how I inform the client that the consultation will take and how to do this, below the technologies I am using.

javax, webservice restful,

my code is something similar to that:

@GET
@Path("/QtdeVendasNoPeriodo/{dtInicio}/{dtFim}")
@Produces("application/json; charset=UTF-8;")
public ArrayList<DetalhesVenda> QtdeVendasNoPeriodo(@PathParam("dtInicio") Date dtInicio,@PathParam("dtFim") Date dtFim){

// Logica do metodo
}

before testing the webservice is impossible because it takes time and the applications I know to test also understand how timeout the response time.

  • What does DW mean?

  • porem o client nao espera what is this client ? is a screen ? another application ? the most important thing is you tell which client is this kkk

  • is an application, an application that will receive from this web service a json more or less in this format { January: 10, February: 1000, March: 6000000, . . December: 525 }

  • DW stands for Data Warehouse

  • It seems your question is duplicated here https://answall.com/questions/214371/webmetodo-timeout-exception?rq=1

1 answer

1

For the customer to know how long it will take, you need to send an answer. This type of problem can be adequately addressed using an asynchronous approach. There are advantages to doing this. For example, in case of high demand, you can queue requests to your DW and not leave "locked" threads on your server. In addition, customers do not need to change connection timeouts to their REST service. Certainly the complexity of implementation increases a little, but it is worth you have a more elegant and scalable solution.

My suggestion is that you create a service POST /QtdeVendasNoPeriodo/{dtInicio}/{dtFim}. Upon receiving the request, you submit the report generation to a thread worker and immediately respond to the client with HTTP code 202 Accepted and the HTTP header Location with the address where the report generation status will be consulted. The response to service POST would be something like:

HTTP/1.1 202 Accepted
Location: https://<seu_endereco_web>/filaProcessamento/sdf4524wrwerwe

sdf4524wrwerwe is any identifier that uniquely informs the status of the report generation. /filaProcessamento/{idStatusProcessamento} is a service GET where you would answer, for example:

http GET https://<seu_endereco_web>/filaProcessamento/sdf4524wrwerwe
HTTP/1.1 200 Ok

<resposta>
    <status>PENDENTE</status>
    <tempoRestante>120 segundos</tempoRestante>
    <link rel="cancel" method="delete" href="/filaProcessamento/sdf4524wrwerwe" />
</resposta>

I used XML above, but it might be JSON. The tag link would be to inform the user the possibility to cancel the generation of the report. In this case, a service DELETE it would be necessary.

Once the report has been created, the GET response described above would be

http GET https://<seu_endereco_web>/filaProcessamento/sdf4524wrwerwe
HTTP/1.1 303 See Other
Location: https://<seu_endereco_web>/QtdeVendasNoPeriodo/97525252665

The HTTP code 303 is for client redirection. The header Location tells the customer where the feature is available. It is necessary that you implement a service GET /QtdeVendasNoPeriodo/{idResultado}, where idResultado is an identifier any of the report expected by the client, which will also be controlled by its application.

Some implementation details need to be decided according to your need. For example, in response to service GET /QtdeVendasNoPeriodo/{idResultado}, inform in the header Expires the time the report can be cached. Enter consistent HTTP codes such as 404 Not Found, if the report does not exist, or 410 Gone after the customer has already downloaded the report or its application has discarded the data after a certain period of time. Finally, several improvements can be considered.

Safety tip: the idResultado can be, for example, a large random number or a hash. Thus, an average customer has a low probability of being able to access other customers' results through service queries GET /QtdeVendasNoPeriodo/{idResultado}. If you use a sequence, for example, a customer may have access to other results only by increasing idResultado.

  • face mto thanks for the help already gives me a direction to look, I know I may be being a little more insistent than I should, but is there any tutorial that shows how to do this ? For example, how do I inform the client with this 202 code and tell him where he should get the result? by what I understood I would have to create 3 or 4 more methods to treat this situation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.