|
| 1 | +//// |
| 2 | +This document is maintained in the main Quarkus repository |
| 3 | +and pull requests should be submitted there: |
| 4 | +https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc |
| 5 | +//// |
| 6 | +[id="extension-writing-dev-service"] |
| 7 | += Writing a Dev Service |
| 8 | +include::_attributes.adoc[] |
| 9 | +:categories: writing-extensions |
| 10 | +:diataxis-type: howto |
| 11 | +:topics: extensions |
| 12 | +//// |
| 13 | +//// |
| 14 | + |
| 15 | + |
| 16 | +== Prerequisites |
| 17 | + |
| 18 | +- You should already have an xref:building-my-first-extension.adoc[extension structure] in place |
| 19 | +- You should have a containerised version of your external service (not all Dev Services rely on containers, but most do) |
| 20 | + |
| 21 | +== Creating a Dev Service |
| 22 | + |
| 23 | +If your extension provides APIs for connecting to an external service, it's a good idea to provide a xref:dev-services.adoc[Dev Service] implementation. |
| 24 | + |
| 25 | +To create a Dev Service, add a new build step into the extension processor class that returns a `DevServicesResultBuildItem`. |
| 26 | +Here, the link:https://hub.docker.com/_/hello-world`hello-world` image is used, but you should set up the right image for your service. |
| 27 | + |
| 28 | +[source%nowrap,java] |
| 29 | +---- |
| 30 | + @BuildStep(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class) { |
| 31 | + public DevServicesResultBuildItem createContainer() { |
| 32 | + DockerImageName dockerImageName = DockerImageName.parse("hello-world"); |
| 33 | + GenericContainer container = new GenericContainer<>(dockerImageName) |
| 34 | + .withExposedPorts(SERVICE_PORT, OTHER_SERVICE_PORT) |
| 35 | + .waitingFor(Wait.forLogMessage(".*" + "Started" + ".*", 1)) |
| 36 | + .withReuse(true); |
| 37 | +
|
| 38 | + container.start(); |
| 39 | +
|
| 40 | + String newUrl = "http://" + container.getHost() + ":" + container.getMappedPort(SERVICE_PORT); |
| 41 | + Map<String, String> configOverrides = Map.of("some-service.base-url", newUrl); |
| 42 | +
|
| 43 | + return new DevServicesResultBuildItem.RunningDevService(FEATURE, container.getContainerId(), |
| 44 | + container::close, configOverrides) |
| 45 | + .toBuildItem(); |
| 46 | + } |
| 47 | +---- |
| 48 | + |
| 49 | +With this code, you should be able to see your container starting if you add your extension to a test application and run `quarkus dev`. |
| 50 | +However, the application will not be able to connect to it, because no ports are exposed. To expose ports, add `withExposedPorts` to the container construction. |
| 51 | +For example, |
| 52 | + |
| 53 | +[source%nowrap,java] |
| 54 | +---- |
| 55 | +GenericContainer container = new GenericContainer<>(dockerImageName) |
| 56 | + .withExposedPorts(SERVICE_PORT, OTHER_SERVICE_PORT); |
| 57 | +---- |
| 58 | + |
| 59 | +Testcontainers will map these ports to random ports on the host. This avoids port conflicts, but presents a new problem – how do applications connect to the service in the container? |
| 60 | + |
| 61 | +To allow applications to connect, the extension should override the default configuration for the service with the mapped ports. |
| 62 | +This must be done after starting the container. |
| 63 | +For example, |
| 64 | + |
| 65 | +[source%nowrap,java] |
| 66 | +---- |
| 67 | + container.start(); |
| 68 | + Map<String, String> configOverrides = Map.of("some-service.base-url", |
| 69 | + "http://" + container.getHost() + ":" + container.getMappedPort(SERVICE_PORT)); |
| 70 | +---- |
| 71 | + |
| 72 | +Other configuration overrides may be included in the same map. |
| 73 | + |
| 74 | +== Waiting for the container to start |
| 75 | + |
| 76 | +You should add a `.waitingFor` call to the container construction, to wait for the container to start. For example |
| 77 | + |
| 78 | +[source%nowrap,java] |
| 79 | +---- |
| 80 | + .waitingFor(Wait.forLogMessage(".*" + "Started" + ".*", 1)) |
| 81 | +---- |
| 82 | + |
| 83 | +Waiting for a port to be open is another option. See the link:https://java.testcontainers.org/features/startup_and_waits/[Testcontainers documentation] for a full discussion of wait strategies. |
| 84 | + |
| 85 | +== Configuring the Dev Service |
| 86 | + |
| 87 | +To configure the Dev Service launch process, your build step can accept a `ConfigPhase.BUILD_TIME` config class in its constructor. |
| 88 | +For example, |
| 89 | + |
| 90 | +[source%nowrap,java] |
| 91 | +---- |
| 92 | + @BuildStep(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class) { |
| 93 | + public DevServicesResultBuildItem createContainer(MyConfig config) { |
| 94 | +---- |
| 95 | + |
| 96 | +You may wish to use this config to set a fixed port, or set an image name, for example. |
| 97 | + |
| 98 | +[source%nowrap,java] |
| 99 | +---- |
| 100 | + if (config.port.isPresent()) { |
| 101 | + container.setPortBindings(List.of(config.port.get() + ":" + SERVICE_PORT)); |
| 102 | + } |
| 103 | +---- |
| 104 | + |
| 105 | +== Controlling re-use |
| 106 | + |
| 107 | +In dev mode, with live reload, Quarkus may restart frequently. By default, this will also restart test containers. |
| 108 | +Quarkus restarts are usually very fast, but containers may take much longer to restart. |
| 109 | +To prevent containers restarting on every code change, you can mark the container as reusable: |
| 110 | + |
| 111 | +[source%nowrap,java] |
| 112 | +---- |
| 113 | + .withReuse(true) |
| 114 | +---- |
| 115 | + |
| 116 | +Some Dev Services implement sophisticated reuse logic in which they track the state of the container in the processor itself. |
| 117 | +You may need this if your service has more complex requirements, or needs sharing across instances. |
| 118 | + |
| 119 | + |
| 120 | +== References |
| 121 | + |
| 122 | +- xref:dev-services.adoc[Dev services overview] |
| 123 | +- xref:writing-extensions.adoc[Guide to writing extensions] |
0 commit comments