Small refactoring in code no 2 + and removing deprecated websocketclient creator.
parent
22e0a6ffbd
commit
34e397682b
|
@ -12,7 +12,9 @@ public class WebsocketClientApp {
|
||||||
WebsocketClientParticle websocketClientParticle = new WebsocketClientParticle("localhost", 8080, "/");
|
WebsocketClientParticle websocketClientParticle = new WebsocketClientParticle("localhost", 8080, "/");
|
||||||
vertx.deployVerticle(websocketClientParticle);
|
vertx.deployVerticle(websocketClientParticle);
|
||||||
logger.info("Deployed websocket particle");
|
logger.info("Deployed websocket particle");
|
||||||
websocketClientParticle.waitingUntilFinished();
|
websocketClientParticle
|
||||||
|
.waitingUntilFinished()
|
||||||
|
.join();
|
||||||
vertx.close();
|
vertx.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,16 @@ package ch.polgrabia.demos.client;
|
||||||
import io.vertx.core.AbstractVerticle;
|
import io.vertx.core.AbstractVerticle;
|
||||||
import io.vertx.core.AsyncResult;
|
import io.vertx.core.AsyncResult;
|
||||||
import io.vertx.core.http.HttpClient;
|
import io.vertx.core.http.HttpClient;
|
||||||
|
import io.vertx.core.http.HttpClientOptions;
|
||||||
import io.vertx.core.http.WebSocket;
|
import io.vertx.core.http.WebSocket;
|
||||||
|
import io.vertx.core.http.impl.WebSocketClientImpl;
|
||||||
|
import io.vertx.core.impl.CloseFuture;
|
||||||
|
import io.vertx.core.impl.VertxInternal;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class WebsocketClientParticle extends AbstractVerticle {
|
public class WebsocketClientParticle extends AbstractVerticle {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(WebsocketClientParticle.class);
|
private static final Logger logger = LoggerFactory.getLogger(WebsocketClientParticle.class);
|
||||||
|
@ -29,8 +34,8 @@ public class WebsocketClientParticle extends AbstractVerticle {
|
||||||
public void start() throws Exception {
|
public void start() throws Exception {
|
||||||
this.client = getVertx()
|
this.client = getVertx()
|
||||||
.createHttpClient();
|
.createHttpClient();
|
||||||
// TODO refactor it as it's deprecated
|
new WebSocketClientImpl((VertxInternal) getVertx(), new HttpClientOptions(), new CloseFuture())
|
||||||
client.webSocket(port, hostname, path, this::handleMessage);
|
.webSocket(port, hostname, path, this::handleMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleMessage(AsyncResult<WebSocket> webSocketAsyncResult) {
|
private void handleMessage(AsyncResult<WebSocket> webSocketAsyncResult) {
|
||||||
|
@ -67,17 +72,20 @@ public class WebsocketClientParticle extends AbstractVerticle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void waitingUntilFinished() {
|
public CompletableFuture<Boolean> waitingUntilFinished() {
|
||||||
while(!isShuttingDownActivated) {
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
synchronized (runningMonitor) {
|
while (!isShuttingDownActivated) {
|
||||||
try {
|
synchronized (runningMonitor) {
|
||||||
runningMonitor.wait();
|
try {
|
||||||
return;
|
runningMonitor.wait();
|
||||||
} catch (InterruptedException e) {
|
return true;
|
||||||
logger.warn("Got interrupted", e);
|
} catch (InterruptedException e) {
|
||||||
|
logger.warn("Got interrupted", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue