Search in sources :

Example 1 with WebSocketMessageRouter

use of org.apache.nifi.websocket.WebSocketMessageRouter in project nifi by apache.

the class JettyWebSocketClient method maintainSessions.

void maintainSessions() throws Exception {
    if (client == null) {
        return;
    }
    connectionLock.lock();
    final ComponentLog logger = getLogger();
    try {
        // Loop through existing sessions and reconnect.
        for (String clientId : activeSessions.keySet()) {
            final WebSocketMessageRouter router;
            try {
                router = routers.getRouterOrFail(clientId);
            } catch (final WebSocketConfigurationException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("The clientId {} is no longer active. Discarding the clientId.", new Object[] { clientId });
                }
                activeSessions.remove(clientId);
                continue;
            }
            final String sessionId = activeSessions.get(clientId);
            // If this session is still alive, do nothing.
            if (!router.containsSession(sessionId)) {
                // This session is no longer active, reconnect it.
                // If it fails, the sessionId will remain in activeSessions, and retries later.
                // This reconnect attempt is continued until user explicitly stops a processor or this controller service.
                connect(clientId, sessionId);
            }
        }
    } finally {
        connectionLock.unlock();
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Session maintenance completed. activeSessions={}", new Object[] { activeSessions });
    }
}
Also used : WebSocketMessageRouter(org.apache.nifi.websocket.WebSocketMessageRouter) WebSocketConfigurationException(org.apache.nifi.websocket.WebSocketConfigurationException) ComponentLog(org.apache.nifi.logging.ComponentLog)

Example 2 with WebSocketMessageRouter

use of org.apache.nifi.websocket.WebSocketMessageRouter in project nifi by apache.

the class JettyWebSocketClient method connect.

private void connect(final String clientId, String sessionId) throws IOException {
    connectionLock.lock();
    try {
        final WebSocketMessageRouter router;
        try {
            router = routers.getRouterOrFail(clientId);
        } catch (WebSocketConfigurationException e) {
            throw new IllegalStateException("Failed to get router due to: " + e, e);
        }
        final RoutingWebSocketListener listener = new RoutingWebSocketListener(router);
        listener.setSessionId(sessionId);
        final ClientUpgradeRequest request = new ClientUpgradeRequest();
        final Future<Session> connect = client.connect(listener, webSocketUri, request);
        getLogger().info("Connecting to : {}", new Object[] { webSocketUri });
        final Session session;
        try {
            session = connect.get(connectionTimeoutMillis, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            throw new IOException("Failed to connect " + webSocketUri + " due to: " + e, e);
        }
        getLogger().info("Connected, session={}", new Object[] { session });
        activeSessions.put(clientId, listener.getSessionId());
    } finally {
        connectionLock.unlock();
    }
}
Also used : WebSocketMessageRouter(org.apache.nifi.websocket.WebSocketMessageRouter) WebSocketConfigurationException(org.apache.nifi.websocket.WebSocketConfigurationException) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) IOException(java.io.IOException) WebSocketConfigurationException(org.apache.nifi.websocket.WebSocketConfigurationException) IOException(java.io.IOException) Session(org.eclipse.jetty.websocket.api.Session)

Aggregations

WebSocketConfigurationException (org.apache.nifi.websocket.WebSocketConfigurationException)2 WebSocketMessageRouter (org.apache.nifi.websocket.WebSocketMessageRouter)2 IOException (java.io.IOException)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 Session (org.eclipse.jetty.websocket.api.Session)1 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)1