-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
213 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ananto.asyncgateway; | ||
|
||
/** | ||
* @author Azizul Haque Ananto | ||
* @since 2/4/21 | ||
*/ | ||
|
||
|
||
public enum Constants { | ||
ASYNC_RESULT_EVENT("ASYNC_RESULT_EVENT"), | ||
ASYNC_CALLBACK_IDENTIFIER("id"); | ||
|
||
public final String val; | ||
|
||
Constants(String val) { | ||
this.val = val; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ananto/asyncgateway/handlers/CallbackHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ananto.asyncgateway.handlers; | ||
|
||
import com.ananto.asyncgateway.Constants; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* @author Azizul Haque Ananto | ||
* @since 2/4/21 | ||
*/ | ||
public class CallbackHandler implements Handler<RoutingContext> { | ||
@Override | ||
public void handle(RoutingContext routingContext) { | ||
var body = routingContext.getBodyAsJson(); | ||
routingContext.vertx().eventBus().publish(Constants.ASYNC_RESULT_EVENT.val, body); | ||
routingContext.response().end("OK"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/ananto/asyncgateway/handlers/CommonAsyncHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.ananto.asyncgateway.handlers; | ||
|
||
import com.ananto.asyncgateway.store.Store; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* @author Azizul Haque Ananto | ||
* @since 2/4/21 | ||
*/ | ||
public abstract class CommonAsyncHandler implements Handler<RoutingContext> { | ||
|
||
abstract String handleRequest(RoutingContext routingContext); | ||
|
||
@Override | ||
public void handle(RoutingContext routingContext) { | ||
HttpServerResponse response = routingContext.response(); | ||
response.putHeader("content-type", "application/json"); // Please remove this if all responses are not json | ||
var id = handleRequest(routingContext); | ||
Store.requests.put(id, response); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/ananto/asyncgateway/handlers/ExampleHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.ananto.asyncgateway.handlers; | ||
|
||
import com.ananto.asyncgateway.Constants; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* @author Azizul Haque Ananto | ||
* @since 2/4/21 | ||
*/ | ||
public class ExampleHandler extends CommonAsyncHandler { | ||
|
||
@Override | ||
String handleRequest(RoutingContext routingContext) { | ||
return routingContext.pathParam(Constants.ASYNC_CALLBACK_IDENTIFIER.val); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.ananto.asyncgateway.store; | ||
|
||
import io.vertx.core.http.HttpServerResponse; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* @author Azizul Haque Ananto | ||
* @since 2/4/21 | ||
*/ | ||
public class Store { | ||
|
||
// keep track of the requests (actually response), to send them later when arrives in callback | ||
public static ConcurrentHashMap<String, HttpServerResponse> requests = new ConcurrentHashMap<>(); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/ananto/asyncgateway/verticles/AsyncVerticle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.ananto.asyncgateway.verticles; | ||
|
||
import com.ananto.asyncgateway.Constants; | ||
import com.ananto.asyncgateway.store.Store; | ||
import io.vertx.core.AbstractVerticle; | ||
import io.vertx.core.json.JsonObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author Azizul Haque Ananto | ||
* @since 2/4/21 | ||
*/ | ||
|
||
public class AsyncVerticle extends AbstractVerticle { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AsyncVerticle.class); | ||
|
||
@Override | ||
public void start() throws Exception { | ||
vertx.eventBus().consumer(Constants.ASYNC_RESULT_EVENT.val).handler(data -> { | ||
var body = (JsonObject) data.body(); | ||
var id = body.getString(Constants.ASYNC_CALLBACK_IDENTIFIER.val); | ||
logger.info(String.format("Event received | %s : %s%n", id, body)); | ||
var request = Store.requests.remove(id); | ||
if (request != null) request.end(body.toBuffer()); | ||
}); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/ananto/asyncgateway/verticles/WebVerticle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.ananto.asyncgateway.verticles; | ||
|
||
import com.ananto.asyncgateway.handlers.CallbackHandler; | ||
import com.ananto.asyncgateway.handlers.ExampleHandler; | ||
import io.vertx.core.AbstractVerticle; | ||
import io.vertx.core.Promise; | ||
import io.vertx.core.http.HttpServer; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.handler.BodyHandler; | ||
import io.vertx.ext.web.handler.LoggerHandler; | ||
import io.vertx.ext.web.handler.TimeoutHandler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author Azizul Haque Ananto | ||
* @since 2/4/21 | ||
*/ | ||
public class WebVerticle extends AbstractVerticle { | ||
|
||
private static final int PORT = 8888; | ||
private static final long DEFAULT_REQUEST_TIMEOUT = 10000L; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(WebVerticle.class); | ||
|
||
@Override | ||
public void start(Promise<Void> startPromise) throws Exception { | ||
|
||
HttpServer server = vertx.createHttpServer(); | ||
|
||
Router router = Router.router(vertx); | ||
router.route().handler(LoggerHandler.create()); | ||
router.route().handler(BodyHandler.create()); | ||
router.route().handler(TimeoutHandler.create(DEFAULT_REQUEST_TIMEOUT)); // change default timeout to 10sec | ||
router.route().failureHandler(ctx -> { | ||
logger.error(ctx.failure().getMessage(), ctx.failure()); | ||
ctx.next(); | ||
}); | ||
|
||
router.get("/health").handler(ctx -> { | ||
HttpServerResponse response = ctx.response(); | ||
response.end("OK"); | ||
}); | ||
|
||
router.get("/async/:id").blockingHandler(new ExampleHandler()); | ||
router.post("/callback").handler(new CallbackHandler()); | ||
|
||
server.requestHandler(router).listen(PORT, http -> { | ||
if (http.succeeded()) { | ||
startPromise.complete(); | ||
logger.info("HTTP server started on port " + PORT); | ||
} else { | ||
startPromise.fail(http.cause()); | ||
} | ||
}); | ||
|
||
} | ||
|
||
} |