Hello people
This is my baby steps with Scala and Vert.x
I'm trying to create a microservice sample scala project "from scratch" with the Redis backend
Almost all is ok:
I deploy the ScalaVerticle with a companion object:
object BeeSlack extends App {
val vertx = Vertx.vertx
// do some stuff, initialize discovery backend, etc...
vertx
.deployVerticleFuture(ScalaVerticle.nameForVerticle[brain.BeeSlack])
.onComplete {
case Success(verticleId) => println(s"Successfully deployed verticle: $verticleId")
case Failure(error) => println(s"Failed to deploy verticle: $error")
}
}
this is my verticle:
class BeeSlack extends ScalaVerticle {
override def startFuture(): Future[_] = {
val server = vertx.createHttpServer()
val router = Router.router(vertx)
val httpPort = sys.env.getOrElse("PORT", "8080").toInt
// publish the microservice record
BeeSlack.discovery.publishFuture(BeeSlack.record).onComplete{
case Success(result) => println(s"😃 publication OK")
case Failure(cause) => println(s"😡 publication KO: $cause")
}
router.route().handler(BodyHandler.create)
router.get("/hey").handler(context =>
context
.response
.putHeader("content-type", "application/json;charset=UTF-8")
.end(new JsonObject().put("message", "👋 hey!").encodePrettily)
)
router.route("/*").handler(StaticHandler.create)
println(s"🌍 Listening on $httpPort - Enjoy 😄")
server.requestHandler(router.accept _).listenFuture(httpPort)
}
}
I would like to unpublish the record verticle from the Redis backend when I quit
with Java I do like that
public void stop(Future<Void> stopFuture) {
System.out.println("Unregistration process is started ("+record.getRegistration()+")...");
discovery.unpublish(record.getRegistration(), asyncResult -> {
if(asyncResult.succeeded()) {
System.out.println("👋 bye bye " + record.getRegistration());
} else {
System.out.println("😡 Not able to unpublish the microservice: " + asyncResult.cause().getMessage());
//asyncResult.cause().printStackTrace();
}
stopFuture.complete();
});
}
and stop method is triggered when I quit
but I can't do the same thing with Scala and with the
I try something like that with override def stopFuture(): Future[_] = {...}
but I can't trigger anything (again: this is my baby steps 😉)
override def stopFuture(): Future[_] = {
var unpublishRecordFuture = BeeSlack.discovery.unpublishFuture(BeeSlack.record.getRegistration)
unpublishRecordFuture.onComplete {
case Success(result) => {
println(s"😃 removing publication OK")
}
case Failure(cause) => {
println(s"😡 removing publication KO: $cause")
}
}
unpublishRecordFuture
}
It's probably easy, but I don't find a solution.
Does anybody know an example about this or has an idea?
Regards
Philippe