@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)); }); } }
@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()); }); }); } }
/** * The same as <code>reply(R message, DeliveryOptions)</code> but you can specify handler for the reply - i.e. * to receive the reply to the reply. * @param message the reply message * @param options the delivery options * @return */ public <R> Single<io.vertx.rxjava.core.eventbus.Message<R>> rxReply(Object message, DeliveryOptions options) { return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> { reply(message, options, fut); })); }
/** * The same as <code>reply(R message)</code> but you can specify handler for the reply - i.e. * to receive the reply to the reply. * @param message the message to reply with. * @return * @deprecated use {@link #rxReply} instead */ @Deprecated() public <R> Observable<io.vertx.rxjava.core.eventbus.Message<R>> replyObservable(Object message) { io.vertx.rx.java.ObservableFuture<io.vertx.rxjava.core.eventbus.Message<R>> replyHandler = io.vertx.rx.java.RxHelper.observableFuture(); reply(message, replyHandler.toHandler()); return replyHandler; }
/** * The same as <code>reply(R message, DeliveryOptions)</code> but you can specify handler for the reply - i.e. * to receive the reply to the reply. * @param message the reply message * @param options the delivery options * @return * @deprecated use {@link #rxReply} instead */ @Deprecated() public <R> Observable<io.vertx.rxjava.core.eventbus.Message<R>> replyObservable(Object message, DeliveryOptions options) { io.vertx.rx.java.ObservableFuture<io.vertx.rxjava.core.eventbus.Message<R>> replyHandler = io.vertx.rx.java.RxHelper.observableFuture(); reply(message, options, replyHandler.toHandler()); return replyHandler; }
/** * The same as <code>reply(R message)</code> but you can specify handler for the reply - i.e. * to receive the reply to the reply. * @param message the message to reply with. * @return */ public <R> Single<io.vertx.rxjava.core.eventbus.Message<R>> rxReply(Object message) { return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> { reply(message, fut); })); }
/** * The same as <code>reply(R message)</code> but you can specify handler for the reply - i.e. * to receive the reply to the reply. * @param message the message to reply with. * @return */ public <R> Single<io.vertx.rxjava.core.eventbus.Message<R>> rxReply(Object message) { return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> { reply(message, fut); })); }
/** * The same as <code>reply(R message, DeliveryOptions)</code> but you can specify handler for the reply - i.e. * to receive the reply to the reply. * @param message the reply message * @param options the delivery options * @return */ public <R> Single<io.vertx.rxjava.core.eventbus.Message<R>> rxReply(Object message, DeliveryOptions options) { return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> { reply(message, options, fut); })); }
@Override public void start() throws Exception { vertx.eventBus().consumer("check", message -> message.reply("Check!")); } }
msg.fail(404, "invalid scroll id"); } else { msg.reply( new JsonObject() .put("totalHits", TOTAL_HITS)
/** * Register consumer for delete messages */ private void registerDelete() { vertx.eventBus().<JsonObject>consumer(AddressConstants.INDEXER_DELETE) .toObservable() .subscribe(msg -> { onDelete(msg.body()).subscribe(() -> { msg.reply(null); }, err -> { log.error("Could not delete document", err); msg.fail(throwableToCode(err), throwableToMessage(err, "")); }); }); }
/** * Register consumer for queries */ private void registerQuery() { vertx.eventBus().<JsonObject>consumer(AddressConstants.INDEXER_QUERY) .toObservable() .subscribe(msg -> { onQuery(msg.body()).subscribe(reply -> { msg.reply(reply); }, err -> { log.error("Could not perform query", err); msg.fail(throwableToCode(err), throwableToMessage(err, "")); }); }); }
discovery.getRecord(rec -> rec.getName().equalsIgnoreCase("my-http-service"), ar -> { if (ar.failed()) { message.reply("FAIL - No service"); } else { JsonObject result = new JsonObject(); ServiceReference reference = discovery.getReference(ar.result()); if (reference == null) { message.reply("FAIL - reference is null"); } else { HttpClient client = reference.getAs(HttpClient.class); reference.release(); result.put("bindings", getBindings(discovery)); message.reply(result); ar -> { if (ar.failed()) { message.reply("FAIL - no service"); } else { HttpClient client = ar.result(); ServiceDiscovery.releaseServiceObject(discovery, client); result.put("bindings", getBindings(discovery)); message.reply(result); discovery.getRecord(rec -> rec.getName().equalsIgnoreCase("my-http-service"), ar -> { if (ar.failed()) { message.reply("FAIL - No service"); } else {
msg.fail(500, client.bulkResponseItemGetErrorMessage(item)); } else { msg.reply(null);
@Test public void testConcatReplies() { EventBus eb = vertx.eventBus(); eb.<String>consumer("the-address", msg -> { msg.reply(msg.body()); }); Single<Message<String>> obs1 = eb.rxSend("the-address", "msg1"); Single<Message<String>> obs2 = eb.rxSend("the-address", "msg2"); eb.send("the-address", "done", reply -> { Observable<Message<String>> all = Single.concat(obs1, obs2); LinkedList<String> values = new LinkedList<String>(); all.subscribe(next -> { values.add(next.body()); }, err -> { fail(); }, () -> { assertEquals(Arrays.asList("msg1", "msg2"), values); testComplete(); }); }); await(); }