package examples.async;

import com.vtence.molecule.WebServer;
import com.vtence.molecule.middlewares.Failsafe;

import java.io.IOException;
import java.util.concurrent.CompletionException;

import static java.util.concurrent.CompletableFuture.runAsync;

public class AsyncExample {

    public void run(WebServer server) throws IOException {
        // Capture internal server errors and display a 500 page
        server.add(new Failsafe());
        server.start((request, response) -> {
            // Serve response from a separate thread, simulating an async long running process
            runAsync(() -> {
                aLongRunningProcess(500);

                response.done("After waiting for a long time...");
            });
        });
    }

    private void aLongRunningProcess(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            throw new CompletionException(e);
        }
    }


    public static void main(String[] args) throws IOException {
        AsyncExample example = new AsyncExample();
        // Run the default web server
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri());
    }
}
@@ -59,57 +59,8 @@
 ver;
-%0Aimport com.vtence.molecule.middlewares.Failsafe;
 %0A%0Aim
@@ -203,196 +203,715 @@
 c;%0A%0A
-public class AsyncExample %7B%0A%0A    public void run(WebServer server) throws IOException %7B
+/**%0A * %3Cp%3E%0A *     In this example we serve responses from a different thread than the request servicing thread.%0A *     We need to be careful not to block the request servicing thread if request processing takes some time to complete.
 %0A 
+*
      
-  // Capture internal server errors and display a 500 p
+In this case it is better to run the time consuming process asynchronously and write the response when this%0A *     process completes.%0A * %3C/p%3E%0A * %3Cp%3E%0A *     This could happen for instance if we are dependent on some remote server to complete a task, or some mess
 age%0A 
+*
      
-  server.add(new Failsafe());
+to arrive. Below is a very simple and rather contrived example of how this can be implemented.%0A * %3C/p%3E%0A */%0Apublic class AsyncExample %7B%0A%0A    public void run(WebServer server) throws IOException %7B
 %0A   
@@ -968,17 +968,24 @@
      // 
-S
+We can s
 erve res
@@ -989,16 +989,32 @@
 response
+s asynchronously
  from a 
@@ -1034,120 +1034,489 @@
 ad, 
-simulating an async long runn
+without blocking I/O. We'll use%0A            // the common fork-jo
 in
-g
  p
-rocess%0A            runAsync(() -%3E %7B%0A                aLongRunningProcess(500);%0A
+ool to run a task that takes 500ms to complete.%0A            runAsync(() -%3E %7B%0A                // To simulate a long running process...%0A                aLongRunningProcess(500);%0A%0A                // When the task completes, a call to done triggers completion of the response.%0A                // Processing of the middleware pipeline resumes and then server writes the response back%0A                // to the client.
 %0A   
@@ -1819,17 +1819,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -2121,8 +2121,9 @@
 %0A    %7D%0A%7D
+%0A

Add inline doc to the asynchronous example