Search in sources :

Example 1 with Mix

use of co.paralleluniverse.strands.channels.Mix in project vertx-examples by vert-x3.

the class Combine method start.

@Suspendable
@Override
public void start() throws Exception {
    EventBus eb = vertx.eventBus();
    // Create a couple of consumers on different addresses
    // The adaptor allows handler to be used as a Channel
    HandlerReceiverAdaptor<Message<String>> adaptor1 = streamAdaptor();
    eb.<String>consumer(ADDRESS1).handler(adaptor1);
    HandlerReceiverAdaptor<Message<String>> adaptor2 = streamAdaptor();
    eb.<String>consumer(ADDRESS2).handler(adaptor2);
    // Set up a periodic timer to send messages to these addresses
    vertx.setPeriodic(500, tid -> {
        eb.send(ADDRESS1, "wibble");
        eb.send(ADDRESS2, "flibble");
    });
    ReceivePort<Message<String>> channel1 = adaptor1.receivePort();
    ReceivePort<Message<String>> channel2 = adaptor2.receivePort();
    // Combine them into a single channel
    // Not sure how to avoid this ugly cast with Quasar
    Mix<Message<String>> mix = (Mix<Message<String>>) Channels.mix(channel1, channel2);
    // Take the first ten
    for (int i = 0; i < 10; i++) {
        Message<String> msg = mix.receive();
        System.out.println("got message: " + msg.body());
    }
    System.out.println("done");
}
Also used : Message(io.vertx.core.eventbus.Message) EventBus(io.vertx.core.eventbus.EventBus) Mix(co.paralleluniverse.strands.channels.Mix) Suspendable(co.paralleluniverse.fibers.Suspendable)

Aggregations

Suspendable (co.paralleluniverse.fibers.Suspendable)1 Mix (co.paralleluniverse.strands.channels.Mix)1 EventBus (io.vertx.core.eventbus.EventBus)1 Message (io.vertx.core.eventbus.Message)1