Search in sources :

Example 1 with EventBus

use of io.vertx.rxjava.core.eventbus.EventBus in project vertx-examples by vert-x3.

the class PingPong method start.

@Override
public void start() throws Exception {
    EventBus eb = vertx.eventBus();
    eb.consumer(ADDRESS).toObservable().subscribe(message -> {
        System.out.println("Received " + message.body());
        message.reply("PONG");
    });
    // Send a message every second
    vertx.setPeriodic(1000, v -> {
        eb.rxSend(ADDRESS, "PING").subscribe(reply -> {
            System.out.println("Received reply " + reply.body());
        });
    });
}
Also used : EventBus(io.vertx.rxjava.core.eventbus.EventBus)

Example 2 with EventBus

use of io.vertx.rxjava.core.eventbus.EventBus in project vertx-examples by vert-x3.

the class Receiver method start.

@Override
public void start() throws Exception {
    EventBus eb = vertx.eventBus();
    eb.consumer("news-feed").toObservable().subscribe(message -> System.out.println("Received news: " + message.body()));
    System.out.println("Ready!");
}
Also used : EventBus(io.vertx.rxjava.core.eventbus.EventBus)

Example 3 with EventBus

use of io.vertx.rxjava.core.eventbus.EventBus in project vertx-examples by vert-x3.

the class Receiver method start.

@Override
public void start() throws Exception {
    Random random1 = new Random();
    EventBus eb = vertx.eventBus();
    eb.consumer("heatsensor1").toObservable().subscribe(message -> {
        message.reply(9 + random1.nextInt(5));
    });
    eb.consumer("heatsensor2").toObservable().subscribe(message -> {
        message.reply(10 + random1.nextInt(3));
    });
}
Also used : Random(java.util.Random) EventBus(io.vertx.rxjava.core.eventbus.EventBus)

Example 4 with EventBus

use of io.vertx.rxjava.core.eventbus.EventBus in project vertx-examples by vert-x3.

the class Sender method start.

@Override
public void start() throws Exception {
    EventBus eb = vertx.eventBus();
    vertx.setPeriodic(1000, v -> {
        // Send two messages expecting replies
        Single<Message<Integer>> reply1 = eb.<Integer>rxSend("heatsensor1", "ping");
        Single<Message<Integer>> reply2 = eb.<Integer>rxSend("heatsensor2", "ping");
        // Zip responses to receive both at the same time
        Single<int[]> reply = reply1.zipWith(reply2, (msg1, msg2) -> new int[] { msg1.body(), msg2.body() });
        reply.subscribe(heats -> {
            // Print highest temp
            if (heats[0] > heats[1]) {
                System.out.println("heat sensor 1 is highest " + heats[0]);
            } else {
                System.out.println("heat sensor 2 is highest " + heats[1]);
            }
        }, err -> {
            System.out.println("Reply error:");
            err.printStackTrace();
        });
    });
}
Also used : Message(io.vertx.rxjava.core.eventbus.Message) EventBus(io.vertx.rxjava.core.eventbus.EventBus)

Example 5 with EventBus

use of io.vertx.rxjava.core.eventbus.EventBus in project rulesservice by genny-project.

the class KieTest method drlWhenConditionTest.

@Test
public void drlWhenConditionTest() {
    String rule = "";
    rule += "package org.kie.test\n";
    rule += "import java.util.Map;\n";
    rule += "global java.util.List list\n";
    rule += "rule rule2\n";
    rule += "when\n";
    rule += "  $a : Integer(intValue > 1)\n";
    rule += "  $map : Map($value: this[\"token\"] != null)\n";
    rule += "then\n";
    rule += "  System.out.println(\"value a=\"+$a);\n";
    rule += "  list.add( $a );\n";
    rule += "end\n";
    rule += "\n";
    String rulesGroup = "GRP_RULES_TEST";
    List<Tuple3<String, String, String>> rules = new ArrayList<Tuple3<String, String, String>>();
    List<Tuple2<String, Object>> globals = new ArrayList<Tuple2<String, Object>>();
    EventBus eb = null;
    rules.add(Tuple.of("genny", "rule2.drl", rule));
    List<?> list = new ArrayList<Object>();
    globals.add(Tuple.of("list", list));
    RulesLoader.setupKieRules(rulesGroup, rules);
    Map<String, KieBase> cache = RulesLoader.getKieBaseCache();
    Integer count = cache.size();
    System.out.println("Loaded Test Kie Session with " + count + " ruleGroups");
    List<Object> facts = new ArrayList<Object>();
    facts.add(1);
    facts.add(2);
    facts.add(3);
    Map<String, String> keyvalue = new HashMap<String, String>();
    keyvalue.put("token", "TOKEN");
    RulesLoader.executeStatefull(rulesGroup, eb, globals, facts, keyvalue);
    assertThat(list.size(), is(2));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventBus(io.vertx.rxjava.core.eventbus.EventBus) Tuple2(io.vavr.Tuple2) KieBase(org.kie.api.KieBase) Tuple3(io.vavr.Tuple3) Test(org.junit.Test)

Aggregations

EventBus (io.vertx.rxjava.core.eventbus.EventBus)6 Tuple2 (io.vavr.Tuple2)2 Tuple3 (io.vavr.Tuple3)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 Message (io.vertx.rxjava.core.eventbus.Message)1 Random (java.util.Random)1