博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vert.x 之 HelloWorld
阅读量:4324 次
发布时间:2019-06-06

本文共 8918 字,大约阅读时间需要 29 分钟。

Hello World

欢迎来到Vert.x的世界,相信您在接触Vert.x的同时,迫不及待想动手试一试,如您在学习计算机其它知识一样,总是从Hello World开始,下面我们将引导您制作一个最基本简单的Hello World例子,但在此之前,我们需要您具备有以下基础知识:

  1. Java基础知识,您不需要了解Java EE或者是Java ME的知识,但是需要您对Java有所了解,在此文档中,我们不会介绍任何关于Java SE又称Core Java的知识点。请注意:Vert.x 3以上版本需要Java 8以上版本方能运行;

  2. Maven相关知识,您需要知道什么Maven是做什么用的,以及如何使用Maven;

  3. 互联网的基础知识,知道什么是网络协议,尤其是TCP,HTTP协议。

本文将会建立一个基本的HTTP服务器,并监听8080端口,对于任何发往该服务器以及端口的请求,服务器会返回一个Hello World字符串。

首先新建一个Maven项目,一个基本的Maven项目目录结构如下所示:

├── pom.xml├── src│   ├── main│   │   ├── java│   │   └── resources│   └── test│       └── java

 

随后在pom.xml中加入相关的依赖和插件,如下所示:

4.0.0
io.example
vertx-example
1.0-SNAPSHOT
3.4.2
io.example.Main
io.vertx
vertx-core
${vertx.version}
maven-compiler-plugin
3.3
1.8
1.8
org.apache.maven.plugins
maven-shade-plugin
2.4.2
package
shade
${main.class}
${project.build.directory}/${project.artifactId}-${project.version}-prod.jar

 

跟其它Maven项目一样,我们首先定义了项目的GroupId,ArtifactId以及版本号,随后我们定义了两个属性,分别是:vertx.version,也就是Vert.x的版本号,此处我们使用最新的Vert.x版本,也就是3.4.2;以及main.class,也就是我们要使用的包含有main函数的主类。之后我们引入了两个Maven插件,分别是maven-compiler-pluginmaven-shade-plugin,前者用来将.java的源文件编译成.class的字节码文件,后者可将编译后的.class字节码文件打包成可执行的jar文件,俗称fat-jar

然后我们在src/main/java/io/example目录下新建两个java文件,分别是Main.javaMyFirstVerticle.java,代码如下:

Main.java

package io.example;import io.vertx.core.Vertx;/** * Created by chengen on 26/04/2017. */public class Main {    public static void main(String[] args){        Vertx vertx = Vertx.vertx();        vertx.deployVerticle(MyFirstVerticle.class.getName());    }}

 

MyFirstVerticle.java

package io.example;import io.vertx.core.AbstractVerticle;/** * Created by chengen on 26/04/2017. */public class MyFirstVerticle extends AbstractVerticle {    public void start() {        vertx.createHttpServer().requestHandler(req -> {            req.response()                    .putHeader("content-type", "text/plain")                    .end("Hello World!");        }).listen(8080);    }}

 

然后用Maven的mvn package命令打包,随后在src的同级目录下会出现target目录,进入之后,会出现vert-example-1.0-SNAPSHOT.jarvert-example-1.0-SNAPSHOT-prod.jar两个jar文件,后者是可执行文件,在有图形界面的操作系统中,您可双击执行,或者用以下命令:java -jar vert-example-1.0-SNAPSHOT-prod.jar执行。

随后打开浏览器,在浏览器的地址栏中输入: 便可看到熟悉的Hello World!啦。

启动器

我们也可以使用Launcher来替代Main类,这也是官方推荐的方式,在pom.xml中加入main.verticle属性,并将该属性值设置为maven-shade-plugin插件的manifestEntriesMain-Verticle对应的值,最后修改main.classio.vertx.core.Launcher,修改后的pom.xml如下:

4.0.0
io.example
vertx-example
1.0-SNAPSHOT
3.4.2
io.vertx.core.Launcher
io.example.MainVerticle
io.vertx
vertx-core
${vertx.version}
maven-compiler-plugin
3.3
1.8
1.8
org.apache.maven.plugins
maven-shade-plugin
2.4.2
package
shade
${main.class}
${main.verticle}
${project.build.directory}/${project.artifactId}-${project.version}-prod.jar

 

