use of org.everrest.websockets.client.WSClient in project che by eclipse.
the class WSocketEventBusClient method connect.
private void connect(final URI wsUri, final Collection<String> channels) throws IOException, DeploymentException {
Future<WSClient> clientFuture = connections.get(wsUri);
if (clientFuture == null) {
FutureTask<WSClient> newFuture = new FutureTask<>(() -> {
WSClient wsClient = new WSClient(wsUri, new WSocketListener(wsUri, channels));
wsClient.connect((int) WS_CONNECTION_TIMEOUT);
return wsClient;
});
clientFuture = connections.putIfAbsent(wsUri, newFuture);
if (clientFuture == null) {
clientFuture = newFuture;
newFuture.run();
}
}
boolean connected = false;
try {
// wait for connection
clientFuture.get();
connected = true;
} catch (ExecutionException e) {
final Throwable cause = e.getCause();
if (cause instanceof Error) {
throw (Error) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof DeploymentException)
throw (DeploymentException) cause;
throw new RuntimeException(e);
} catch (InterruptedException e) {
LOG.info("Client interrupted " + e.getLocalizedMessage());
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} finally {
if (!connected) {
connections.remove(wsUri);
}
}
}
Aggregations