Maven dev mode documentation
Gradle dev mode documentation
Open Liberty Tools VS Code extension (optional)
Open Liberty Tools IntelliJ extension (optional)
-
Clone this repo.
-
Start dev mode:
- Maven:
mvn liberty:dev
- Gradle:
gradle libertyDev
- Maven:
-
Add
mpHealth-3.1
feature tosrc/main/liberty/config/server.xml
. You can now access the http://localhost:9080/health endpoint (though it's just an empty array).
4. Create the src/main/java/io/openliberty/sample/system/SystemLivenessCheck.java class. Changes are reflected in the http://localhost:9080/health endpoint.
package io.openliberty.sample.system;
import javax.enterprise.context.ApplicationScoped;
import java.lang.management.MemoryMXBean;
import java.lang.management.ManagementFactory;
import org.eclipse.microprofile.health.Liveness;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
@Liveness
@ApplicationScoped
public class SystemLivenessCheck implements HealthCheck {
@Override
public HealthCheckResponse call() {
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
long memUsed = memBean.getHeapMemoryUsage().getUsed();
long memMax = memBean.getHeapMemoryUsage().getMax();
return HealthCheckResponse.named(
SystemResource.class.getSimpleName() + " liveness check")
.withData("memory used", memUsed)
.withData("memory max", memMax)
.status(memUsed < memMax * 0.9).build();
}
}
5. Create the src/main/java/io/openliberty/sample/system/SystemReadinessCheck.java class. Changes are reflected in the http://localhost:9080/health endpoint.
package io.openliberty.sample.system;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Provider;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.health.Readiness;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
@Readiness
@ApplicationScoped
public class SystemReadinessCheck implements HealthCheck {
@Inject
@ConfigProperty(name = "io_openliberty_guides_system_inMaintenance")
Provider<String> inMaintenance;
@Override
public HealthCheckResponse call() {
HealthCheckResponseBuilder builder = HealthCheckResponse.named(
SystemResource.class.getSimpleName() + " readiness check");
if (inMaintenance != null && inMaintenance.get().equalsIgnoreCase("true")) {
return builder.withData("services", "not available").down().build();
}
return builder.withData("services", "available").up().build();
}
}
-
Change the
io_openliberty_guides_system_inMaintenance
variable insrc/main/liberty/config/server.xml
totrue
. Changes are reflected in the http://localhost:9080/health endpoint. Undo this afterwards. -
Make changes to the
src/main/webapp/index.html
(or any other webapp files). Changes are reflected on the home page http://localhost:9080/.
- Go to the console where you started dev mode, and press Enter. The integration tests are run on a separate thread while dev mode is still active.
2. Create the src/test/java/it/io/openliberty/sample/HealthEndpointIT.java class as an integration test. Notice the "liberty.hostname" and "liberty.http.port" system properties which are provided by dev mode when running integration tests. Press Enter in the console to run the tests. They should pass.
package it.io.openliberty.sample;
import static org.junit.Assert.assertEquals;
import javax.json.JsonObject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class HealthEndpointIT {
private static String baseUrl;
private static final String HEALTH_ENDPOINT = "/health";
private static final String LIVENESS_ENDPOINT = "/health/live";
private static final String READINESS_ENDPOINT = "/health/ready";
private Client client;
private Response response;
@BeforeClass
public static void oneTimeSetup() {
String hostname = System.getProperty("liberty.hostname", "localhost");
String port = System.getProperty("liberty.http.port", "9080");
baseUrl = "http://" + hostname + ":" + port + "/";
}
@Before
public void setup() {
response = null;
client = ClientBuilder.newClient();
client.register(JsrJsonpProvider.class);
}
@After
public void teardown() {
response.close();
client.close();
}
@Test
public void testHealthEndpoint() {
String healthURL = baseUrl + HEALTH_ENDPOINT;
response = this.getResponse(baseUrl + HEALTH_ENDPOINT);
this.assertResponse(healthURL, response);
JsonObject healthJson = response.readEntity(JsonObject.class);
String expectedOutcome = "UP";
String actualOutcome = healthJson.getString("status");
assertEquals("Application should be healthy", expectedOutcome, actualOutcome);
JsonObject healthCheck = healthJson.getJsonArray("checks").getJsonObject(0);
String healthCheckName = healthCheck.getString("name");
actualOutcome = healthCheck.getString("status");
assertEquals(healthCheckName + " wasn't healthy", expectedOutcome, actualOutcome);
healthCheck = healthJson.getJsonArray("checks").getJsonObject(1);
healthCheckName = healthCheck.getString("name");
actualOutcome = healthCheck.getString("status");
assertEquals(healthCheckName + " wasn't healthy", expectedOutcome, actualOutcome);
}
@Test
public void testLivenessEndpoint() {
String livenessURL = baseUrl + LIVENESS_ENDPOINT;
response = this.getResponse(baseUrl + LIVENESS_ENDPOINT);
this.assertResponse(livenessURL, response);
JsonObject healthJson = response.readEntity(JsonObject.class);
String expectedOutcome = "UP";
String actualOutcome = healthJson.getString("status");
assertEquals("Applications liveness check passed", expectedOutcome, actualOutcome);
}
@Test
public void testReadinessEndpoint() {
String readinessURL = baseUrl + READINESS_ENDPOINT;
response = this.getResponse(baseUrl + READINESS_ENDPOINT);
this.assertResponse(readinessURL, response);
JsonObject healthJson = response.readEntity(JsonObject.class);
String expectedOutcome = "UP";
String actualOutcome = healthJson.getString("status");
assertEquals("Applications readiness check passed", expectedOutcome, actualOutcome);
}
private Response getResponse(String url) {
return client.target(url).request().get();
}
private void assertResponse(String url, Response response) {
assertEquals("Incorrect response code from " + url, 200, response.getStatus());
}
}
-
Stop dev mode by pressing Ctrl-C in the console.
-
Run dev mode with hot testing enabled.
- Maven:
mvn liberty:dev -DhotTests
- Gradle:
gradle libertyDev --hotTests
- Maven:
-
Notice tests are run immediately after dev mode starts up.
-
In
src/main/java/io/openliberty/sample/system/SystemResource.java
, change the annotation@Path("/properties")
to@Path("/properties2")
. -
Notice tests are run automatically after the source change, and a test fails because the endpoint path is wrong.
-
Revert the annotation back to
@Path("/properties")
. -
Notice tests are run automatically and pass.
-
In
src/main/java/io/openliberty/sample/system/SystemLivenessCheck.java
, set a breakpoint inside thecall()
method. -
Attach your IDE's debugger to port
7777
.
For example, in VS Code, clickDebug
>Add Configuration...
>Java: Attach
> set"port": "7777"
. ThenView
>Debug
> selectDebug (Attach)
, then press the arrow icon to start debugging. -
In your browser, go to http://localhost:9080/health.
-
Notice your IDE pauses at the breakpoint that you set, allowing you to debug.
-
Disconnect the debugger.
-
When you are done, press Ctrl-C in the console to terminate dev mode and stop your server.
The Open Liberty Tools for VS Code extension is an IDE specific extension that lets you interact with dev mode through menu options as an alternative to using the console.
-
In VS Code, go to the Extensions view (
View
>Extensions
). -
Search for
Open Liberty Tools
. -
Click
Install
. -
Go to the Explorer view (
View
>Explorer
). -
In the side bar,
Liberty Dev Dashboard
shows your Liberty dev projects. -
Right-click your project to start, stop, and interact with dev mode.
The Open Liberty Tools for IntelliJ extension is an IDE specific extension lets you interact with dev mode through menu options as an alternative to using the console.
-
In IntelliJ, go to the Plugins Marketplace (
Settings
>Plugins
>Marketplace
). -
Search for
Open Liberty Tools
. -
Click
Install
. -
In the right side bar,
Liberty Dev Dashboard
shows your Liberty dev projects. -
Right-click your project name or select a task from the drop-down list to start, stop, and interact with dev mode.