Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# shenyu-client-java

support jdk 1.8+ and spring boot 2.x
support jdk 1.8+ and spring boot 2.x

## WebSocket gateway runtime probe

The example command uses the `shenyu-example-spring-native-websocket` module in
the [Java 17 migration candidate](https://github.com/apache/shenyu-client-java/pull/16).
Build and start the application separately from ShenYu Admin and the gateway.
Enable the WebSocket plugin and wait for Admin to show the application's
`/ws-native` selector and rule. Run the command below with JDK 17 or later from the repository root.

```sh
java script/runtime/WebSocketProbe.java \
'ws://127.0.0.1:9195/ws-native/myWebSocket?token=Jack' \
'apache shenyu server send to Jack message : -> '
```

Use the gateway address and the full registered path. The second argument is the
prefix the application adds to its reply. The probe sends a fresh payload and
requires the exact echoed response and a normal close frame. A failed upgrade,
timeout, unexpected response, or abnormal close returns a nonzero exit status.
113 changes: 113 additions & 0 deletions script/runtime/WebSocketProbe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.WebSocket;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* Exercises a text echo endpoint through the gateway using only the JDK.
*/
public final class WebSocketProbe {

private static final int TIMEOUT_SECONDS = 5;

private WebSocketProbe() {
}

/**
* Connect to the supplied gateway URL and expect the fixture's prefix followed by the payload.
*
* @param args gateway WebSocket URL and expected response prefix
* @throws Exception if the handshake, message exchange or close fails
*/
public static void main(final String[] args) throws Exception {
if (args.length != 2) {
throw new IllegalArgumentException("Usage: java WebSocketProbe.java <gateway-ws-url> <response-prefix>");
}
URI endpoint = URI.create(args[0]);
String payload = "shenyu-runtime-" + UUID.randomUUID();
EchoListener listener = new EchoListener();
WebSocket socket = await(HttpClient.newHttpClient()
.newWebSocketBuilder()
.connectTimeout(Duration.ofSeconds(TIMEOUT_SECONDS))
.buildAsync(endpoint, listener), "WebSocket handshake");
try {
await(socket.sendText(payload, true), "sending the text message");
if (!await(listener.message, "receiving the echo").equals(args[1] + payload)) {
throw new IllegalStateException("Gateway returned an unexpected text echo");
}
await(socket.sendClose(WebSocket.NORMAL_CLOSURE, ""), "sending the close frame");
int status = await(listener.closeStatus, "receiving the close frame");
if (status != WebSocket.NORMAL_CLOSURE) {
throw new IllegalStateException("Gateway returned close status " + status);
}
} finally {
socket.abort();
}
System.out.println("WebSocket gateway handshake, text echo and normal close passed");
}

private static <T> T await(final CompletableFuture<T> future, final String phase) throws InterruptedException {
try {
return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (ExecutionException | TimeoutException ex) {
future.cancel(true);
throw new IllegalStateException("Failed " + phase, ex);
}
}

private static final class EchoListener implements WebSocket.Listener {

private final CompletableFuture<String> message = new CompletableFuture<>();

private final CompletableFuture<Integer> closeStatus = new CompletableFuture<>();

private final StringBuilder text = new StringBuilder();

@Override
public CompletionStage<?> onText(final WebSocket socket, final CharSequence data, final boolean last) {
text.append(data);
if (last) {
message.complete(text.toString());
text.setLength(0);
}
socket.request(1);
return null;
}

@Override
public CompletionStage<?> onClose(final WebSocket socket, final int status, final String reason) {
message.completeExceptionally(new IllegalStateException("Peer closed before sending a complete text echo, status " + status));
closeStatus.complete(status);
return null;
}

@Override
public void onError(final WebSocket socket, final Throwable error) {
message.completeExceptionally(error);
closeStatus.completeExceptionally(error);
}
}
}