Search in sources :

Example 1 with WSClient

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);
        }
    }
}
Also used : WSClient(org.everrest.websockets.client.WSClient) IOException(java.io.IOException) FutureTask(java.util.concurrent.FutureTask) DeploymentException(javax.websocket.DeploymentException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 FutureTask (java.util.concurrent.FutureTask)1 DeploymentException (javax.websocket.DeploymentException)1 WSClient (org.everrest.websockets.client.WSClient)1