Example usage for javax.websocket ContainerProvider getWebSocketContainer

List of usage examples for javax.websocket ContainerProvider getWebSocketContainer

Introduction

In this page you can find the example usage for javax.websocket ContainerProvider getWebSocketContainer.

Prototype

public static WebSocketContainer getWebSocketContainer() 

Source Link

Document

Create a new container used to create outgoing WebSocket connections.

Usage

From source file:org.apache.hadoop.gateway.websockets.WebsocketMultipleConnectionTest.java

/**
 * Test websocket proxying through gateway.
 * /*from  ww  w  .  j a  v  a2  s . c  o  m*/
 * @throws Exception
 */
@Test
public void testMultipleConnections() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();

    final CountDownLatch latch = new CountDownLatch(MAX_CONNECTIONS);

    Session[] sessions = new Session[MAX_CONNECTIONS];

    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();

    System.gc();
    final long heapt1 = memoryMXBean.getHeapMemoryUsage().getUsed();
    final long nonHeapt1 = memoryMXBean.getNonHeapMemoryUsage().getUsed();

    for (int i = 0; i < MAX_CONNECTIONS; i++) {

        sessions[i] = container.connectToServer(new WebsocketClient() {

            @Override
            public void onMessage(String message) {
                latch.countDown();

            }

        }, new URI(serverUri.toString() + "gateway/websocket/ws"));

    }

    for (int i = 0; i < MAX_CONNECTIONS; i++) {
        /* make sure the session is active and valid before trying to connect */
        if (sessions[i].isOpen() && sessions[i].getBasicRemote() != null) {
            sessions[i].getBasicRemote().sendText("OK");
        }
    }

    latch.await(5 * MAX_CONNECTIONS, TimeUnit.MILLISECONDS);

    System.gc();

    final long heapUsed = memoryMXBean.getHeapMemoryUsage().getUsed() - heapt1;
    final long nonHeapUsed = memoryMXBean.getNonHeapMemoryUsage().getUsed() - nonHeapt1;

    System.out.println("heapUsed = " + heapUsed);
    System.out.println("nonHeapUsed = " + nonHeapUsed);

    /* 90 KB per connection */
    /*
    long expected = 90 * 1024 * MAX_CONNECTIONS;
    assertThat("heap used", heapUsed, lessThan(expected));
    */
}

From source file:org.kurento.test.services.KmsService.java

