Class List Method List File List

# Class: Vertx::EventBus

Inherits:
  Object
  - Object
  - Vertx::EventBus
  show all
Defined in:
  src/main/ruby_scripts/core/event_bus.rb

## Overview

For point to point messaging, messages can be sent to an address using the send method. The messages will be delivered to a single handler, if one is registered on that address. If more than one handler is registered on the same address, Vert.x will choose one and deliver the message to that. Vert.x will aim to fairly distribute messages in a round-robin way, but does not guarantee strict round-robin under all circumstances.

All messages sent over the bus are transient. On event of failure of all or part of the event bus messages may be lost. Applications should be coded to cope with lost messages, e.g. by resending them, and making application services idempotent.

The order of messages received by any specific handler from a specific sender should match the order of messages sent from that sender.

When sending a message, a reply handler can be provided. If so, it will be called when the reply from the receiver has been received. Reply messages can also be replied to, etc, ad infinitum.

Different event bus instances can be clustered together over a network, to give a single logical event bus.

When receiving a message in a handler the received object is an instance of EventBus::Message - this contains the actual Hash of the message plus a reply method which can be used to reply to it.

Author:

- Tim Fox

## Constant Summary

@@handler_map =

      {}

@@j_eventbus =

      org.vertx.java.deploy.impl.VertxLocator.vertx.eventBus()

## Class Method Summary (collapse)

- + (Object) publish(address, message)

  Publish a message on the event bus.

- + (FixNum) register_handler(address, local_only = false, &message_hndlr)

  Register a handler.

- + (FixNum) register_simple_handler(local_only = false, &message_hndlr)

  Registers a handler against a uniquely generated address, the address is returned as the id received by the handler.

- + (Object) send(address, message, &reply_handler)

  Send a message on the event bus It will be called when the reply from a receiver is received.

- + (Object) unregister_handler(handler_id)

  Unregisters a handler.

## Class Method Details

### + (`Object`) publish(address, message)

Publish a message on the event bus

Parameters:

- message (`Hash`) —

  The message to publish

    70
    71
    72

    # File 'src/main/ruby_scripts/core/event_bus.rb', line 70

    def EventBus.publish(address, message)
      EventBus.send_or_pub(false, address, message)
    end

### + (`FixNum`) register_handler(address, local_only = false, &message_hndlr)

Register a handler. received by the handler. A single handler can be registered against many addresses.

Parameters:

- address (`String`) —

  The address to register for. Any messages sent to that address will be

- local_only (`Boolean`) (defaults to: `false`) —

  If true then handler won’t be propagated across cluster

- message_hndlr (`Block`) —

  The handler

Returns:

- (`FixNum`) —

  id of the handler which can be used in unregister_handler

    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107

    # File 'src/main/ruby_scripts/core/event_bus.rb', line 96

    def EventBus.register_handler(address, local_only = false, &message_hndlr)
      raise "An address must be specified" if !address
      raise "A message handler must be specified" if !message_hndlr
      internal = InternalHandler.new(message_hndlr)
      if local_only
        id = @@j_eventbus.registerLocalHandler(address, internal)
      else
        id = @@j_eventbus.registerHandler(address, internal)
      end
      @@handler_map[id] = internal
      id
    end

### + (`FixNum`) register_simple_handler(local_only = false, &message_hndlr)

Registers a handler against a uniquely generated address, the address is returned as the id received by the handler. A single handler can be registered against many addresses.

Parameters:

- local_only (`Boolean`) (defaults to: `false`) —

  If true then handler won’t be propagated across cluster

- message_hndlr (`Block`) —

  The handler

Returns:

- (`FixNum`) —

  id of the handler which can be used in unregister_handler

    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124

    # File 'src/main/ruby_scripts/core/event_bus.rb', line 114

    def EventBus.register_simple_handler(local_only = false, &message_hndlr)
      raise "A message handler must be specified" if !message_hndlr
      internal = InternalHandler.new(message_hndlr)
      if local_only
        id = @@j_eventbus.registerLocalHandler(internal)
      else
        id = @@j_eventbus.registerHandler(internal)
      end
      @@handler_map[id] = internal
      id
    end

### + (`Object`) send(address, message, &reply_handler)

Send a message on the event bus It will be called when the reply from a receiver is received.

Parameters:

- message (`Hash`) —

  The message to send

- reply_handler (`Block`) —

  replyHandler An optional reply handler.

    64
    65
    66

    # File 'src/main/ruby_scripts/core/event_bus.rb', line 64

    def EventBus.send(address, message, &reply_handler)
      EventBus.send_or_pub(true, address, message, reply_handler)
    end

### + (`Object`) unregister_handler(handler_id)

Unregisters a handler

Parameters:

- handler_id (`FixNum`) —

  The id of the handler to unregister. Returned from register_handler

    128
    129
    130
    131
    132
    133

    # File 'src/main/ruby_scripts/core/event_bus.rb', line 128

    def EventBus.unregister_handler(handler_id)
      raise "A handler_id must be specified" if !handler_id
      handler = @@handler_map.delete(handler_id)
      raise "Cannot find handler for id #{handler_id}" if !handler
      @@j_eventbus.unregisterHandler(handler_id)
    end