Vertx Skeleton
Overview
Skeleton infrastructure classes for Vert.x projects.
Requisites
- Java 8+
- Maven 3+
Install
Gradle
dependencies {
compile 'com.github.malkomich:vertx-skeleton:1.0.2'
}
Maven
<dependency>
<groupId>com.github.malkomich</groupId>
<artifactId>vertx-skeleton</artifactId>
<version>1.0.2</version>
</dependency>
Usage
CONFIG MODULE:
public class CustomConfigModule extends ConfigModule {
public CustomConfigModule(final Vertx vertx, final JsonObject config) {
super(vertx, config);
}
@Override
protected void configure() {
bind(ThirdPartyService.class).toInstance(thirdPartyService());
}
private ThirdPartyService thirdPartyService() {
return new ThirdPartyService();
}
}
SERVICE:
@ProxyGen
public interface CustomService extends VertxService {
@Fluent
@Override
CustomService execute(final JsonObject json, final Handler<AsyncResult<Void>> handler);
@ProxyClose
@Override
void close();
}
public class CustomServiceImpl implements CustomService {
private ThirdPartyService thirdPartyService;
public CustomServiceImpl(final ThirdPartyService thirdPartyService) {
this.thirdPartyService = thirdPartyService;
}
@Override
public CustomService execute(final JsonObject json,
final Handler<AsyncResult<Void>> handler) {
handler.handle(Future.succeededFuture()); // TODO: Implement your custom service logic
return this;
}
@Override
public void close() {
eventService.close(onClosed ->
System.out.println("Service was closed"));
}
}
VERTICLE:
public class CustomVerticle extends ServiceVerticle<CustomService> {
public static final String ADDRESS = "customAddress";
@Inject
private ThirdPartyService thirdPartyService;
@Override
protected CustomService initializeService() {
return new CustomServiceImpl(thirdPartyService);
}
@Override
protected String eventBusAddress() {
return ADDRESS;
}
}
LAUNCHER:
public class ServiceLauncher {
private static final String ENDPOINT_PATH = "/your-endpoint-path";
public static void main(final String[] args) {
new ServiceLauncher().launch();
}
private void launch() {
VerticleLauncher.builder()
.configModules(CustomConfigModule.class)
.verticles(CustomVerticle.class) // Take care of verticles order (in case of dependency)
.withPublicEndpoint(ENDPOINT_PATH, CustomVerticle.ADDRESS, CustomService.class)
.execute();
}
}