vertx verticle 实例
所属分类 vertx
浏览量 270
Verticle 表示一个可部署的组件或模块,可以独立地运行在Vert.x的事件循环中
一个执行单元,它可以处理事件、响应请求、执行任务等。
每个Verticle都有自己的生命周期,可以被部署、启动、停止和卸载。
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicInteger;
public class VerticleDemo1 extends AbstractVerticle {
private static final AtomicInteger ID = new AtomicInteger(0);
private final int id = ID.getAndIncrement();
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle("dyyx.VerticleDemo1");
// 传递参数
JsonObject config = new JsonObject().put("name", "VerticleDemo1");
Future future1 = vertx.deployVerticle(new VerticleDemo1(),new DeploymentOptions().setConfig(config));
// 指定实例数
Future future2 = vertx.deployVerticle("dyyx.VerticleDemo1",new DeploymentOptions().setInstances(3));
System.out.println("now time "+LocalDateTime.now());
doSleep(2000);
// 部署已完成 获取结果
System.out.println("future1.result="+future1.result());
System.out.println("future2.result="+future2.result());
vertx.close();
}
@Override
public void start() {
doSleep(1000);
System.out.println("Verticle start done,id="+id+","+ LocalDateTime.now()+",config="+config());
}
@Override
public void stop() {
System.out.println("Verticle stop,id="+id+","+ LocalDateTime.now()+",config="+config());
}
public static void doSleep(long time){
try{
Thread.sleep(time);
}catch(Exception e){
//
}
}
}
now time 2024-01-12T22:30:52.266
Verticle start done,id=3,2024-01-12T22:30:53.259,config={}
Verticle start done,id=2,2024-01-12T22:30:53.259,config={}
Verticle start done,id=4,2024-01-12T22:30:53.260,config={}
Verticle start done,id=0,2024-01-12T22:30:53.259,config={}
Verticle start done,id=1,2024-01-12T22:30:53.260,config={"name":"VerticleDemo1"}
future1.result=db028512-0751-49b7-8c76-35ed3ad6d034
future2.result=5bad5620-5789-4286-9331-b6a93672107f
Verticle stop,id=0,2024-01-12T22:30:54.297,config={}
Verticle stop,id=3,2024-01-12T22:30:54.316,config={}
Verticle stop,id=2,2024-01-12T22:30:54.316,config={}
Verticle stop,id=4,2024-01-12T22:30:54.316,config={}
Verticle stop,id=1,2024-01-12T22:30:54.316,config={"name":"VerticleDemo1"}
上一篇
下一篇
vertx core web 实例
JAVA Void类
vertx promise future
vertx verticle 部署和卸载
项目和产品技术栈
java8 JavaScript引擎 Nashorn