-
Notifications
You must be signed in to change notification settings - Fork 3
Introduction to TCP Eventbus Bridge
Vert.x is a toolkit for the Java Virtual Machine enabling the implementation of reactive, highly concurrent, polyglot applications. Thanks to its eventbus, any application can interact with Vert.x applications.
The TCP EventBus bridge is a bridge built on top of TCP and it provides facility to interact with any other application which is written in different programming languages. The TCP EventBus bridge can be called as a server interface.
Download and install
Download Vertx from here
This provides a quick guide to install vert.x here
Implementation
Figure 1- Basic structure of TCP eventbus Bridge
Addressing
Messages are sent on the event bus to an address. It can be any string.
Handlers *
A handler is one that receives messages from the bus. A handler can be registered at an address. Many different handlers from the same or different verticles can be registered at the same address. A single handler can be registered by the verticle at many different addresses.
- When messages received, those messages will be immediately handled to the registered handlers. Further details will be discussed later.
Protocol
Frame is a LV JSON message.
<Frame length: 4 bytes (big endian encoding) >{JSON Object : utf-8}
Figure 2- Message stream from a client to the TCP eventbus bridge
Structure of a frame
<Length: 4 bytes(Big Endian)><{
type: String,
address: String,
(reply Address : String)(Optional),
headers: JsonObject,
body: JsonObject
}: JsonObject>
Type
These are the valid types for incoming messages
1. “send” – The message will be routed to just one of the handlers registered at that address. If there is more than one handler registered at the address, one will be chosen using a non-strict round-robin algorithm.
2. “publish” – The message will be routed to the all registered handlers.
3. ’“register” – register handler for the address
4. “unregister” – unregister handler for the address
These are the types that send to the clients
1. “message” – reply messages or acknowledgement messages
2. “err” – error messages
This code can be download from GitHub. here
Incoming and outgoing messages can be limited by the address of the messages.
*listen(port,handler)
This method is used to start the bridge. More information can be found here.
*MessageConsumer< JsonObject > consumer=eb.consumer(Address,Handler)
here address is a string.
Whenever a message comes to the address it will be routed to the handler. This process is called handler registration.
*message.body()
This returns the body of the message as a json object.
*message.reply(jsonObject)
If the received message has a reply address then handler will reply a frame which contains ‘jsonObject’ in the body, to reply address.
Let’s run
download the example from Github. here
Then package it using maven and run. You can find more information about packaging and running of vertx applications here .