0

I am new to Vert.x and trying to create verticle using scala. While I am sending a message from vertx using eventbus, but message is not received by handler. Following is my code:

class HelloWorldEventBusVerticle extends ScalaVerticle {

  override def startFuture(): Future[_] = {

    vertx
      .eventBus()
      .consumer[String]("com.harmeetsingh13.verticle")
      .handler { msg =>
        println(s"Message Body ${msg.body()}")
        msg.body() match {
          case user: String => println(s"Hello $user!! message")
        }
      }
      .completionFuture()
  }
}

object HelloWorldEventBusVerticle extends App {
  val vertex = Vertx.vertx()
  vertex.deployVerticleFuture(ScalaVerticle.nameForVerticle[HelloWorldEventBusVerticle]).onComplete {
    case Success(result) => println(s"Deployment Id is : ${result}")
    case Failure(cause) => cause.printStackTrace()
  }
  vertex.eventBus()
    .send("com.harmeetsingh13.verticle", "James")
}

What is the problem with this code ?

Update

While I am adding vertex.eventBus().send("com.harmeetsingh13.verticle", "James") in Future onComplete method. In that case, the event bus received the message successfully. But when, I am using following below code, the event handler will not able to receive the message.

vertex.deployVerticle(ScalaVerticle.nameForVerticle[HelloWorldEventBusVerticle])
vertex.eventBus().send("com.harmeetsingh13.verticle", "James")
Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126
  • 2
    I suspect this is due to timing: your message is sent before the verticle is deployed. Update your example to send the message in the success callback. – tsegismont Jun 26 '17 at 12:28
  • @tsegismont In that case, code is running successfully, but know I updated by code and question as well. – Harmeet Singh Taara Jun 26 '17 at 12:29

0 Answers0