This document describes best practice for developing Vert.x applications as one or more Vert.x modules using the standard Vert.x project layout. It's highly recommended that you write any non-trivial Vert.x applications as one or more modules.

If your application is relatively small it might make sense to create it as a single module. If your application is large split it up into a set of modules.

It's also recommended you read the Vert.x main manual and the Vert.x modules manual so you understand the basics of Vert.x and modules before starting out.

There should be only one output module per project, so for each module in your application (simple applications may well only have one module) you should create a new project.

The standard layout has a "Maven-style" directory structure that will probably be already familiar with you.

If you're a Gradle user you can clone the Gradle Template Project to get you started quickly.

If you're a Maven user you'll get the same structure if you use the Vert.x Maven Archetype to create your project (without the Gradle files of course!).

If you prefer to use some other build tool (e.g. ant), that's fine - Vert.x is agnostic about build tool, but you'll have to provide your own build script (perhaps someone could contribute one?) for now.

We'll now explore the standard layout.

The module

All Vert.x modules contain a mod.json descriptor. This a file containing some JSON which describes the module. Amongst other things it usually contains a main field. This tells Vert.x which verticle to run when the module is deployed.

The mod.json file is in the src/main/resources directory of the project. Any files in this directory are copied to the root of the module during packaging.

The thing that is run is called a Verticle. A Verticle can be written in any of the languages that Vert.x supports.

If you've created your project using the Vert.x Maven Archetype or Vert.x Gradle Template you'll see we have a simple Java Verticle calledPingVerticle. You can see the source for that in the src/main/java sub tree of the project.

The verticle has a start() method which is called when the verticle is deployed. In the start() method the verticle simply registers a handler on the event bus against address ping-address. The handler will be called when a message is received at that address. When a message is received the verticle simply replies to the ping! with a pong!.

The standard project layout also contains equivalents of the ping verticle written in JavaScript, Groovy (both compiled and script), Ruby and Python. You can edit main in mod.json to tell it to use one of the other versions of the ping verticle.

The tests

The rest of the stuff in the example project is a set of example tests.

Unit tests

Unit tests are tests that run outside the Vert.x container and which work with your module's Java classes directly. They go by convention insrc/test/unit.

You can run unit tests in your IDE as normal by right clicking on the test or on the command line.

Integration tests

We define integration tests here to mean Vert.x tests that are run inside the Vert.x container.

We provide a custom JUnit test runner which auto-magically takes your tests, starts up a Vert.x container, runs your test in it, and then reports the test results back, all as if the tests had been run locally as normal JUnit tests.

You can even write your tests in JavaScript, Groovy, Ruby or Python and still use the familiar JUnit Assert API.

There are example integration tests for each language in the example project. If you are not interested in writing tests in different languages you can safely delete the files you're not interested in.

Java integration tests

The example Java integration tests are in src/test/java/com/mycompany/integration/java

Java integration tests subclass TestVerticle - after all, the test is run inside the Vert.x container as a verticle. Then you just add standard JUnit test methods to it as normal, annotated with @Test.

Please note that the JUnit annotations: @Before@After@BeforeClass@AfterClass, and @Expects are not currently supported.

Vert.x integration tests are asynchronous so they won't necessarily have finished until after the test method completes, therefore to signal that the test has completed you should call the method VertxAssert.testComplete() at the end.

If your test deploys other verticles you can also assert from there and call testComplete from there too.

Please see the example tests for more examples.

JavaScript integration tests

The example Java integration tests are in src/test/resources/integration_tests/javascript

The class in src/test/java/com/mycompany/integration/javascript is just a stub class that tells JUnit where the real JavaScript tests are. You can safely ignore it.

Ruby integration tests

The example Java integration tests are in src/test/resources/integration_tests/ruby

The class in src/test/java/com/mycompany/integration/ruby is just a stub class that tells JUnit where the real Ruby tests are. You can safely ignore it.

Groovy integration tests

The example Java integration tests are in src/test/resources/integration_tests/groovy

The class in src/test/java/com/mycompany/integration/groovy is just a stub class that tells JUnit where the real Groovy tests are. You can safely ignore it.

Python integration tests

The example Java integration tests are in src/test/resources/integration_tests/python

The class in src/test/java/com/mycompany/integration/python is just a stub class that tells JUnit where the real Python tests are. You can safely ignore it.

Run tests in your IDE

To run Vert.x integration tests in your IDE, simply open the folder src/test/java/com/mycompany/integration in your IDE and right click it and chose to run all tests as JUnit tests (how this is done depends on your IDE). Or you can select individual test classes.

