Example usage for javax.websocket Session close

List of usage examples for javax.websocket Session close

Introduction

In this page you can find the example usage for javax.websocket Session close.

Prototype

@Override
void close() throws IOException;

Source Link

Document

Close the connection to the remote end point using the code javax.websocket.CloseReason.CloseCodes#NORMAL_CLOSURE and an empty reason phrase.

Usage

From source file:org.wso2.carbon.event.output.adaptor.websocket.WebsocketEventAdaptor.java

@Override
public void testConnection(OutputEventAdaptorConfiguration outputEventAdaptorConfiguration, int tenantId) {
    Map<String, String> adaptorProps = outputEventAdaptorConfiguration.getOutputProperties();
    String url = adaptorProps.get(WebsocketEventAdaptorConstants.ADAPTER_SERVER_URL);
    ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
    ClientManager client = ClientManager.createClient();
    Session session = null;
    try {/*from w w  w . j  a  v  a 2  s.c  o m*/
        session = client.connectToServer(new WebsocketClient(), cec, new URI(url));
    } catch (URISyntaxException e) {
        throw new OutputEventAdaptorEventProcessingException(e);
    } catch (DeploymentException e) {
        throw new OutputEventAdaptorEventProcessingException(e);
    } catch (IOException e) {
        throw new OutputEventAdaptorEventProcessingException(e);
    } finally {
        try {
            session.close();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}

From source file:org.wso2.carbon.event.output.adaptor.websocket.WebsocketEventAdaptor.java

@Override
public void removeConnectionInfo(OutputEventAdaptorMessageConfiguration outputEventAdaptorMessageConfiguration,
        OutputEventAdaptorConfiguration outputEventAdaptorConfiguration, int tenantId) {
    /**//  w  w  w.ja  v a  2 s .  c  o m
     * Clearing all the sessions created.
     */
    for (ConcurrentHashMap<String, Session> urlSessionMap : outputEventAdaptorSessionMap.values()) {
        for (Session session : urlSessionMap.values()) {
            try {
                session.close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}

From source file:org.wso2.carbon.identity.agent.outbound.server.UserStoreServerEndpoint.java

/**
 * Send error message to client//from  w  w  w.j  a v a2s .  c  om
 * @param session web socket session
 * @param message Error message
 * @throws IOException
 */
private void sendErrorMessage(Session session, String message) throws IOException, JSONException {
    LOGGER.error(message);
    UserOperation userOperation = new UserOperation();
    userOperation.setRequestType(UserStoreConstants.UM_OPERATION_TYPE_ERROR);
    JSONObject jsonMessage = new JSONObject();
    jsonMessage.put("message", message);
    userOperation.setRequestData(jsonMessage.toString());
    session.getBasicRemote().sendText(MessageRequestUtil.getUserOperationJSONMessage(userOperation));
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        LOGGER.error("Error occurred while sleep before close session");
    }
    session.close();
}

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

public void send(String url, String message) {
    receivedMessage = null;/*from  w  w w . ja v  a  2s.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 ww  w . j a va2  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();

}