├── .editorconfig ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── io │ └── github │ └── jponge │ └── vertx │ └── boot │ └── BootVerticle.java └── test ├── java └── io │ └── github │ └── jponge │ └── vertx │ └── boot │ ├── BootVerticleTest.java │ └── samples │ ├── BarVerticle.java │ ├── ConfigDumpingVerticle.java │ └── FooVerticle.java └── resources ├── alternative.conf ├── application.conf ├── config-dump-noparameters.conf └── config-dump-withparameters.conf /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | trim_trailing_whitespace = true 8 | end_of_line = lf 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle/ 2 | .idea/ 3 | .vertx/ 4 | out/ 5 | *.iml 6 | /build/ 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: xenial 2 | language: java 3 | 4 | jdk: 5 | - openjdk8 6 | - openjdk11 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Julien Ponge 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/jponge/vertx-boot.svg?branch=master)](https://travis-ci.org/jponge/vertx-boot) 2 | ![License](https://img.shields.io/github/license/jponge/vertx-boot.svg) 3 | 4 | # 🚀 Vert.x Boot 5 | 6 | > An Eclipse Vert.x verticle to boot an application from HOCON configuration. 7 | 8 | The goal of this micro-library is to offer a simple way to deploy verticles from a [HOCON](https://github.com/lightbend/config/blob/master/HOCON.md) configuration. 9 | More specifically, it allows to: 10 | 11 | 1. specify what verticles to deploy, and 12 | 2. specify how many instances of each verticle to deploy, and 13 | 3. pass some JSON configuration (HOCON is a superset of JSON). 14 | 15 | ## Dependency 16 | 17 | * `groupId`: `io.github.jponge` 18 | * `artifactId`: `vertx-boot` 19 | 20 | The library is being published to both [Maven Central](https://search.maven.org/#search%7Cga%7C1%7Ca%3A%22vertx-boot%22) and [Bintray JCenter](https://bintray.com/jponge/vertx-boot/vertx-boot). 21 | 22 | ## Configuring Vert.x Boot 23 | 24 | The HOCON configuration is fetched with [lightbend/config](https://github.com/lightbend/config) using [the standard behavior](https://github.com/lightbend/config#standard-behavior) so please check the corresponding documentation for overriding files, resources and also overriding values using system properties and environment variables. 25 | You can use all of the nice features in HOCON, really (includes, substitutions, etc). 26 | 27 | The HOCON configuration can be larger than what is required for _Vert.x Boot_. 28 | 29 | ### Basic configuration 30 | 31 | Here is an example: 32 | 33 | ```hocon 34 | vertx-boot { 35 | 36 | verticles { 37 | 38 | foo { 39 | name = "io.github.jponge.vertx.boot.samples.FooVerticle" 40 | instances = 4 41 | } 42 | 43 | bar { 44 | name = "io.github.jponge.vertx.boot.samples.BarVerticle" 45 | instances = 2 46 | configuration.a = "abc" 47 | configuration.b = "def" 48 | configuration { 49 | c = 123 50 | d = [1, 2, 3] 51 | } 52 | } 53 | 54 | baz { 55 | name = "io.github.jponge.vertx.boot.samples.FooVerticle" 56 | } 57 | } 58 | } 59 | ``` 60 | 61 | Each verticle key (e.g., `foo` and `bar` in the example above) is purely decorative. 62 | A verticle class can be deployed more than once with different configurations and instance count. 63 | 64 | The `instance` and `configuration` keys in verticles are optional: by default a single instance is being deployed, and the configuration is an empty JSON object. 65 | 66 | ### Advanced configuration 67 | 68 | More advanced settings are available to match Vert.x `DeploymentOptions`: 69 | 70 | * `extra-classpath` a string array of extra classpath entries 71 | * `high-availability` a boolean for verticle high-availability 72 | * `isolated-classes` a string array of isolated classes 73 | * `isolated-group` a string for an isolated classes group name 74 | * `worker` a boolean to deploy as a worker verticle 75 | * `max-worker-execution-time` an integer number to define the maximum worker execution time 76 | * `worker-pool-name` a string to name the worker pool 77 | * `worker-pool-size` an integer to size the worker pool 78 | 79 | Here is a sample advanced configuration: 80 | 81 | ```hocon 82 | vertx-boot { 83 | verticles { 84 | foo { 85 | name = "io.github.jponge.vertx.boot.samples.FooVerticle" 86 | instances = 4 87 | worker = true 88 | worker-pool-name = "Fooz" 89 | worker-pool-size = 4 90 | configuration { 91 | a = 1 92 | b = 2 93 | } 94 | } 95 | } 96 | } 97 | ``` 98 | 99 | ## Using the verticle 100 | 101 | The verticle class is `io.github.jponge.vertx.boot.BootVerticle`. 102 | 103 | You can deploy it programmatically and it will then deploy the other verticles. 104 | 105 | If you create a _fat jar_ and rely on the `Main-Verticle` manifest entry and the `io.vertx.core.Launcher` main class, then all you have to do is point the `Main-Verticle` entry to `io.github.jponge.vertx.boot.BootVerticle`. 106 | 107 | ## Contributing 108 | 109 | Feel-free to report issues and propose pull-requests! 110 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java-library' 3 | id 'maven-publish' 4 | id 'com.github.ben-manes.versions' version '0.27.0' 5 | } 6 | 7 | repositories { 8 | mavenCentral() 9 | maven { 10 | url 'https://oss.sonatype.org/content/repositories/snapshots/' 11 | } 12 | } 13 | 14 | group 'io.github.jponge' 15 | version '1.2.2-SNAPSHOT' 16 | 17 | compileJava { 18 | sourceCompatibility = JavaVersion.VERSION_1_8 19 | targetCompatibility = JavaVersion.VERSION_1_8 20 | } 21 | 22 | dependencies { 23 | api 'io.vertx:vertx-core:3.9.4' 24 | implementation 'com.typesafe:config:1.4.0' 25 | 26 | testImplementation 'io.vertx:vertx-junit5:3.9.4' 27 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' 28 | testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.0' 29 | } 30 | 31 | jar { 32 | manifest { 33 | attributes( 34 | 'Implementation-Title': project.name, 35 | 'Implementation-Version': project.version) 36 | } 37 | } 38 | 39 | test { 40 | useJUnitPlatform() 41 | } 42 | 43 | task sourceJar(type: Jar) { 44 | from sourceSets.main.allJava 45 | } 46 | 47 | task javadocJar(type: Jar, dependsOn: javadoc) { 48 | from javadoc.destinationDir 49 | } 50 | 51 | publishing { 52 | publications { 53 | vertxBoot(MavenPublication) { 54 | 55 | from components.java 56 | 57 | artifact sourceJar { 58 | classifier "sources" 59 | } 60 | 61 | artifact javadocJar { 62 | classifier 'javadoc' 63 | } 64 | 65 | pom { 66 | name = 'Vert.x Boot' 67 | description = 'A Vert.x verticle to boot an application from HOCON configuration.' 68 | url = 'https://github.com/jponge/vertx-boot' 69 | licenses { 70 | license { 71 | name = 'MIT License' 72 | url = 'https://opensource.org/licenses/MIT' 73 | } 74 | } 75 | developers { 76 | developer { 77 | id = 'jponge' 78 | name = 'Julien Ponge' 79 | email = 'julien.ponge@gmail.com' 80 | url = 'https://julien.ponge.org/' 81 | } 82 | } 83 | scm { 84 | connection = 'scm:git:git://github.com/jponge/vertx-boot.git' 85 | developerConnection = 'scm:git:ssh://github.com/jponge/vertx-boot.git' 86 | url = 'https://github.com/jponge/vertx-boot/tree/master' 87 | } 88 | } 89 | } 90 | } 91 | repositories { 92 | maven { 93 | url 'https://api.bintray.com/maven/jponge/vertx-boot/vertx-boot' 94 | credentials { 95 | username project.hasProperty('bintrayRepoUsername') ? bintrayRepoUsername : System.getenv('bintrayRepoUsername') 96 | password project.hasProperty('bintrayRepoPassword') ? bintrayRepoPassword : System.getenv('bintrayRepoPassword') 97 | } 98 | } 99 | } 100 | } 101 | 102 | wrapper { 103 | gradleVersion = '6.0' 104 | } 105 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jponge/vertx-boot/9938e4200f754ab6bf44bbb7a23fe43471dd09ee/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin or MSYS, switch paths to Windows format before running java 129 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=`expr $i + 1` 158 | done 159 | case $i in 160 | 0) set -- ;; 161 | 1) set -- "$args0" ;; 162 | 2) set -- "$args0" "$args1" ;; 163 | 3) set -- "$args0" "$args1" "$args2" ;; 164 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=`save "$@"` 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | exec "$JAVACMD" "$@" 184 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 34 | 35 | @rem Find java.exe 36 | if defined JAVA_HOME goto findJavaFromJavaHome 37 | 38 | set JAVA_EXE=java.exe 39 | %JAVA_EXE% -version >NUL 2>&1 40 | if "%ERRORLEVEL%" == "0" goto init 41 | 42 | echo. 43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 44 | echo. 45 | echo Please set the JAVA_HOME variable in your environment to match the 46 | echo location of your Java installation. 47 | 48 | goto fail 49 | 50 | :findJavaFromJavaHome 51 | set JAVA_HOME=%JAVA_HOME:"=% 52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 53 | 54 | if exist "%JAVA_EXE%" goto init 55 | 56 | echo. 57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 58 | echo. 59 | echo Please set the JAVA_HOME variable in your environment to match the 60 | echo location of your Java installation. 61 | 62 | goto fail 63 | 64 | :init 65 | @rem Get command-line arguments, handling Windows variants 66 | 67 | if not "%OS%" == "Windows_NT" goto win9xME_args 68 | 69 | :win9xME_args 70 | @rem Slurp the command line arguments. 71 | set CMD_LINE_ARGS= 72 | set _SKIP=2 73 | 74 | :win9xME_args_slurp 75 | if "x%~1" == "x" goto execute 76 | 77 | set CMD_LINE_ARGS=%* 78 | 79 | :execute 80 | @rem Setup the command line 81 | 82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 83 | 84 | @rem Execute Gradle 85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 86 | 87 | :end 88 | @rem End local scope for the variables with windows NT shell 89 | if "%ERRORLEVEL%"=="0" goto mainEnd 90 | 91 | :fail 92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 93 | rem the _cmd.exe /c_ return code! 94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 95 | exit /b 1 96 | 97 | :mainEnd 98 | if "%OS%"=="Windows_NT" endlocal 99 | 100 | :omega 101 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'vertx-boot' 2 | -------------------------------------------------------------------------------- /src/main/java/io/github/jponge/vertx/boot/BootVerticle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Julien Ponge 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.jponge.vertx.boot; 27 | 28 | import java.util.ArrayList; 29 | import java.util.List; 30 | import java.util.stream.Collectors; 31 | import java.util.stream.Stream; 32 | 33 | import com.typesafe.config.Config; 34 | import com.typesafe.config.ConfigFactory; 35 | import com.typesafe.config.ConfigRenderOptions; 36 | 37 | import io.vertx.core.*; 38 | import io.vertx.core.json.JsonObject; 39 | 40 | /** 41 | * A verticle to deploy other verticles, based on a HOCON configuration. 42 | * 43 | * @author Julien Ponge 44 | */ 45 | public class BootVerticle extends AbstractVerticle { 46 | 47 | private static final String VERTX_BOOT_VERTICLES_PATH = "vertx-boot.verticles"; 48 | private static final String CONF_KEY = "configuration"; 49 | private static final String INSTANCES_KEY = "instances"; 50 | private static final String EXTRA_CLASSPATH_KEY = "extra-classpath"; 51 | private static final String HA_KEY = "high-availability"; 52 | private static final String ISOLATED_CLASSES_KEY = "isolated-classes"; 53 | private static final String ISOLATION_GROUP_KEY = "isolation-group"; 54 | private static final String MAXWORKER_EXECTIME_KEY = "max-worker-execution-time"; 55 | private static final String WORKER_KEY = "worker"; 56 | private static final String WORKER_POOLNAME_KEY = "worker-pool-name"; 57 | private static final String WORKER_POOLSIZE_KEY = "worker-pool-size"; 58 | 59 | @Override 60 | public void start(Promise promise) { 61 | try { 62 | Config bootConfig = ConfigFactory.load(); 63 | List configList = bootConfig.getConfig(VERTX_BOOT_VERTICLES_PATH).root().keySet().stream() 64 | .map(key -> bootConfig.getConfig(VERTX_BOOT_VERTICLES_PATH + "." + key)).collect(Collectors.toList()); 65 | 66 | List futures = configList.stream() 67 | .map(this::deployVerticle) 68 | .collect(Collectors.toList()); 69 | 70 | CompositeFuture.all(futures).onComplete(ar -> { 71 | if (ar.succeeded()) { 72 | promise.complete(); 73 | } else { 74 | promise.fail(ar.cause()); 75 | } 76 | }); 77 | } catch (Throwable t) { 78 | promise.fail(t); 79 | } 80 | } 81 | 82 | private Future deployVerticle(Config config) { 83 | Promise promise = Promise.promise(); 84 | try { 85 | String name = config.getString("name"); 86 | DeploymentOptions options = new DeploymentOptions() 87 | .setInstances(getInstances(config)) 88 | .setConfig(getConfig(config)) 89 | .setExtraClasspath(getExtraClasspath(config)) 90 | .setHa(getHa(config)) 91 | .setIsolatedClasses(getIsolatedClasses(config)) 92 | .setIsolationGroup(getIsolationGroup(config)) 93 | .setMaxWorkerExecuteTime(getMaxWorkerExecuteTime(config)) 94 | .setWorker(getWorker(config)) 95 | .setWorkerPoolName(getWorkerPoolName(config)) 96 | .setWorkerPoolSize(getWorkerPoolSize(config)); 97 | vertx.deployVerticle(name, options, ar -> { 98 | if (ar.succeeded()) { 99 | promise.complete(ar.result()); 100 | } else { 101 | promise.fail(ar.cause()); 102 | } 103 | }); 104 | } catch (Throwable t) { 105 | promise.fail(t); 106 | } 107 | return promise.future(); 108 | } 109 | 110 | private int getWorkerPoolSize(Config config) { 111 | if (config.hasPath(WORKER_POOLSIZE_KEY)) { 112 | return config.getInt(WORKER_POOLSIZE_KEY); 113 | } 114 | return 1; 115 | } 116 | 117 | private String getWorkerPoolName(Config config) { 118 | if (config.hasPath(WORKER_POOLNAME_KEY)) { 119 | return config.getString(WORKER_POOLNAME_KEY); 120 | } 121 | return null; 122 | } 123 | 124 | private boolean getWorker(Config config) { 125 | if (config.hasPath(WORKER_KEY)) { 126 | return config.getBoolean(WORKER_KEY); 127 | } 128 | return false; 129 | } 130 | 131 | private long getMaxWorkerExecuteTime(Config config) { 132 | if (config.hasPath(MAXWORKER_EXECTIME_KEY)) { 133 | return config.getLong(MAXWORKER_EXECTIME_KEY); 134 | } 135 | return Long.MAX_VALUE; 136 | } 137 | 138 | private String getIsolationGroup(Config config) { 139 | if (config.hasPath(ISOLATION_GROUP_KEY)) { 140 | return config.getString(ISOLATION_GROUP_KEY); 141 | } 142 | return null; 143 | } 144 | 145 | private List getIsolatedClasses(Config config) { 146 | if (config.hasPath(ISOLATED_CLASSES_KEY)) { 147 | return config.getStringList(ISOLATED_CLASSES_KEY); 148 | } 149 | return null; 150 | } 151 | 152 | private boolean getHa(Config config) { 153 | if (config.hasPath(HA_KEY)) { 154 | return config.getBoolean(HA_KEY); 155 | } 156 | return false; 157 | } 158 | 159 | private List getExtraClasspath(Config config) { 160 | if (config.hasPath(EXTRA_CLASSPATH_KEY)) { 161 | return config.getStringList(EXTRA_CLASSPATH_KEY); 162 | } 163 | return null; 164 | } 165 | 166 | private JsonObject getConfig(Config config) { 167 | if (config.hasPath(CONF_KEY)) { 168 | return new JsonObject(config.getValue(CONF_KEY).render(ConfigRenderOptions.concise())); 169 | } 170 | return new JsonObject(); 171 | } 172 | 173 | private int getInstances(Config config) { 174 | if (config.hasPath(INSTANCES_KEY)) { 175 | return config.getInt(INSTANCES_KEY); 176 | } 177 | return 1; 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /src/test/java/io/github/jponge/vertx/boot/BootVerticleTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Julien Ponge 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.jponge.vertx.boot; 27 | 28 | import com.typesafe.config.ConfigFactory; 29 | import io.github.jponge.vertx.boot.samples.ConfigDumpingVerticle; 30 | import io.vertx.core.Vertx; 31 | import io.vertx.core.json.JsonArray; 32 | import io.vertx.core.json.JsonObject; 33 | import io.vertx.junit5.Checkpoint; 34 | import io.vertx.junit5.VertxExtension; 35 | import io.vertx.junit5.VertxTestContext; 36 | import org.junit.jupiter.api.BeforeEach; 37 | import org.junit.jupiter.api.DisplayName; 38 | import org.junit.jupiter.api.Test; 39 | import org.junit.jupiter.api.extension.ExtendWith; 40 | 41 | import static org.junit.jupiter.api.Assertions.assertEquals; 42 | import static org.junit.jupiter.api.Assertions.assertTrue; 43 | 44 | @ExtendWith(VertxExtension.class) 45 | @DisplayName("🚀 Verticle deployments") 46 | class BootVerticleTest { 47 | 48 | @BeforeEach 49 | void prepare() { 50 | System.clearProperty("config.resource"); 51 | ConfigFactory.invalidateCaches(); 52 | } 53 | 54 | @Test 55 | @DisplayName("Deploy multiple verticles with a default application.conf resource file") 56 | void deployment_default_config(Vertx vertx, VertxTestContext testContext) { 57 | Checkpoint fooCheckpoint = testContext.checkpoint(4); 58 | Checkpoint barCheckpoint = testContext.checkpoint(2); 59 | 60 | vertx.deployVerticle(new BootVerticle(), testContext.succeeding(id -> { 61 | 62 | vertx.eventBus().consumer("foo", message -> { 63 | testContext.verify(() -> { 64 | assertTrue(message.body() instanceof JsonObject); 65 | assertTrue(((JsonObject) message.body()).isEmpty()); 66 | fooCheckpoint.flag(); 67 | }); 68 | }); 69 | 70 | vertx.eventBus().consumer("bar", message -> { 71 | testContext.verify(() -> { 72 | assertTrue(message.body() instanceof JsonObject); 73 | JsonObject conf = (JsonObject) message.body(); 74 | assertEquals("abc", conf.getString("a")); 75 | assertEquals("def", conf.getString("b")); 76 | assertEquals((Integer) 123, conf.getInteger("c")); 77 | JsonArray d = conf.getJsonArray("d"); 78 | assertEquals(3, d.size()); 79 | assertEquals((Integer) 1, d.getInteger(0)); 80 | assertEquals((Integer) 2, d.getInteger(1)); 81 | assertEquals((Integer) 3, d.getInteger(2)); 82 | barCheckpoint.flag(); 83 | }); 84 | }); 85 | 86 | })); 87 | } 88 | 89 | @Test 90 | @DisplayName("Deploy from a alternative.conf resource file") 91 | void deployment_alternative_config(Vertx vertx, VertxTestContext testContext) { 92 | System.setProperty("config.resource", "alternative.conf"); 93 | Checkpoint checkpoint = testContext.checkpoint(); 94 | 95 | vertx.deployVerticle(new BootVerticle(), testContext.succeeding(id -> { 96 | 97 | vertx.eventBus().consumer("foo", message -> { 98 | testContext.verify(() -> { 99 | assertTrue(message.body() instanceof JsonObject); 100 | JsonObject conf = (JsonObject) message.body(); 101 | assertEquals(1, conf.size()); 102 | assertEquals("Yo!", conf.getString("abc")); 103 | checkpoint.flag(); 104 | }); 105 | }); 106 | 107 | })); 108 | } 109 | 110 | @Test 111 | @DisplayName("Do not pass any extra configuration (worker verticle, etc)") 112 | void pass_no_extra_config(Vertx vertx, VertxTestContext testContext) { 113 | System.setProperty("config.resource", "config-dump-noparameters.conf"); 114 | 115 | vertx.eventBus().consumer("config.dump", message -> { 116 | testContext.verify(() -> { 117 | assertTrue(message.body() instanceof JsonObject); 118 | JsonObject conf = (JsonObject) message.body(); 119 | assertEquals(false, conf.getBoolean("worker")); 120 | assertEquals(false, conf.getBoolean("clustered")); 121 | testContext.completeNow(); 122 | }); 123 | }); 124 | 125 | vertx.deployVerticle(new BootVerticle(), testContext.succeeding()); 126 | } 127 | 128 | @Test 129 | @DisplayName("Pass extra configuration (worker verticle, etc)") 130 | void pass_extra_config(Vertx vertx, VertxTestContext testContext) { 131 | System.setProperty("config.resource", "config-dump-withparameters.conf"); 132 | 133 | vertx.eventBus().consumer("config.dump", message -> { 134 | testContext.verify(() -> { 135 | assertTrue(message.body() instanceof JsonObject); 136 | JsonObject conf = (JsonObject) message.body(); 137 | assertEquals(true, conf.getBoolean("worker")); 138 | assertEquals(false, conf.getBoolean("clustered")); 139 | testContext.completeNow(); 140 | }); 141 | }); 142 | 143 | vertx.deployVerticle(new BootVerticle(), testContext.succeeding()); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/test/java/io/github/jponge/vertx/boot/samples/BarVerticle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Julien Ponge 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.jponge.vertx.boot.samples; 27 | 28 | import io.vertx.core.AbstractVerticle; 29 | 30 | public class BarVerticle extends AbstractVerticle { 31 | 32 | @Override 33 | public void start() throws Exception { 34 | vertx.setTimer(1000, id -> 35 | vertx.eventBus().send("bar", config())); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/io/github/jponge/vertx/boot/samples/ConfigDumpingVerticle.java: -------------------------------------------------------------------------------- 1 | package io.github.jponge.vertx.boot.samples; 2 | 3 | import io.vertx.core.AbstractVerticle; 4 | import io.vertx.core.json.JsonObject; 5 | 6 | public class ConfigDumpingVerticle extends AbstractVerticle { 7 | 8 | @Override 9 | public void start() { 10 | JsonObject dump = new JsonObject(); 11 | dump.put("worker", context.isWorkerContext()); 12 | dump.put("clustered", vertx.isClustered()); 13 | vertx.eventBus().send("config.dump", dump); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/io/github/jponge/vertx/boot/samples/FooVerticle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Julien Ponge 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.jponge.vertx.boot.samples; 27 | 28 | import io.vertx.core.AbstractVerticle; 29 | 30 | public class FooVerticle extends AbstractVerticle { 31 | 32 | @Override 33 | public void start() throws Exception { 34 | vertx.setTimer(1000, id -> 35 | vertx.eventBus().send("foo", config())); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/resources/alternative.conf: -------------------------------------------------------------------------------- 1 | vertx-boot { 2 | verticles { 3 | foo { 4 | name = "io.github.jponge.vertx.boot.samples.FooVerticle" 5 | configuration.abc = "Yo!" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/test/resources/application.conf: -------------------------------------------------------------------------------- 1 | vertx-boot { 2 | 3 | verticles { 4 | 5 | foo { 6 | name = "io.github.jponge.vertx.boot.samples.FooVerticle" 7 | instances = 4 8 | } 9 | 10 | bar { 11 | name = "io.github.jponge.vertx.boot.samples.BarVerticle" 12 | instances = 2 13 | configuration.a = "abc" 14 | configuration.b = "def" 15 | configuration { 16 | c = 123 17 | d = [1, 2, 3] 18 | } 19 | } 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/resources/config-dump-noparameters.conf: -------------------------------------------------------------------------------- 1 | vertx-boot { 2 | verticles { 3 | dump { 4 | name = "io.github.jponge.vertx.boot.samples.ConfigDumpingVerticle" 5 | instances = 1 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/test/resources/config-dump-withparameters.conf: -------------------------------------------------------------------------------- 1 | vertx-boot { 2 | verticles { 3 | dump { 4 | name = "io.github.jponge.vertx.boot.samples.ConfigDumpingVerticle" 5 | instances = 1 6 | worker = true 7 | multi-threaded = true 8 | } 9 | } 10 | } 11 | --------------------------------------------------------------------------------