Monday, May 23, 2016

Vert.x EventBus Implementation

I came across Vert.x when I was looking for lightweight, easy to use event driven framework. Vert.x is a polyglot event-driven application framework that runs on the Java Virtual Machine. Tim Fox creator of Vert.x inspired by node.js.

Vert.x has modular hierarchy, most of the features presented with vertx-core jar. If you need web, data access, integration, authentication you can add necessary jar to the dependency. It also supports Java, JavaScript, Ruby, Groovy, Ceylon.

I want to show basic eventbus implementation with vert.x. You can find the codes at my github repo

first we need to add vert.x core as a dependency.

    io.vertx
    vertx-core
    3.2.0


We need to create a global Vertx object and EventBus.
Now we can create our consumer which will listen for an event, in this case "customer" event
public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    EventBus eventBus = vertx.eventBus();

    eventBus.consumer("customer").handler(message -> {
    System.out.println("I have received a message: " + message.body());
        message.reply("how interesting");
    });

    CustomerService customerService = new CustomerService();
    customerService.get("Josh", eventBus);
    customerService.get("Nathan", eventBus);

}

Consumer also replies with a "how interesting" message after it receives a message.

Let's write part which creates the messages.

public class CustomerService {

private List<Customer> customerList;

public CustomerService() {
    customerList = new ArrayList<>();
    customerList.add(new Customer(1l, "Josh"));
    customerList.add(new Customer(2l, "David"));
    customerList.add(new Customer(3l, "Peter"));
}


public Customer get(String name, EventBus eventBus) {
    
    System.out.println("searching for a customer " + name);
    List<Customer> customerList = this.customerList.stream().filter(c -> c.getName().equals(name)).collect(Collectors.toList());
    if (customerList.isEmpty()) {
        eventBus.send("customer", "customer not found", ar -> {
            if (ar.succeeded()) {
                System.out.println("Received reply: " + ar.result().body());
            }
        });
        return null;
    }

    eventBus.publish("customer", "yay found customer");

    return customerList.get(0);
}


When a customer has found "yay found customer" message thrown. If customer not found then "customer not found" message thrown and wait for an answer which is "how interesting" in this case.

searching for a customer Josh
searching for a customer Nathan
I have received a message: yay found customer
I have received a message: customer not found
Received reply: how interesting

As you can see from the output messages is not synchronous because vert.x is asynchronous and process is not blocked so it delivered the 2 messages then replies come. In synchronous application output would be like below

searching for a customer Josh
I have received a message: yay found customer
searching for a customer Nathan
I have received a message: customer not found
Received reply: how interesting

Like they say at documentation Vert.x is easy and fun to learn. If you're interested just follow the documentation or code examples at