Search in sources :

Example 1 with StompSessionHandler

use of org.springframework.messaging.simp.stomp.StompSessionHandler in project spring-integration by spring-projects.

the class StompSessionManagerTests method testDoConnectFailure.

@Test
public void testDoConnectFailure() throws Exception {
    StompClientSupport stompClient = mock(StompClientSupport.class);
    stompClient.setTaskScheduler(new ConcurrentTaskScheduler());
    AbstractStompSessionManager sessionManager = new AbstractStompSessionManager(stompClient) {

        private final AtomicBoolean thrown = new AtomicBoolean();

        @Override
        protected ListenableFuture<StompSession> doConnect(StompSessionHandler handler) {
            if (!this.thrown.getAndSet(true)) {
                throw new RuntimeException("intentional");
            } else {
                SettableListenableFuture<StompSession> future = new SettableListenableFuture<StompSession>();
                StompSession stompSession = mock(StompSession.class);
                future.set(stompSession);
                handler.afterConnected(stompSession, getConnectHeaders());
                return future;
            }
        }
    };
    sessionManager.start();
    final SettableListenableFuture<StompSession> stompSessionFuture = new SettableListenableFuture<StompSession>();
    sessionManager.connect(new StompSessionHandlerAdapter() {

        @Override
        public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
            stompSessionFuture.set(session);
        }
    });
    assertNotNull(stompSessionFuture.get(10, TimeUnit.SECONDS));
    sessionManager.stop();
}
Also used : ConcurrentTaskScheduler(org.springframework.scheduling.concurrent.ConcurrentTaskScheduler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) StompClientSupport(org.springframework.messaging.simp.stomp.StompClientSupport) StompSession(org.springframework.messaging.simp.stomp.StompSession) StompSessionHandler(org.springframework.messaging.simp.stomp.StompSessionHandler) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) StompSessionHandlerAdapter(org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter) Test(org.junit.Test)

Example 2 with StompSessionHandler

use of org.springframework.messaging.simp.stomp.StompSessionHandler in project elastest-torm by elastest.

the class StompTestUtils method connectToRabbitMQ.

public static StompSession connectToRabbitMQ(int serverPort) throws InterruptedException, ExecutionException, TimeoutException {
    WebSocketContainer cont = ContainerProvider.getWebSocketContainer();
    cont.setDefaultMaxTextMessageBufferSize(65500);
    WebSocketClient webSocketClient = new StandardWebSocketClient(cont);
    WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
    stompClient.setMessageConverter(new StringMessageConverter());
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    // for heartbeats
    stompClient.setTaskScheduler(taskScheduler);
    stompClient.setDefaultHeartbeat(new long[] { 10000, 10000 });
    String url = "ws://localhost:" + serverPort + "/rabbitMq";
    StompSessionHandler sessionHandler = new LogConnectedSessionHandler();
    final int MAX_RETRIES = 5;
    int retry = 0;
    while (true) {
        try {
            StompSession stompSession = stompClient.connect(url, sessionHandler).get(10, TimeUnit.SECONDS);
            log.info("Test connected to RabbitMQ in URL '{}'", url);
            return stompSession;
        } catch (Exception e) {
            if (retry < MAX_RETRIES) {
                retry++;
                log.warn("Exception trying to connect to RabbitMQ: {}:{}", e.getClass().getName(), e.getMessage());
                log.info("Retrying {}/{} in 5 second", retry, MAX_RETRIES);
                Thread.sleep(5000);
            } else {
                throw e;
            }
        }
    }
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) WebSocketContainer(javax.websocket.WebSocketContainer) StompSession(org.springframework.messaging.simp.stomp.StompSession) StompSessionHandler(org.springframework.messaging.simp.stomp.StompSessionHandler) WebSocketStompClient(org.springframework.web.socket.messaging.WebSocketStompClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) WebSocketClient(org.springframework.web.socket.client.WebSocketClient) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)

Example 3 with StompSessionHandler

use of org.springframework.messaging.simp.stomp.StompSessionHandler in project spring-boot by spring-projects.

the class WebSocketMessagingAutoConfigurationTests method performStompSubscription.

