Skip to content

Commit

Permalink
Document verticle support
Browse files Browse the repository at this point in the history
Also add a test verifying that verticle instances can be beans.
  • Loading branch information
cescoffier committed Feb 14, 2020
1 parent 7b0a750 commit ec3b4c3
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
80 changes: 80 additions & 0 deletions docs/src/main/asciidoc/vertx.adoc
Expand Up @@ -487,6 +487,86 @@ Then, create the native executable with:
./mvnw package -Pnative
----

== Deploying verticles

https://vertx.io/docs/vertx-core/java/#_verticles[Verticles] is "a simple, scalable, actor-like deployment and concurrency model" provided by _Vert.x_.
This model does not claim to be a strict actor-model implementation, but it does share similarities especially with respect to concurrency, scaling and deployment.
To use this model, you write and _deploy_ verticles, communicating with each other by sending messages on the event bus.

You can deploy _verticles_ in Quarkus.
It supports:

* _bare_ verticle - Java classes extending `io.vertx.core.AbstractVerticle`
* _Mutiny_ verticle - Java classes extending `io.smallrye.mutiny.vertx.core.AbstractVerticle`

To deploy verticles, use the regular Vert.x API:

[source, java]
====
@Inject Vertx vertx;
// ...
vertx.deployVerticle(MyVerticle.class.getName(), ar -> { });
vertx.deployVerticle(new MyVerticle(), ar -> { });
====

You can also pass deployment options to configure the verticle as well as set the number of instances.

Verticles are not _beans_ by default.
However, you can implement them as _ApplicationScoped_ beans and get injection support:

[source, java]
====
package io.quarkus.vertx.verticles;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.vertx.core.AbstractVerticle;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class MyBeanVerticle extends AbstractVerticle {
@ConfigProperty(name = "address") String address;
@Override
public Uni<Void> asyncStart() {
return vertx.eventBus().consumer(address)
.handler(m -> m.replyAndForget("hello"))
.completionHandler();
}
}
====

Don't inject the `vertx` instance in an injected verticle.
Access the `vertx` instance using the `vertx` file in your `start` / `asyncStart` method.

Then, deploy the verticle instance with:

[source, java]
====
package io.quarkus.vertx.verticles;
import io.quarkus.runtime.StartupEvent;
import io.vertx.mutiny.core.Vertx;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
@ApplicationScoped
public class VerticleDeployer {
@Inject Vertx vertx;
@Inject MyBeanVerticle verticle;
public void init(@Observes StartupEvent e) {
vertx.deployVerticle(verticle).await().indefinitely();
}
}
====

== Read only deployment environments

In environments with read only file systems you may receive errors of the form:
Expand Down
@@ -0,0 +1,20 @@
package io.quarkus.vertx.verticles;

import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.vertx.core.AbstractVerticle;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class MyBeanVerticle extends AbstractVerticle {

@ConfigProperty(name = "address") String address;

@Override
public Uni<Void> asyncStart() {
return vertx.eventBus().consumer(address)
.handler(m -> m.replyAndForget("hello"))
.completionHandler();
}
}
@@ -0,0 +1,20 @@
package io.quarkus.vertx.verticles;

import io.quarkus.runtime.StartupEvent;
import io.vertx.mutiny.core.Vertx;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

@ApplicationScoped
public class VerticleDeployer {

@Inject Vertx vertx;
@Inject MyBeanVerticle verticle;

public void init(@Observes StartupEvent e) {
vertx.deployVerticle(verticle).await().indefinitely();
}

}
@@ -0,0 +1,38 @@
package io.quarkus.vertx.verticles;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.AxleCodecTest;
import io.quarkus.vertx.MyPetCodec;
import io.quarkus.vertx.Person;
import io.quarkus.vertx.Pet;
import io.vertx.mutiny.core.Vertx;
import io.vertx.mutiny.core.eventbus.Message;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import javax.inject.Inject;

import static org.assertj.core.api.Assertions.assertThat;

public class VerticleDeploymentTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap
.create(JavaArchive.class)
.addClasses(MyBeanVerticle.class, VerticleDeployer.class)
.addAsResource(new StringAsset("address=foo"), "application.properties"));

@Inject Vertx vertx;

@Test
public void test() {
String s = vertx.eventBus().<String>request("foo", "anyone?")
.onItem().apply(Message::body)
.await().indefinitely();
assertThat(s).isEqualTo("hello");
}
}

0 comments on commit ec3b4c3

Please sign in to comment.