You'll need to make sure you don't have a module built using the command line when running tests in the IDE. Execute ./gradlew cleanor mvn clean to make sure or the test run will pick up resources from there instead.

Note that you can change your code or config and re-run the tests and Vert.x will pick up the changes without you having to rebuild at the command line!

You can also run the tests at the command line if you prefer (using mvn integration-tests or ./gradlew test)

Debug tests in your IDE

You can also set breakpoints in your Java code for seamless debugging into your Vert.x verticles and modules as normal. No special set-up is required

Run your module and see your changes immediately

When developing a Vert.x module, especially if it has a web interface, it's often useful to have your module running and have it automatically pick up any changes in classes or other resources in the module without you having to rebuild the module.

To get this to work your module must be marked as:

"auto-redeploy": true

In your mod.json. See the modules manual for more information on this.

If you haven't done so already, you can create project files for your IDE using:

./gradlew idea

Or

./gradlew eclipse

Now open your project file in your IDE, and set your project in your IDE to automatically compile when source files are saved - how to do this depends on your particular IDE so consult your IDE documentation if in doubt.

Now, make sure your IDE has built your project at least once (e.g. hit CTRL-F9 in IntelliJ IDEA).

Note you should make sure there is no previously built module lurking in the target/mods directory - of one is found there then runModwill deploy that, not the module corresponding to the resources in your IDE. So delete target/mods if it exists before running runMod.

Then, if you're using the standard Vert.x Gradle Template project, you can run the following from a console in your project directory:

./gradlew runMod -i

This will start Vert.x running and it will monitor the file system to changes to your module as you edit them and save your changes.

