0

I am implementing a RESTfull web service with Java, which should compute every call in a reliable way, even if the systems crashes. After a potential failure, the system should be able to find all jobs which were open at the time the system crashed and proceed with the computation where the system crashed.

What is the best way of achieving that task? Is JMS the correct direction to go?

Hans Sperker
  • 1,347
  • 2
  • 15
  • 37
  • Could you tell us more about jobs? – mkrakhin Dec 09 '14 at 12:54
  • JMS would be a replacement for doing RESTful calls - it is a different method of inter-process communication. That is not what you are after. Likely what triggered you to mention it is because JMS supports message persistence for failure recovery - and that is what you are actually after; to do something similar in a RESTful environment. Correct? – Gimby Dec 09 '14 at 12:57
  • 1
    A RESTful service uses HTTP as the communication protocol. That is a stateless protocol: there is no concept of an *open job*. Your question makes no sense. – Raedwald Dec 09 '14 at 13:08
  • See also [Mapping processing control to REST](http://stackoverflow.com/questions/19607102/mapping-processing-control-to-rest) – Raedwald Dec 09 '14 at 13:09
  • @Raedwald +1, however you can respond with 202 accepted and a link to the open job. ;-) – inf3rno Dec 09 '14 at 13:11
  • Sorry, by job I mean the intention of a caller getting a 'job' like a computation, done by the RESTful service. What if the RESTful service received the call but crashed while computing the request? How could the request be stored? – Hans Sperker Dec 09 '14 at 13:13
  • @mkrakhin, the job is the processing of the received call which may take longer and results in the call of other services in the end. – Hans Sperker Dec 09 '14 at 13:14
  • you have to register each job to some store and de-register when the job is done. you could do it asynchronously like the previous comment [202] or synchronously. in case of a crash on startup you should check the job registry to see if there exist any unfinished jobs that are not currently being processed by the system and process them accordingly.Have a look at Interceptors... – Timmo Dec 09 '14 at 13:16
  • For asynchronous, assured delivery calls use JMS not REST. You will have all you need out of the box. And how you plan to respond to the caller after the server crashes, even if you redo the computation? – Gas Dec 09 '14 at 14:40
  • @Gas, i think JMS is a good choice indeed! In the described system, only the interface to the system will answer the caller with a message like 'received'. After that the caller knows that the service is reliable and will do the job, even after a failure. – Hans Sperker Dec 09 '14 at 14:55

1 Answers1

0

You can try using RestEasy.

In RestEasy you can bind a handler before and after a REST call has been executed.

You can than store a REST call on the before handler and acknowledge the execution once finished.

If the REST was not executed ... it can be retried, or it's action repeated.

The code would look something like this (REQUEST):

@Provider
public class RestInterceptor implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext context) {

(RESPONSE)

@Provider
public class EventFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {

The question arises WHY?

REST calls should be simple atomic tasks executed in less than 30s. Plus they should be (if possible) idempotent.

If you would like to trigger and monitor long running tasks with REST calls then this is the way to go. But the job itself should be separated and monitored on some other level (not the HTTP stack).

Drejc
  • 14,196
  • 16
  • 71
  • 106
  • the REST interface is atomic. I want to store the call behind the REST interface to ensure the correct computation behind the REST interface which gets triggered from the REST interface. – Hans Sperker Dec 09 '14 at 14:08
  • Then it is probably the best to rephrase your question. I don't think that the REST interface is the right place to monitor you execution. – Drejc Dec 17 '14 at 12:40