然后在src/main/java/io/example目录下新增MainVerticle.java文件,代码如下:

package io.example;import io.vertx.core.AbstractVerticle;/** * Created by chengen on 26/04/2017. */public class MainVerticle extends AbstractVerticle {    public void start() {        vertx.deployVerticle(MyFirstVerticle.class.getName());    }}

 

然后重新打包后执行,便可再次看到Hello World!。

请注意:重新打包之前,您可能需要清除之前编译后留下的文件,用mvn clean package命令打包。

测试

下面我们将会介绍测试部分,首先引入两个新的测试依赖,修改后的pom.xml如下:

4.0.0
io.example
vertx-example
1.0-SNAPSHOT
3.4.2
io.vertx.core.Launcher
io.example.MainVerticle
io.vertx
vertx-core
${vertx.version}
io.vertx
vertx-unit
${vertx.version}
test
junit
junit
4.12
test
maven-compiler-plugin
3.3
1.8
1.8
org.apache.maven.plugins
maven-shade-plugin
2.4.2
package
shade
${main.class}
${main.verticle}
META-INF/services/io.vertx.core.spi.VerticleFactory
${project.build.directory}/${project.artifactId}-${project.version}-prod.jar

 

随后在src/test/java/io/example目录下新增MyFirstVerticleTest.java文件:

package io.example;import io.vertx.core.Vertx;import io.vertx.ext.unit.Async;import io.vertx.ext.unit.TestContext;import io.vertx.ext.unit.junit.VertxUnitRunner;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;/** * Created by chengen on 26/04/2017. */@RunWith(VertxUnitRunner.class)public class MyFirstVerticleTest {    private Vertx vertx;    @Before    public void setUp(TestContext context) {        vertx = Vertx.vertx();        vertx.deployVerticle(MyFirstVerticle.class.getName(), context.asyncAssertSuccess());    }    @After    public void tearDown(TestContext context) {        vertx.close(context.asyncAssertSuccess());    }    @Test    public void testApplication(TestContext context) {        final Async async = context.async();        vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {            response.handler(body -> {                context.assertTrue(body.toString().contains("Hello"));                async.complete();            });        });    }}
package io.example;import io.vertx.core.Vertx;import io.vertx.ext.unit.Async;import io.vertx.ext.unit.TestContext;import io.vertx.ext.unit.junit.VertxUnitRunner;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;/** * Created by chengen on 26/04/2017. */@RunWith(VertxUnitRunner.class)public class MyFirstVerticleTest {    private Vertx vertx;    @Before    public void setUp(TestContext context) {        vertx = Vertx.vertx();        vertx.deployVerticle(MyFirstVerticle.class.getName(), context.asyncAssertSuccess());    }    @After    public void tearDown(TestContext context) {        vertx.close(context.asyncAssertSuccess());    }    @Test    public void testApplication(TestContext context) {        final Async async = context.async();        vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {            response.handler(body -> {                context.assertTrue(body.toString().contains("Hello"));                async.complete();            });        });    }}

 

执行该测试案例便可得到期望的结果,理解测试代码并不难,留给读者作为练习。

至此,大功告成,欢迎来到Vert.x的世界。

转载于:https://www.cnblogs.com/heqiyoujing/p/9746183.html

你可能感兴趣的文章
【转载】Linux下的crontab定时执行任务命令
查看>>
STM32 HAL库的定时器中断回调函数跟串口中断回调函数
查看>>
vs2010找不到ado.net 实体数据模型解决办法
查看>>
(转)深入理解javascript连续赋值表达式
查看>>
用户场景分析
查看>>
MySQL创建数据库及用户
查看>>
Springboot静态页面放在static路径下还是访问不到
查看>>
centos7 重启网卡失败
查看>>
springboot(一)注解
查看>>
07 Mybatis的多表查询1----1对多和多对1
查看>>
debian和ubuntu的sh dash bash
查看>>
java9-8 局部内部类
查看>>
数据库分页
查看>>
Centos6.8源码编译安装PHP7
查看>>
012 debug调试工具的指令
查看>>
慕课网消息的接收与响应3
查看>>
第三十二讲:UML类图(下)
查看>>
linux下更改时区
查看>>
复杂链表的复制
查看>>
code vs 3376 符号三角形
查看>>