private Object performStompSubscription(final String topic) throws Throwable {
    EnvironmentTestUtils.addEnvironment(this.context, "server.port:0", "spring.jackson.serialization.indent-output:true");
    this.context.register(WebSocketMessagingConfiguration.class);
    new ServerPortInfoApplicationContextInitializer().initialize(this.context);
    this.context.refresh();
    WebSocketStompClient stompClient = new WebSocketStompClient(this.sockJsClient);
    final AtomicReference<Throwable> failure = new AtomicReference<>();
    final AtomicReference<Object> result = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    StompSessionHandler handler = new StompSessionHandlerAdapter() {

        @Override
        public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
            session.subscribe(topic, new StompFrameHandler() {

                @Override
                public void handleFrame(StompHeaders headers, Object payload) {
                    result.set(payload);
                    latch.countDown();
                }

                @Override
                public Type getPayloadType(StompHeaders headers) {
                    return Object.class;
                }
            });
        }

        @Override
        public void handleFrame(StompHeaders headers, Object payload) {
            latch.countDown();
        }

        @Override
        public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
            failure.set(exception);
            latch.countDown();
        }

        @Override
        public void handleTransportError(StompSession session, Throwable exception) {
            failure.set(exception);
            latch.countDown();
        }
    };
    stompClient.setMessageConverter(new SimpleMessageConverter());
    stompClient.connect("ws://localhost:{port}/messaging", handler, this.context.getEnvironment().getProperty("local.server.port"));
    if (!latch.await(30000, TimeUnit.SECONDS)) {
        if (failure.get() != null) {
            throw failure.get();
        }
        fail("Response was not received within 30 seconds");
    }
    return result.get();
}
Also used : ServerPortInfoApplicationContextInitializer(org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer) StompFrameHandler(org.springframework.messaging.simp.stomp.StompFrameHandler) StompSession(org.springframework.messaging.simp.stomp.StompSession) StompSessionHandler(org.springframework.messaging.simp.stomp.StompSessionHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) StompCommand(org.springframework.messaging.simp.stomp.StompCommand) Type(java.lang.reflect.Type) WebSocketStompClient(org.springframework.web.socket.messaging.WebSocketStompClient) SimpleMessageConverter(org.springframework.messaging.converter.SimpleMessageConverter) StompSessionHandlerAdapter(org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter)

Example 4 with StompSessionHandler

use of org.springframework.messaging.simp.stomp.StompSessionHandler in project tutorials by eugenp.

the class StompClient method main.

public static void main(String[] args) {
    WebSocketClient client = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(client);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    StompSessionHandler sessionHandler = new MyStompSessionHandler();
    stompClient.connect(URL, sessionHandler);
    // Don't close immediately.
    new Scanner(System.in).nextLine();
}
Also used : Scanner(java.util.Scanner) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) StompSessionHandler(org.springframework.messaging.simp.stomp.StompSessionHandler) WebSocketStompClient(org.springframework.web.socket.messaging.WebSocketStompClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) WebSocketClient(org.springframework.web.socket.client.WebSocketClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient)

Example 5 with StompSessionHandler

use of org.springframework.messaging.simp.stomp.StompSessionHandler in project CVE-2018-1270 by CaledoniaProject.

the class ServiceClient method main.

public static void main(String... argv) {
    WebSocketClient webSocketClient = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.setTaskScheduler(new ConcurrentTaskScheduler());
    String url = "ws://127.0.0.1:8080/hello";
    StompSessionHandler sessionHandler = new MySessionHandler();
    stompClient.connect(url, sessionHandler);
    // Don't close immediately.
    new Scanner(System.in).nextLine();
}
Also used : ConcurrentTaskScheduler(org.springframework.scheduling.concurrent.ConcurrentTaskScheduler) Scanner(java.util.Scanner) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) StompSessionHandler(org.springframework.messaging.simp.stomp.StompSessionHandler) WebSocketStompClient(org.springframework.web.socket.messaging.WebSocketStompClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) WebSocketClient(org.springframework.web.socket.client.WebSocketClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient)

Aggregations

StompSessionHandler (org.springframework.messaging.simp.stomp.StompSessionHandler)5 WebSocketStompClient (org.springframework.web.socket.messaging.WebSocketStompClient)4 StompSession (org.springframework.messaging.simp.stomp.StompSession)3 WebSocketClient (org.springframework.web.socket.client.WebSocketClient)3 StandardWebSocketClient (org.springframework.web.socket.client.standard.StandardWebSocketClient)3 Scanner (java.util.Scanner)2 MappingJackson2MessageConverter (org.springframework.messaging.converter.MappingJackson2MessageConverter)2 StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)2 StompSessionHandlerAdapter (org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter)2 ConcurrentTaskScheduler (org.springframework.scheduling.concurrent.ConcurrentTaskScheduler)2 Type (java.lang.reflect.Type)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 WebSocketContainer (javax.websocket.WebSocketContainer)1 Test (org.junit.Test)1 ServerPortInfoApplicationContextInitializer (org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer)1 SimpleMessageConverter (org.springframework.messaging.converter.SimpleMessageConverter)1