Search code examples
javawebsocketproxystomp

Use proxy for Stomp over WebSocket protocol


I work on notification consumption with Stomp over WebSocket protocol in java. My problem is to go through a proxy (with defined name and port). I've looked a lot on the internet, but I can't find much...

This code works very well, I manage to consume the queue and receive messages. Nevertheless, when I deploy on server, it does not solve the address, it is necessary to pass imperatively by the dedicated proxy.

public StompSession connect(StompSessionHandler stompSessionHandler) throws ExecutionException, InterruptedException {
    WebSocketStompClient stompClient = initStompClient();
    StompHeaders connectHeaders = configureHeaders();
    return stompClient.connect(configuration.getUrlBroker(), (WebSocketHttpHeaders) null, connectHeaders, stompSessionHandler).get();
}

private WebSocketStompClient initStompClient() {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    container.setDefaultMaxTextMessageBufferSize(configuration.getMaxTextMessageSize());
    WebSocketClient webSocketClient = new StandardWebSocketClient(container);

    WebSocketStompClient webSocketStompClient = new WebSocketStompClient(webSocketClient);
    webSocketStompClient.setMessageConverter(new MappingJackson2MessageConverter());
    webSocketStompClient.setTaskScheduler(new ConcurrentTaskScheduler());
    webSocketStompClient.setInboundMessageSizeLimit(configuration.getMaxTextMessageSize());
    return webSocketStompClient;
}

private StompHeaders configureHeaders() {
    StompHeaders headers = new StompHeaders();
    headers.setLogin(configuration.getLogin());
    headers.setPasscode(configuration.getPassword());
    headers.setHeartbeat(new long[]{configuration.getHeartbeatOut(), configuration.getHeartbeatIn()});
    headers.setAcceptVersion(configuration.getAcceptVersion());
    return headers;
}

Do you have any leads?

Thank you in advance.


Solution

  • Le proxy peut se définir dans les propriétés système de la JVM :

    System.setProperty("http.proxyHost", proxyConfiguration.getHost());
    System.setProperty("http.proxyPort", valueOf(proxyConfiguration.getPort()));
    System.setProperty("https.proxyHost", proxyConfiguration.getHost());
    System.setProperty("https.proxyPort", valueOf(proxyConfiguration.getPort()));