Skip to main content

Using traits in Verticles

It's tedious to do common tasks in Verticles like registering event handlers or publishing JsonMessage based messages.

Doing this over and over again in your Verticle the code becomes bloated and unreadable.

Provide a better reading experience using traits in your Verticle.

Prerequisites

To complete this tutorial, you should be familiar with writing basic Verticles and using the JsonMessage interface from the Telestion APIs.

What you'll build

Base Code »/application/tutorials/using-jsonmessage/

Based on the code from the JsonMessage tutorial, you'll add traits to your MessageTransformer Verticle.

For a concept on what a trait actually is, please take a look on this concept:

Traits concept »/application/concepts/traits/

After the tutorial, your Verticle looks like this:

MessageTransformer.java
package de.wuespace.telestion.project.example;

import de.wuespace.telestion.api.verticle.NoConfiguration;
import de.wuespace.telestion.api.verticle.TelestionVerticle;
import de.wuespace.telestion.api.verticle.trait.WithEventBus;

public class MessageTransformer extends TelestionVerticle<NoConfiguration>
implements WithEventBus {

public static void main(String[] args) throws InterruptedException {
// [...]
}

@Override
public void onStart() {
register("input", message -> {
int received = message.value();
publish("output", new IntegerMessage(received * 2));
}, IntegerMessage.class);
}
}

Step 1: Add a trait to your Verticle

The Telestion API already provides a lot of traits for your Verticles.

List of Telestion Core Verticle traits »https://javadoc.io/doc/de.wuespace.telestion/telestion-api/latest/de/wuespace/telestion/api/verticle/trait/package-summary.html

Import the WithEventBus trait and use it in your MessageTransformer Verticle:

MessageTransformer.java
package de.wuespace.telestion.project.example;

import de.wuespace.telestion.api.verticle.NoConfiguration;
import de.wuespace.telestion.api.verticle.TelestionVerticle;
import de.wuespace.telestion.api.verticle.trait.WithEventBus;

public class MessageTransformer extends TelestionVerticle<NoConfiguration>
implements WithEventBus {

public static void main(String[] args) throws InterruptedException {
// [...]
}

@Override
public void onStart() {
// [...]
}
}

The WithEventBus trait contains a bunch of methods to interact with the EventBus.

For more information, take a look at the API reference:

WithEventBus API reference »https://javadoc.io/doc/de.wuespace.telestion/telestion-api/latest/de/wuespace/telestion/api/verticle/trait/WithEventBus.html

Step 2: Rewrite the event bus consumer

Right now, the Verticle extracts the event bus instance from the vertx context and registers an event handler and an event bus channel. The incoming message is then converted to an IntegerMessage if possible and another event handler gets called which multiplies the received value.

That's a tedious task if you want to consume messages on more than one channel throughout your Verticle.

Use the register method from the WithEventBus:

MessageTransformer.java
package de.wuespace.telestion.project.example;

import de.wuespace.telestion.api.verticle.NoConfiguration;
import de.wuespace.telestion.api.verticle.TelestionVerticle;
import de.wuespace.telestion.api.verticle.trait.WithEventBus;

public class MessageTransformer extends TelestionVerticle<NoConfiguration>
implements WithEventBus {

public static void main(String[] args) throws InterruptedException {
// [...]
}

@Override
public void onStart() {
var eb = vertx.eventBus();

register("input", message -> {
// [...]
}, IntegerMessage.class);
}
}

The register method combines several operations in one call. First, it registers an event handler on the event bus channel "input". If a messages arrives at the channel the register method tries to convert received message to the specified message type (here IntegerMessage.class). If the conversion succeeded, register calls the given event handler with the converted message.

In short, it has the same behaviour as the existing implementation but is much more readable and maintainable.

Step 3: Rewrite the event bus publisher

To remove the eb event bus reference, you can use the publish method provided by the WithEventBus trait:

MessageTransformer.java
package de.wuespace.telestion.project.example;

import de.wuespace.telestion.api.verticle.GenericConfiguration;
import de.wuespace.telestion.api.verticle.TelestionVerticle;
import de.wuespace.telestion.api.verticle.trait.WithEventBus;

public class MessageTransformer extends TelestionVerticle<GenericConfiguration>
implements WithEventBus {

public static void main(String[] args) throws InterruptedException {
// [...]
}

@Override
public void onStart() {
register("input", message -> {
int received = message.value();
publish("output", new IntegerMessage(received * 2));
}, IntegerMessage.class);
}
}

The publish is essentially a shorthand to access the vertx.eventBus().publish() method.

One neat feature of publish is that it does the conversion from a JsonMessage based message back to the JsonObject automatically. Thus, you don't need to call .json() on the message anymore (as described in Using JsonMessage).

Next steps

That's it. You've used traits in your first Verticle. 🍹

It doesn't look like much of an improvement right now, but it cleans up your code in bigger and more complex Verticles.

But that's not the end. The Telestion Core provides more traits for Verticles. Take a look at them if you like.

You can also write your own traits not only for Verticles but much more types.

Traits concept »/application/concepts/traits/List of Telestion Core Verticle traits »https://javadoc.io/doc/de.wuespace.telestion/telestion-api/latest/de/wuespace/telestion/api/verticle/trait/package-summary.htmlWithEventBus API reference »https://javadoc.io/doc/de.wuespace.telestion/telestion-api/latest/de/wuespace/telestion/api/verticle/trait/WithEventBus.htmlWriting your own traits »/application/guides/writing-your-own-traits/