Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to support batch web api request processing using Spring/Servlets

We have our Web API written in using RESTEasy. We would like to provide support for Batch requests processing the way Google Batch request processing works.

Following is the approach which are using currently,

We have a filter which accepts incoming multipart request. This filter then creates multiple mock requests and response objects and then calls chain.doFilter using these mock requests.

public class BatchRequestProcessingFilter extends GenericFilterBean {

  @Override
  public void doFilter(ServletRequest req, ServletResponse res,
      FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest)req;
            MockHttpServletRequest[] mockRequests = BatchRequestProcessorUtils.parseRequest(request);
            MockHttpServletResponse[] mockResponses = new MockHttpServletResponse[mockRequests.length];
            for(int i=0 ; i <= mockRequests.length ; i++  ) {
                chain.doFilter(mockRequests[i], mockResponses[i], chain);
            }
            BatchRequestProcessingUtils.populateResponseFromMockResponses(res, mockResponses);
      }

}

MockHttpServletResponse class returns a dummy OutputStream which wraps ByteArrayOutputStream.

BatchRequestProcessorUtils parses the multipart request and returns the mock request which wraps actual request but returns the header specified in split body of the actual request body.

I could not find any existing library which supports batch request processing. So my question is that, is this the correct approach to support batch request or is there any standard way which should be used?

Note that we are using Tomcat 8.

like image 529
Sachin Gorade Avatar asked May 28 '16 15:05

Sachin Gorade


People also ask

What is Spring Batch processing?

Spring Batch provides reusable functions that are essential in processing large volumes of records, including logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management.

What is batch processing in spring boot?

The spring boot batch chunk aids in the configuration of the execution. Spring Boot Batch includes reusable functions such as logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management that are necessary when processing large volumes of records.


1 Answers

Sachin Gorade. I have not heard about such libraries, but I think your approach is reasonable. If I had to solve that problem, I would think like this:

  1. In our HTTP servlets we can process requests only separately, and it is the reason why we should wrap all requests, that we want to send, into another single request at client side.
  2. As on our server side we have only one request, then we should unwrap all requests we have put into it. And, because we dont know how to process each request in our batch mechanizm - we shold send it through all filters/servlets. Also it is a reason to put our batch filter at the first position in the order.
  3. Eventually, when all requests has been processed, we should send a response back to the client. And again, to do that we should wrap all responses into a single one.
  4. At the client side we should unwrap responses and send each of that to some objects, that can process it.

In my oponion there should be two mechanizms:

  1. Batch sender for client side, that is responsible for collecting and wrapping requests, unwrapping responses and sending them to theirs processors(methods that process regular responses).
  2. Batch processor for server side, that is responsible for unwrapping requests, and collecting and wrapping responses.

Of course, that two parts may be coupled (i.g. to have shared "Wrapper" module), because objects we must be wrapped and unwrapped in the same way.

Also, if I worked on it, I would try to develop the client side mechanizm like a decorator upon a class that I use to send regular requests. In that case, I would be able to substitute regular/batch mode anytime I need to do it.

Hope my opinion is helpful for you.

like image 195
Aleksandr Semyannikov Avatar answered Nov 05 '22 05:11

Aleksandr Semyannikov