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());
}
@Override
public 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 {
@Override
public 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 {
@Override
public 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,现在我们考虑一下不在本地的情况,使用节点管理器