/**
 * JBoss, Home of Professional Open Source
 * Copyright 2016, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.redhat.developers.msa.goodbye;

import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ServerVerticle extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        Router router = Router.router(vertx);
        // Goodbye EndPoint
        router.get("/api/goodbye").handler(ctx -> ctx.response().end(goodbye()));
        // Nap  Endpoint
        router.get("/api/nap").handler(ctx -> {
            vertx.<String>executeBlocking(fut -> fut.complete(nap()), false, ar -> {
                if (ar.succeeded()) {
                    ctx.response().end(ar.result());
                } else {
                    ctx.fail(ar.cause());
                }
            });
        });

        vertx.createHttpServer().requestHandler(router::accept).listen(8080);
        System.out.println("Service running at 0.0.0.0:8080");
    }

    private String nap() {
        System.out.println("Received request on Thread: " + Thread.currentThread().getName());
        Pi.computePi(20000);
        /*
        try {
            // Sleep 30 seconds
            // Thread.sleep(30000);
            
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        */
        System.out.println("Back from the nap: " + Thread.currentThread().getName());
        return "Nap from " + new Date().toString();
    }

    private String goodbye() {
        String msg = "Goodbye on " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS").format(new Date()); 
        System.out.println(msg);
        return msg;
    }


}

@@ -911,16 +911,56 @@
 .Router;
+%0Aimport io.vertx.ext.web.RoutingContext;
 %0A%0Aimport
@@ -1341,311 +1341,40 @@
 p%22).
-handler(ctx -%3E %7B%0A            vertx.%3CString%3EexecuteBlocking(fut -%3E fut.complete(nap()), false, ar -%3E %7B%0A                if (ar.succeeded()) %7B%0A                    ctx.response().end(ar.result());%0A                %7D else %7B%0A                    ctx.fail(ar.cause());%0A                %7D%0A            %7D);%0A        %7D
+blockingHandler(this::nap, false
 );%0A%0A
@@ -1537,19 +1537,35 @@
 ate 
-String nap(
+void nap(RoutingContext ctx
 ) %7B%0A
@@ -2032,23 +2032,35 @@
         
-return 
+ctx.response().end(
 %22Nap fro
@@ -2086,16 +2086,17 @@
 String()
+)
 ;%0A    %7D%0A

simplify blocking code