本文主要是介绍vertx的学习总结三,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、event bus是什么
各个verticle的通信
二、point-to-point, request-reply, publish/subscribe 通过 the event bus
例题一:点对点
package eventBus;import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;public class EventBusExample extends AbstractVerticle {public static void main(String[] args) {Vertx vertx = Vertx.vertx();vertx.deployVerticle(new EventBusExample());}@Overridepublic void start() {vertx.eventBus().consumer("example.address", message -> {System.out.println("Received message: " + message.body());message.reply("Replying to the message");});vertx.eventBus().send("example.address", "Hello Vert.x");}
}
例题二:请求-应答
例题三:发布订阅
Send.java
package eventBus.pointTopoint;import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.EventBus;public class Send extends AbstractVerticle {@Overridepublic void start() throws Exception {EventBus bus=vertx.eventBus();bus.publish("my.update","我怀疑是异步的,所以会sleep1000");}
}
Comsumer.java
package eventBus.pointTopoint;import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.EventBus;public class Comsumer extends AbstractVerticle {@Overridepublic void start() throws Exception {EventBus bus = vertx.eventBus();bus.consumer("my.update",str->{String sty=(String) str.body();System.out.println(sty);});}
}
main.java
package eventBus.pointTopoint;import io.vertx.core.Vertx;public class Main {public static void main(String[] args) {Vertx vertx=Vertx.vertx();vertx.deployVerticle(new Comsumer());vertx.deployVerticle(new Send());}
}
我们之前用的都是在同一个本地的jvm,现在我们考虑一下不在本地的情况,使用节点管理器
这篇关于vertx的学习总结三的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!