DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Vert.x Notes


Today I read Groovy Manual in 'Core' section in vertx documentation. It's a very good starting point.

Good examples: * gradle groovy example * gradle java example * gradle java embeded example * Vert.x Tutorial by Jakob Jenkov

Some facts about vertx:

  • The golden rule: never block event loop (like node.js);
  • vertx use 'future' to handle async events;
  • The 'verticle' is something like actor in akka;
  • Verticles live in event loop (standard verticle) or worker thread (worker verticle). They both live in one thread;
  • An 'event bus' has many 'address', which is a plain string;
  • A 'handler' can be registered to one or more addresses;
  • One or more handlers can be registered to the same address;
  • A 'message' can be published to an address, in which case all handlers can receive this message;
  • A message can be sent to an address, in which case only one handler could receive this message;
  • vertx use JSON as the default message format;

Run Vertical with external jars

Add these jars into the 'dependencies' section in 'build.gradle'. Then add the task 'getjars' into build.gradle:

task(getjars, type: Copy) {
    into "lib"
    from configurations.runtime
}

When develop, run gradle getjars to put all jars into 'lib' folder. Then run verticle with vertx run -cp 'lib/*' <verticle-name>. The wildcard syntax can only be used for Java 6 or later. In Java 5, you have to add all jar file names into -cp option. See How to include all the jars present at a particular directory in the CLASSPATH in one go? for details.

Web App Auto Refresh

First install Auto Refresh plugin for your browser, such as Chrome, then run:

mkdir src
cat << EOF > src/HelloServer.groovy
vertx.createHttpServer().requestHandler({ req ->
    req.response().putHeader("content-type", "text/html").
    end("<html><body><h3>Hello from vert.X!</h3></body></html>")
}).listen(8080)
EOF
vertx run --redeploy="src/**/*.*" --launcher-class=io.vertx.core.Launcher src/HelloServer.groovy 

Now when any file in folder 'src' changes, the vertical will be redeployed. You can put all external jars into 'lib' folder and run the verticle with the -cp option: vertx run --redeploy="src/**/*.*" --launcher-class=io.vertx.core.Launcher -cp lib src/HelloServer.groovy. See Automatic redeployment in Eclipse IDE and vertx run --help for details.

Build Fat Jar with Gradle

Build a fat-jar including all dependencies with Gradle: gradle shadowJar. Then this app can be run with java -jar app-fat-jar.jar.

Project files:

cat << EOF > build.gradle
plugins {
  id 'java'
  id 'groovy'
  id 'application'
  id 'com.github.johnrengelman.shadow' version '1.2.3'
}

repositories {
  mavenCentral()
  maven {
    url "https://oss.sonatype.org/content/repositories/snapshots"
  }
}


version = '3.4.1'

sourceCompatibility = '1.8'
mainClassName = 'io.vertx.core.Launcher'

dependencies {
  compile "io.vertx:vertx-core:$version"
  compile "io.vertx:vertx-lang-groovy:$version"
}

processResources {
  from 'src/main/groovy'
}

shadowJar {
  classifier = 'fat'
  manifest {
    attributes 'Main-Verticle': 'groovy:MyGroovyVerticle'
  }
  mergeServiceFiles {
    include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
  }
}

task wrapper(type: Wrapper) {
  gradleVersion = '3.4.1'
}
EOF
mkdir -p src/main/groovy
cat << EOF > src/main/groovy/MyGroovyVerticle.groovy
vertx.createHttpServer().requestHandler({ req ->
    req.response()
       .putHeader("content-type", "text/html")
       .end("<html><body><h1>Hello from vert.x!</h1></body></html>")
}).listen(8080)
EOF

P.S: Why you can use vertx directly in Groovy script without import or declaring it?

Because vertx is a field in class AbstractVerticle. The entrance class defined in build.gradle is io.vertx.core.Launcher, which will load AbstractVerticle somewhere at runtime (How? I didn't figure it out so far). The launcher then load your verticle (defined in Main-Verticle in build.gradle) As compared with Java verticle class, you must declare you class extends AbstractVerticle explicitly.



Published

Mar 29, 2017

Last Updated

Mar 29, 2017

Category

Tech

Tags

  • uberjar 1
  • vertx 5

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor