Skip to content

Commit d4a180c

Browse files
authored
Merge pull request #134 from OpenLiberty/staging
Merge staging to prod - Update the guide for Windows compatibility (#132)
2 parents 8db1c1d + d0a2971 commit d4a180c

File tree

12 files changed

+41
-33
lines changed

12 files changed

+41
-33
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ jobs:
4848
working-directory: finish
4949
steps:
5050
- uses: actions/checkout@v2
51-
- name: Set up JDK 11
51+
- name: Set up JDK 17
5252
uses: actions/setup-java@v1
5353
with:
54-
java-version: 11
54+
java-version: 17
5555
- run: unset _JAVA_OPTIONS
5656
- name: Run tests
5757
run: ../scripts/testApp.sh

README.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2022, 2023 IBM Corporation and others.
1+
// Copyright (c) 2022, 2024 IBM Corporation and others.
22
// Licensed under Creative Commons Attribution-NoDerivatives
33
// 4.0 International (CC BY-ND 4.0)
44
// https://creativecommons.org/licenses/by-nd/4.0/
@@ -13,7 +13,7 @@
1313
:page-essential: false
1414
:page-description: Learn how to use Jakarta WebSocket to stream messages between client and server services.
1515
:guide-author: Open Liberty
16-
:page-tags: ['Jakarta EE']
16+
:page-tags: ['jakarta-ee']
1717
:page-related-guides: ['grpc-intro']
1818
:page-permalink: /guides/{projectid}
1919
:imagesdir: /img/guide/{projectid}
@@ -135,7 +135,7 @@ Annotate the `SystemService` class with a [hotspot=serverEndpoint file=0]`@Serve
135135

136136
The [hotspot=onOpenMethod file=0]`onOpen()` method stores up the client sessions. The [hotspot=onCloseMethod file=0]`onClose()` method displays the reason for closing the connection and removes the closing session from the client sessions.
137137

138-
The [hotspot=onMessageMethod file=0]`onMessage()` method is called when receiving a message through the `option` parameter. The [hotspot=loadAverage hotspot=memoryUsageOrBoth file=0]`option` parameter signifies which message to construct, either system load, memory usage data, or both, and sends out the [hotspot=sendToAllSessions file=0]`JsonObject` message. The [hotspot=sendToAllSessionseMethod file=0]`sendToAllSessions()` method uses the WebSocket API to broadcast the message to all client sessions.
138+
The [hotspot=onMessageMethod file=0]`onMessage()` method is called when receiving a message through the `option` parameter. The [hotspot=cpuLoad hotspot=memoryUsageOrBoth file=0]`option` parameter signifies which message to construct, either system load, memory usage data, or both, and sends out the [hotspot=sendToAllSessions file=0]`JsonObject` message. The [hotspot=sendToAllSessionseMethod file=0]`sendToAllSessions()` method uses the WebSocket API to broadcast the message to all client sessions.
139139

140140
[role="code_command hotspot file=1", subs="quotes"]
141141
----
@@ -229,7 +229,7 @@ SystemLoadScheduler.java
229229
include::finish/client/src/main/java/io/openliberty/guides/client/scheduler/SystemLoadScheduler.java[]
230230
----
231231

232-
The `SystemLoadScheduler` class uses the [hotspot=systemClient file=1]`SystemClient` class to establish a connection to the server by the `ws://localhost:9081/systemLoad` URI at the [hotspot=postConstruct file=1]`@PostConstruct` annotated method. The [hotspot=sendSystemLoad file=1]`sendSystemLoad()` method calls the client to send a random string from either [hotspot=messages file=1]`loadAverage`, [hotspot=messages file=1]`memoryUsage`, or [hotspot=messages file=1]`both` to the `system` service. Using the link:https://openliberty.io/docs/latest/reference/javadoc/liberty-jakartaee9.1-javadoc.html?package=jakarta/ejb/package-frame.html&class=jakarta/ejb/TimerService.html[Jakarta Enterprise Beans Timer Service^], annotate the `sendSystemLoad()` method with the [hotspot=schedule file=1]`@Schedule` annotation so that it sends out a message every 10 seconds.
232+
The `SystemLoadScheduler` class uses the [hotspot=systemClient file=1]`SystemClient` class to establish a connection to the server by the `ws://localhost:9081/systemLoad` URI at the [hotspot=postConstruct file=1]`@PostConstruct` annotated method. The [hotspot=sendSystemLoad file=1]`sendSystemLoad()` method calls the client to send a random string from either [hotspot=messages file=1]`cpuLoad`, [hotspot=messages file=1]`memoryUsage`, or [hotspot=messages file=1]`both` to the `system` service. Using the link:https://openliberty.io/docs/latest/reference/javadoc/liberty-jakartaee9.1-javadoc.html?package=jakarta/ejb/package-frame.html&class=jakarta/ejb/TimerService.html[Jakarta Enterprise Beans Timer Service^], annotate the `sendSystemLoad()` method with the [hotspot=schedule file=1]`@Schedule` annotation so that it sends out a message every 10 seconds.
233233

234234
Now, create the front-end UI. The images and styles for the UI are provided for you.
235235

finish/client/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<packaging>war</packaging>
99

1010
<properties>
11-
<maven.compiler.source>11</maven.compiler.source>
12-
<maven.compiler.target>11</maven.compiler.target>
11+
<maven.compiler.source>17</maven.compiler.source>
12+
<maven.compiler.target>17</maven.compiler.target>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1515
<!-- Liberty configuration -->

finish/client/src/main/java/io/openliberty/guides/client/scheduler/SystemLoadScheduler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class SystemLoadScheduler {
2525
private SystemClient client;
2626
// tag::messages[]
2727
private static final String[] MESSAGES = new String[] {
28-
"loadAverage", "memoryUsage", "both" };
28+
"cpuLoad", "memoryUsage", "both" };
2929
// end::messages[]
3030

3131
// tag::postConstruct[]

finish/client/src/main/webapp/index.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
<html>
2020
<head>
2121
<meta charset="UTF-8">
22-
<title>Open Liberty System Load</title>
22+
<title>Open Liberty - Jakarta WebSocket Example</title>
2323
<link rel="stylesheet" href="css/styles.css">
2424
<link href="favicon.ico" rel="icon" />
2525
<link href="favicon.ico" rel="shortcut icon" />
2626
</head>
2727
<body>
2828
<section id="appIntro">
2929
<div id="titleSection">
30-
<h1 id="appTitle">Open Liberty System Load</h1>
30+
<h1 id="appTitle">Jakarta WebSocket Example</h1>
3131
<div class="line"></div>
3232
<div class="headerImage"></div>
3333
</div>
@@ -45,8 +45,9 @@ <h2>System Loads</h2>
4545
<table id="systemLoadsTable">
4646
<tbody id="systemLoadsTableBody">
4747
<tr>
48-
<th>Time</th><th>System Load</th>
49-
<th>Memory Usage (%)</th>
48+
<th>Time</th>
49+
<th>CPU Load (%)</th>
50+
<th>Heap Memory Usage (%)</th>
5051
</tr>
5152
</tbody>
5253
</table>
@@ -76,10 +77,10 @@ <h2>System Loads</h2>
7677
webSocket.onmessage = function (event) {
7778
var data = JSON.parse(event.data);
7879
var tableRow = document.createElement('tr');
79-
var loadAverage = data.loadAverage == null ? '-' : data.loadAverage.toFixed(2);
80+
var cpuLoad = data.cpuLoad == null ? '-' : data.cpuLoad.toFixed(7);
8081
var memoryUsage = data.memoryUsage == null ? '-' : data.memoryUsage.toFixed(2);
8182
tableRow.innerHTML = '<td>' + data.time + '</td>' +
82-
'<td>' + loadAverage + '</td>' +
83+
'<td>' + cpuLoad + '</td>' +
8384
'<td>' + memoryUsage + '</td>';
8485
document.getElementById('systemLoadsTableBody').appendChild(tableRow);
8586
};

finish/system/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<packaging>war</packaging>
1111

1212
<properties>
13-
<maven.compiler.source>11</maven.compiler.source>
14-
<maven.compiler.target>11</maven.compiler.target>
13+
<maven.compiler.source>17</maven.compiler.source>
14+
<maven.compiler.target>17</maven.compiler.target>
1515
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1616
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1717
<!-- Liberty configuration -->

finish/system/src/main/java/io/openliberty/guides/system/SystemService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import java.lang.management.ManagementFactory;
1515
import java.lang.management.MemoryMXBean;
16-
import java.lang.management.OperatingSystemMXBean;
1716
import java.util.Calendar;
1817
import java.util.HashSet;
1918
import java.util.Set;
@@ -30,6 +29,8 @@
3029
import jakarta.websocket.Session;
3130
import jakarta.websocket.server.ServerEndpoint;
3231

32+
import com.sun.management.OperatingSystemMXBean;
33+
3334
// tag::serverEndpoint[]
3435
@ServerEndpoint(value = "/systemLoad",
3536
decoders = { SystemLoadDecoder.class },
@@ -42,7 +43,7 @@ public class SystemService {
4243
private static Set<Session> sessions = new HashSet<>();
4344

4445
private static final OperatingSystemMXBean OS =
45-
ManagementFactory.getOperatingSystemMXBean();
46+
(OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
4647

4748
private static final MemoryMXBean MEM =
4849
ManagementFactory.getMemoryMXBean();
@@ -79,11 +80,11 @@ public void onMessage(String option, Session session) {
7980
try {
8081
JsonObjectBuilder builder = Json.createObjectBuilder();
8182
builder.add("time", Calendar.getInstance().getTime().toString());
82-
// tag::loadAverage[]
83-
if (option.equalsIgnoreCase("loadAverage")
83+
// tag::cpuLoad[]
84+
if (option.equalsIgnoreCase("cpuLoad")
8485
|| option.equalsIgnoreCase("both")) {
85-
// end::loadAverage[]
86-
builder.add("loadAverage", Double.valueOf(OS.getSystemLoadAverage()));
86+
// end::cpuLoad[]
87+
builder.add("cpuLoad", Double.valueOf(OS.getCpuLoad() * 100.0));
8788
}
8889
// tag::memoryUsageOrBoth[]
8990
if (option.equalsIgnoreCase("memoryUsage")

finish/system/src/test/java/it/io/openliberty/guides/system/SystemServiceIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void testSystemMultipleSessions() throws Exception {
5555
SystemClient client1 = new SystemClient(uri);
5656
SystemClient client2 = new SystemClient(uri);
5757
SystemClient client3 = new SystemClient(uri);
58-
client2.sendMessage("loadAverage");
58+
client2.sendMessage("cpuLoad");
5959
countDown.await(5, TimeUnit.SECONDS);
6060
client1.close();
6161
client2.close();
@@ -72,7 +72,7 @@ private static void startCountDown(int count) {
7272
public static void verify(JsonObject systemLoad) {
7373
assertNotNull(systemLoad.getString("time"));
7474
assertTrue(
75-
systemLoad.getJsonNumber("loadAverage") != null
75+
systemLoad.getJsonNumber("cpuLoad") != null
7676
|| systemLoad.getJsonNumber("memoryUsage") != null
7777
);
7878
countDown.countDown();

scripts/dailyBuild.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#!/bin/bash
2-
while getopts t:d:b: flag;
2+
while getopts t:d:j:b: flag;
33
do
44
case "${flag}" in
55
t) DATE="${OPTARG}";;
66
d) DRIVER="${OPTARG}";;
7-
b) BUILD="${OPTARG}";;
7+
j) JDK_LEVEL="${OPTARG}";;
8+
*) echo "Invalid option";;
89
esac
910
done
1011

12+
if [ "$JDK_LEVEL" == "11" ]; then
13+
echo "Test skipped because the guide does not support Java 11."
14+
exit 0
15+
fi
16+
1117
sed -i "\#<artifactId>liberty-maven-plugin</artifactId>#a<configuration><install><runtimeUrl>https://public.dhe.ibm.com/ibmdl/export/pub/software/openliberty/runtime/nightly/"$DATE"/"$DRIVER"</runtimeUrl></install></configuration>" client/pom.xml system/pom.xml
1218
cat query/pom.xml system/pom.xml
1319

scripts/testApp.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ mvn -Dhttp.keepAlive=false \
2525
-ntp -pl system failsafe:integration-test
2626

2727
sleep 20
28-
grep loadAverage client/target/liberty/wlp/usr/servers/defaultServer/logs/messages.log || \
28+
grep cpuLoad client/target/liberty/wlp/usr/servers/defaultServer/logs/messages.log || \
2929
sleep 20 || \
30-
grep loadAverage client/target/liberty/wlp/usr/servers/defaultServer/logs/messages.log || exit 1
30+
grep cpuLoad client/target/liberty/wlp/usr/servers/defaultServer/logs/messages.log || exit 1
3131
grep memoryUsage client/target/liberty/wlp/usr/servers/defaultServer/logs/messages.log || \
3232
sleep 20 || \
3333
grep memoryUsage client/target/liberty/wlp/usr/servers/defaultServer/logs/messages.log || exit 1

0 commit comments

Comments
 (0)