1

I am making a restful call to the servlet doGet() method. Inside the doGet() method I am making a method call which takes a lot of time for processing due to which the doGet() method times out and cannot send a response back to the application which made the call.

So now I want to send a response back to the application which called the doGet() method immediately the doGet() is invoked.

Can I use Threads like one thread will reply back and the other one will continue with the method invocation. Do i need to use any webservice frame work like Jersey ?

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("in the Do Get");
        String tableName = (String)request.getParameter("tableName");
        tableName = tableName.replaceAll("[^A-Z0-9_]", "");
        System.out.println("setup connections");
        SampleService sampleService = new SampleService(new Properties());
        Thread t = new Thread(new Runnable() {
            public void run() {
                // stuff here
                sampleService.execute(tableName);
            }
        });
        t.start();

i am making the class to implement the Runnable interface and writing the logic of the method call(the method which takes lot of time to process) inside the run method. Also how to send a response while this one is running

  • Possible duplicate of [REST with JAX-RS - Handling long running operations](http://stackoverflow.com/questions/18787825/rest-with-jax-rs-handling-long-running-operations) – zloster Apr 06 '17 at 14:43

1 Answers1

0

You can use 2 threads so one will respond and terminated and other will do the background job. This approach can be implemented in above code or you can user framework like Jersey. The major benefit of using any frameworks is frameworks hide complexities and provide easy to use and customizable interface (the concept of abstractions)

With Jersey, you can create Asynchronous API using AsyncResponse or you can use @ManagedAsync annotation along with AsyncResponse

One example is below -

    @GET
    @ManagedAsync
    public void getMessage(@Suspended final AsyncResponse asyncResponse) {
        System.out.println("1. Registering callback");
        asyncResponse.register(new CompletionCallback() {
            @Override
            public void onComplete(Throwable throwable) {
                if (throwable == null) {
                    // no error
                    System.out.println(
                            "4. Request processing is successful");
                } else {
                    System.out.println(
                            "4. Error occurred in request processing");
                }
            }
        });
        System.out.println("2. Executing expensive operation");
        String result = expensiveOperation();
        System.out.println("3. Notify callback and send response ");
        asyncResponse.resume(result);

        System.out.println("5. Thread exiting");
    }

Sysout statements are just for reference and can be removed.

If you want to do it with Servlet directly, then create one thread, start it and return your current response -

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("in the Do Get");
        String tableName = (String)request.getParameter("tableName");
        tableName = tableName.replaceAll("[^A-Z0-9_]", "");
        System.out.println("setup connections");
        SampleService sampleService = new SampleService(new Properties());
        // this tread will run in even after response is send   
        Thread t = new Thread(new Runnable() {
            public void run() {
                // stuff here
                sampleService.execute(tableName);
            }
        });
        t.start();
        // send the response
        response.setContentType("application/json");
        resp.getWriter().write("response JSON here");
        return;
 }

You can also use Jackson for object mapping to JSON for returning in response.

Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
  • i need to do this using thread can this be done? Also i have tried a sample code snippet and edited the question. Can you take a look and help me out how this can be done –  Apr 07 '17 at 04:09