Search code examples
rubyjrubyvert.xevent-bus

Can't use Vertx EventBus from official code example on Vertx.io


I have 2 very simple Vertx verticles written in Ruby:

### sender.rb
require 'vertx/vertx'
require 'date'
vertx = Vertx::Vertx.vertx()
event_bus = vertx.event_bus()
vertx.set_periodic(2000) { |v| 
msg = "Ruby NOW " + DateTime.now.strftime("%H:%M:%S")
puts(msg)
event_bus.publish("news.uk.sport", msg)
}

and

### listener.rb
require 'vertx/vertx'
vertx = Vertx::Vertx.vertx()
event_bus = vertx.event_bus()
consumer = event_bus.consumer("news.uk.sport")
consumer.handler() { |message|
puts "I have received a message: #{message.body()}"
}
consumer.completion_handler() { |res_err,res|
if (res_err == nil)
puts "The handler registration has reached all nodes"
else
puts "Registration failed!"
end
}

I have constructed these two verticles exactly from the code examples from vertx.io documentation.

I open 2 terminal sessions. In 1st terminal I deploy the sender.rb verticle:

$ vertx run sender.rb

in 2nd terminal i deploy listener.rb verticle:

$ vertx run listener.rb

The sender sends and prints messages, but listener, doesn't receive anything (no print output from listener.rb)

I have tried to run vertx with flags

-ha
and

-cluster

but it didn't help. Please help.

UPDATE

Many thanks to Tsegismont I have re-implemented both the listener.rb and the sender.rb and it worked :)

Now i just have little question purely out of curiosity:

In my sender:

     simple_eventbus git:(master) ✗ vertx run sender.rb -cluster
Starting clustering... 
No cluster-host specified so using address 192.168.7.179 
Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 2071 ms, time limit is 2000 
Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 3073 ms, time limit is 2000 
You're already on a Vert.x context, are you sure you want to create a new Vertx instance? 
Succeeded in deploying verticle 
On Sender site: We now have a clustered event bus: #<Vertx::EventBus:0x61deddf7>
The message has been sent: 13:20:13
The message has been sent: 13:20:15
The message has been sent: 13:20:17

The sender.rb is:

require 'vertx/vertx'
require 'date'

options = {
}
Vertx::Vertx.clustered_vertx(options) { |res_err,res|
  if (res_err == nil)
    vertx = res
    event_bus = vertx.event_bus()
    puts "On Sender site: We now have a clustered event bus: #{event_bus}"
    vertx.set_periodic(2000) { |v|
      msg = "Message from Sender: ruby message NOW " + DateTime.now.strftime("%H:%M:%S")
      puts "The message has been sent: " + DateTime.now.strftime("%H:%M:%S")
      event_bus.publish("news.uk.sport", msg)
    }
  else
    puts "Failed: #{res_err}"
  end
}

and my listener.rb output:

simple_eventbus git:(master) ✗ vertx run listener.rb -cluster
Starting clustering... 
No cluster-host specified so using address 192.168.7.179 
Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 2169 ms, time limit is 2000 
Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 3172 ms, time limit is 2000 
You're already on a Vert.x context, are you sure you want to create a new Vertx instance? 
Succeeded in deploying verticle 
On listener side: We now have a clustered event bus: #<Vertx::EventBus:0x44af7bbf>
The handler registration has reached all nodes
I have received a message: Message from Sender: ruby message NOW 13:20:13
I have received a message: Message from Sender: ruby message NOW 13:20:15
I have received a message: Message from Sender: ruby message NOW 13:20:17

The listener.rb code:

require 'vertx/vertx'
options = {
}
Vertx::Vertx.clustered_vertx(options) { |res_err,res|
  if (res_err == nil)
    vertx = res
    event_bus = vertx.event_bus()
    puts "On listener side: We now have a clustered event bus: #{event_bus}"

    consumer = event_bus.consumer("news.uk.sport")
    consumer.handler() { |message|
        puts "I have received a message: #{message.body()}"
    }

    consumer.completion_handler() { |res_err,res|
        if (res_err == nil)
            puts "The handler registration has reached all nodes"
        else
            puts "Registration failed!"
        end
    }
  else
    puts "Failed: #{res_err}"
  end
}

In my sender.rb the eventBus object assigned is:

#<Vertx::EventBus:0x61deddf7>

In my listener.rb it is different:

#<Vertx::EventBus:0x44af7bbf>

Is it just the object instance reference? Doesn't have to be the same object instance to be shared?

And another question is why does it tell me onDeploy this?

Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 3073 ms, time limit is 2000 
You're already on a Vert.x context, are you sure you want to create a new Vertx instance? 

I have run the code once: vertx run listener.rb -cluster

Why does it tell me that I am already in Vertx context?


Solution

  • The problem is that your code creates a standalone Vert.x instance. To create a clustered Vert.x instance, follow this example:

    require 'vertx/vertx'
    options = {
    }
    Vertx::Vertx.clustered_vertx(options) { |res_err,res|
      if (res_err == nil)
        vertx = res
        eventBus = vertx.event_bus()
        puts "We now have a clustered event bus: #{eventBus}"
      else
        puts "Failed: #{res_err}"
      end
    }
    

    from the Vert.x Event Bus clustering documentation section.