Skip to content

Commit

Permalink
Change websocket client init method
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Feb 25, 2025
1 parent eea41eb commit fd90c42
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ private boolean checkForConflicts(ListenerConfiguration listenerConfiguration, H
return false;
}

public WebSocketClientConnector getWebSocketClientConnector(WebSocketClientConnectorConfig configuration) {
return httpConnectorFactory.createWsClientConnector(configuration);
public WebSocketClientConnector getWebSocketClientConnector(WebSocketClientConnectorConfig configuration)
throws Exception {
return httpConnectorFactory.createWsClientConnectorWithSSL(configuration);
}

public TransportsConfiguration getTransportConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,16 @@ public static boolean failover(ObjectValue webSocketClient, WebSocketService wsS
if (currentIndex != failoverContext.getInitialIndex()) {
failoverContext.setCurrentIndex(currentIndex);
createDelay(failoverContext.getFailoverInterval());
establishWebSocketConnection(createWebSocketClientConnector(targets.get(currentIndex).toString(),
webSocketClient), webSocketClient, wsService);
return true;
try {
establishWebSocketConnection(createWebSocketClientConnector(targets.get(currentIndex).toString(),
webSocketClient), webSocketClient, wsService);
return true;
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug(LOG_MESSAGE, "Couldn't create the WebSocket client connector for the target: "
+ targets.get(currentIndex).toString(), e.getMessage());
}
}
}
if (logger.isDebugEnabled()) {
logger.debug(LOG_MESSAGE, "Couldn't connect to one of the server in the targets: ", targets);
Expand Down Expand Up @@ -522,7 +529,8 @@ public static void waitForHandshake(CountDownLatch countDownLatch) {
}

private static WebSocketClientConnector createWebSocketClientConnector(String remoteUrl,
ObjectValue webSocketClient) {
ObjectValue webSocketClient)
throws Exception {
@SuppressWarnings(WebSocketConstants.UNCHECKED)
MapValue<String, Object> clientEndpointConfig = (MapValue<String, Object>) webSocketClient.getMapValue(
HttpConstants.CLIENT_ENDPOINT_CONFIG);
Expand All @@ -532,7 +540,7 @@ private static WebSocketClientConnector createWebSocketClientConnector(String re
HttpWsConnectorFactory connectorFactory = ((HttpWsConnectorFactory) webSocketClient.
getNativeData(WebSocketConstants.CONNECTOR_FACTORY));
// Creates the client connector
return connectorFactory.createWsClientConnector(clientConnectorConfig);
return connectorFactory.createWsClientConnectorWithSSL(clientConnectorConfig);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,25 @@ public static void initEndpoint(ObjectValue webSocketClient) {
WebSocketClientConnectorConfig clientConnectorConfig = new WebSocketClientConnectorConfig(remoteUrl);
String scheme = URI.create(remoteUrl).getScheme();
WebSocketUtil.populateClientConnectorConfig(clientEndpointConfig, clientConnectorConfig, scheme);
// Creates the client connector.
WebSocketClientConnector clientConnector = connectorFactory.createWsClientConnector(clientConnectorConfig);
webSocketClient.addNativeData(WebSocketConstants.CONNECTOR_FACTORY, connectorFactory);
// Add the client connector as a native data
// because there is no need to create the client connector again when using one URL.
webSocketClient.addNativeData(WebSocketConstants.CLIENT_CONNECTOR, clientConnector);
if (webSocketClient.getNativeData(WebSocketConstants.CLIENT_LISTENER) == null) {
webSocketClient.addNativeData(WebSocketConstants.CLIENT_LISTENER, new ClientConnectorListener());
try {
// Creates the client connector.
WebSocketClientConnector clientConnector = connectorFactory
.createWsClientConnectorWithSSL(clientConnectorConfig);
webSocketClient.addNativeData(WebSocketConstants.CONNECTOR_FACTORY, connectorFactory);
// Add the client connector as a native data
// because there is no need to create the client connector again when using one URL.
webSocketClient.addNativeData(WebSocketConstants.CLIENT_CONNECTOR, clientConnector);
if (webSocketClient.getNativeData(WebSocketConstants.CLIENT_LISTENER) == null) {
webSocketClient.addNativeData(WebSocketConstants.CLIENT_LISTENER, new ClientConnectorListener());
}
CountDownLatch countDownLatch = new CountDownLatch(1);
webSocketClient.addNativeData(WebSocketConstants.COUNT_DOWN_LATCH, countDownLatch);
WebSocketUtil.establishWebSocketConnection(clientConnector, webSocketClient, wsService);
// Sets the count down latch for the initial connection.
WebSocketUtil.waitForHandshake(countDownLatch);
} catch (Exception e) {
throw WebSocketUtil.createErrorByType(e);
}
CountDownLatch countDownLatch = new CountDownLatch(1);
webSocketClient.addNativeData(WebSocketConstants.COUNT_DOWN_LATCH, countDownLatch);
WebSocketUtil.establishWebSocketConnection(clientConnector, webSocketClient, wsService);
// Sets the count down latch for the initial connection.
WebSocketUtil.waitForHandshake(countDownLatch);
}

private InitEndpoint() {
Expand Down

0 comments on commit fd90c42

Please sign in to comment.