If you want to provide command line arguments to the running module, e.g. you want to specify a config file you can edit the runModArgsproperty in `gradle.properties".

Note that we use the -i switch when running gradlew - this tells Gradle not to swallow INFO level debug output when running. By default Gradle swallows all logging output (!)

If you're using Maven you can run the following from a console in your project directory:

mvn clean vertx:runMod

This will start Vert.x running and it will monitor the file system to changes to your module as you edit them and save your changes.

By installing the Maven or Gradle plugin for your IDE you should be able to run the runMod tasks directly in the IDE.

Vert.x uses the file vertx_classpath.txt to determine where to find the resources of your module during development. This file is configured for standard directories used by both IntelliJ IDEA and Eclipse, but if you have put your resources in a different place you can edit this file to point at where the resources are.

If you don't use Maven or Gradle but still want to see your changes in a running module immediately you can run the following from a console in your project directory:

vertx create-module-link com.yourcompany~your-module~1.0
vertx runmod com.yourcompany~your-module~1.0

Working with Multi Module Applications

For larger applications it makes sense to implement them as a set of Vert.x modules.

The application should be made up of several projects (probably created using the Maven archetype or Gradle template), each outputting a single module.

You can then use

mvn install

Or

./gradlew install

From any of the projects to install that module in your local Maven repository, and it will be automatically picked up by your other modules which use it. Vert.x module system understands how to pull modules from local (as well as remote) Maven repositories.

If you want to enable auto-redeploy for all modules in a multi-module project then it's recommended that you set VERTX_MODS to point to a directory in the parent directory for your modules.

Then in each module project directory execute:

vertx create-module-link <module_name>

This only has to be done once.

Then just run the main module with vertx runmod as normal and any changes in sub modules will be picked up causing redeployment.

Other best practices

Using a Verticle to co-ordinate loading of an application

If you have an application that is composed of multiple verticles that all need to be started at application start-up, then you can use another verticle that maintains the application configuration and starts all the other verticles. You can think of this as your application starter verticle.

For example, you could create a verticle app.js (you could use another scripting language - Ruby, Groovy or Python if you prefer) as follows:

// Start the verticles that make up the app

vertx.deployVerticle("verticle1.js", appConfig.verticle1Config);
vertx.deployVerticle("verticle2.js", appConfig.verticle2Config, );
vertx.deployVerticle("verticle3.js", appConfig.verticle3Config);
vertx.deployWorkerVerticle("verticle4.js", appConfig.verticle4Config);
vertx.deployWorkerVerticle("verticle5.js", appConfig.verticle5Config, );

Then set the app.js verticle as the main of your module and then you can start your entire application by simply running:

vertx runmod com.mycompany~my-mod~1.0 -conf conf.json

Where conf.json is a config file like:

// Application config
{
verticle1Config: {
// Config for verticle1
},
verticle2Config: {
// Config for verticle2
},
verticle3Config: {
// Config for verticle3
},
verticle4Config: {
// Config for verticle4
},
verticle5Config: {
// Config for verticle5
}
}

If your application is large and actually composed of multiple modules rather than verticles you can use the same technique.

摘自:http://vertx.io/vertx2/dev_guide.html

Developing Vert.x Modules using the Standard Project的更多相关文章

  1. Developing Vert.x Modules with Gradle

    ========= https://www.techempower.com/benchmarks/#section=data-r12&hw=peak&test=json ======= ...

  2. [Xamarin] - Xamarin.Forms Project with .Net Standard 2.0

    1. Install .NET Core 2.0 SDK .https://www.microsoft.com/net/download/core 2. Install Android 7.1 (AP ...

  3. Create the Project

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspn ...

  4. Project Management Process

    Project Management ProcessDescription .............................................................. ...

  5. Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules

    在eclipse里面配置tomcat时候遇到的问题: Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web mo ...

  6. build.gradle(Project) 和 build.gradle(Module) 的区别

    参考: http://stackoverflow.com/questions/28295933/difference-between-build-gradleproject-and-build-gra ...

  7. Task '' not found in root project '***'.

    android编译app报错:Task '' not found in root project '***'.将build.gradle里的 if (gradle.gradleVersion > ...

  8. office 2016 官方原版 (含Visio Project 等全套 )下载地址 (不含破解,非网盘下载)不用登录

    原文地址:https://www.heidoc.net/joomla/technology-science/microsoft/8-office-2016-direct-download-links ...

  9. Amazon onsite behavior question

    https://www.1point3acres.com/bbs/thread-307462-1-1.html http://kraftshala.com/how-to-raise-the-bar-i ...

随机推荐

  1. 工厂参观记:.NET Core 中 HttpClientFactory 如何解决 HttpClient 臭名昭著的问题

    在 .NET Framework 与 .NET Core 中 HttpClient 有个臭名昭著的问题,HttpClient 实现了 IDispose 接口,但当你 Dispose 它时,它不会立即关 ...

  2. js canvas游戏初级demo-上下左右移动

    大概流程就是监听状态变化擦除画布重绘 由于js监听时间变化的函数addEventListener只能达到每秒触发20次左右,也就是每秒20帧,看起来有点卡卡的 所以用定时器搞到每秒30帧 按上下左右键 ...

  3. Xcode工程编译错误之iOS开发之The Xcode build system has crashed. Please close and reopen your workspace

    解决方法: . 删除DerivedData . 参照上面的链接设置:File -> Workspace Settings -> Build System -> Legacy Buil ...

  4. npm是什么,用来干嘛的

    网上的 npm 教程主要都在讲怎么安装.配置和使用 npm,却不告诉新人「为什么要使用 npm」.今天我就来讲讲这个话题. 本文目标读者是「不太了解 npm 的新人」,大神您别看了,不然又说我啰嗦了 ...

  5. 召回率(Recall),精确率(Precision),平均正确率

    https://blog.csdn.net/yanhx1204/article/details/81017134 摘要 在训练YOLO v2的过程中,系统会显示出一些评价训练效果的值,如Recall, ...

  6. SSMP一次请求数据处理过程分析

    控制器代码 @RequestMapping("/changeUserPwd") public TranMessage changeUserPwd(String oriPwd, St ...

  7. mybatis增强

    MyBatis SQL参数传递(掌握) SQL映射器Mapper接口(掌握)Myb atis批量操作(理解掌握) (多对一)关联映射(掌握) (一对多,多对多)集合映射 MyBatis原理回顾(Obj ...

  8. word_freq

    1) 博客开头给出自己的基本信息,格式建议如下: 学号:2017*****7193(保留前4位和后4位,中间用星号代替,避免泄露个人信息): 姓名:刘新飞,用你的真实姓名替代 我的码云仓库地址:[ht ...

  9. #WEB安全基础 : HTTP协议 | 0x10 扩展HTTP报文结构概念和内容编码

    #以后的知识都是HTTP协议的扩展,如果精力有限可以选择暂时忽略,注意只是暂时忽略,以后的东西同样重要 HTTP传输数据时可以直接传输也可以对数据进行编码,由于编码在计算机内运行,所以会占用一些CPU ...

  10. es6 遍历总结

    1.for in / for of for in --> index是key值 var array = [1,2,3,4,5]; for(let index in array) { consol ...