private void waitForKms() {
    long initTime = System.nanoTime();

    @ClientEndpoint/*from   w  ww.  j  a v a2s  .c om*/
    class WebSocketClient extends Endpoint {

        @OnClose
        @Override
        public void onClose(Session session, CloseReason closeReason) {
        }

        @OnOpen
        @Override
        public void onOpen(Session session, EndpointConfig config) {
        }
    }

    if (wsUri != null) {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();

        final int retries = 600;
        final int waitTime = 100;

        for (int i = 0; i < retries; i++) {
            try {
                log.debug("({}) Wait for KMS: {}. Container: {}", i, wsUri, container);
                Session wsSession = container.connectToServer(new WebSocketClient(),
                        ClientEndpointConfig.Builder.create().build(), new URI(wsUri));
                wsSession.close();

                double time = (System.nanoTime() - initTime) / (double) 1000000;

                log.debug("Connected to KMS in " + String.format("%3.2f", time) + " milliseconds");
                return;
            } catch (DeploymentException | IOException | URISyntaxException e) {
                try {
                    log.warn("Exception while waiting for KMS: {}. {}", wsUri, e.getMessage());
                    Thread.sleep(waitTime);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }

        throw new KurentoException("Timeout of " + retries * waitTime + " millis waiting for KMS " + wsUri);

    } else {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("InterruptedException {}", e.getMessage());
        }
    }
}

From source file:org.springframework.web.reactive.socket.client.StandardWebSocketClient.java

/**
 * Default constructor that calls/*from w ww.j ava  2 s  .co  m*/
 * {@code ContainerProvider.getWebSocketContainer()} to obtain a (new)
 * {@link WebSocketContainer} instance.
 */
public StandardWebSocketClient() {
    this(ContainerProvider.getWebSocketContainer());
}

From source file:org.springframework.web.socket.client.endpoint.StandardWebSocketClient.java

public StandardWebSocketClient() {
    this.webSocketContainer = ContainerProvider.getWebSocketContainer();
}

From source file:org.wso2.carbon.device.mgt.analytics.wsproxy.outbound.AnalyticsClient.java

/**
 * Create {@link AnalyticsClient} instance.
 *//* w w w  .j  a  va  2  s  .c om*/
public AnalyticsClient(Session clientSession, URI endpointURI) throws WSProxyException {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    this.clientSession = clientSession;

    try {
        this.analyticsSession = container.connectToServer(this, endpointURI);
    } catch (DeploymentException | IOException e) {
        String msg = "Error occurred while connecting to remote endpoint " + endpointURI.toString();
        log.error(msg, e);
        throw new WSProxyException(msg, e);
    }
}

From source file:org.wso2.carbon.integration.test.client.WebSocketClient.java

public void send(String url, String message) {
    receivedMessage = null;// ww w  .j av a2  s.  c  o  m
    URI uri = URI.create(url);

    try {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        try {
            // Attempt Connect
            Session session = container.connectToServer(EventSocketClient.class, uri);
            // Send a message
            session.getBasicRemote().sendText(message);
            // Close session
            session.close();
        } finally {
            // Force lifecycle stop when done with container.
            // This is to free up threads and resources that the
            // JSR-356 container allocates. But unfortunately
            // the JSR-356 spec does not handle lifecycles (yet)
            if (container instanceof LifeCycle) {
                ((LifeCycle) container).stop();
            }
        }
    } catch (Throwable t) {
        log.error(t);
    }
}

From source file:org.wso2.carbon.integration.test.client.WebSocketClient.java

public void receive(String url, int retryCount) {

    receivedMessage = null;//from  w ww  . j  av a  2 s . c o  m
    final URI uri = URI.create(url);
    final int tryCount = retryCount;

    new Thread(new Runnable() {
        @Override

        public void run() {
            try {

                WebSocketContainer container = ContainerProvider.getWebSocketContainer();
                try {
                    // Attempt Connect
                    Session session = container.connectToServer(EventSocketClient.class, uri);

                    int count = 0;
                    while (count < tryCount && receivedMessage == null) {
                        log.info("Waiting for the sever to send message");
                        Thread.sleep(1000);
                    }

                    session.close();
                } finally {
                    // Force lifecycle stop when done with container.
                    // This is to free up threads and resources that the
                    // JSR-356 container allocates. But unfortunately
                    // the JSR-356 spec does not handle lifecycles (yet)
                    if (container instanceof LifeCycle) {
                        ((LifeCycle) container).stop();
                    }
                }
            } catch (Throwable t) {
                log.error(t);
            }
        }
    }).start();

}

From source file:org.wso2.carbon.sample.websocket.WebSocketClient.java

public void connect(String url) {
    URI uri = URI.create(url);

    try {/* w  w  w . ja  va  2s.c  o m*/
        container = ContainerProvider.getWebSocketContainer();
        // Attempt Connect
        session = container.connectToServer(EventSocketClient.class, uri);

        if (session == null) {
            throw new RuntimeException("Cannot connect to url :" + url);
        }
    } catch (Throwable t) {
        log.error(t);
        if (container != null) {
            if (container instanceof LifeCycle) {
                try {
                    ((LifeCycle) container).stop();
                } catch (Exception e) {
                    log.error(e);
                }
            }
        }
    }
}

From source file:wsclient.WebsocketClientEndpoint.java

public void go() {

    try {/*from www  .ja v  a2  s  .  co m*/
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        container.connectToServer(this, new URI("ws://46.254.19.232:8080"));
    } catch (Exception e) {
        System.out.println("OPA");
        throw new RuntimeException(e);
    }

}