Example usage for org.springframework.messaging.simp.stomp StompSession send

List of usage examples for org.springframework.messaging.simp.stomp StompSession send

Introduction

In this page you can find the example usage for org.springframework.messaging.simp.stomp StompSession send.

Prototype

Receiptable send(StompHeaders headers, Object payload);

Source Link

Document

An overloaded version of #send(String,Object) with full StompHeaders instead of just a destination.

Usage

From source file:org.appverse.web.framework.backend.frontfacade.websocket.IntegrationWebsocketTest.java

@Test
public void executeTrade() throws Exception {

    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<Throwable>();

    StompSessionHandler handler = new AbstractTestSessionHandler(failure) {

        @Override/*from   w  w w  . j av  a 2s.c  om*/
        public void afterConnected(final StompSession session, StompHeaders connectedHeaders) {
            session.subscribe("/user/queue/position-updates", new StompFrameHandler() {
                @Override
                public Type getPayloadType(StompHeaders headers) {
                    return PortfolioPosition.class;
                }

                @Override
                public void handleFrame(StompHeaders headers, Object payload) {
                    PortfolioPosition position = (PortfolioPosition) payload;
                    logger.debug("Got " + position);
                    try {
                        assertEquals(75, position.getShares());
                        assertEquals("Dell Inc.", position.getCompany());
                    } catch (Throwable t) {
                        failure.set(t);
                    } finally {
                        session.disconnect();
                        latch.countDown();
                    }
                }
            });

            try {
                Trade trade = new Trade();
                trade.setAction(Trade.TradeAction.Buy);
                trade.setTicker("DELL");
                trade.setShares(25);
                session.send("/app/trade", trade);
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }
    };
    //test websocket
    WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.connect("ws://localhost:{port}/services/websocket/standard", headers, handler, port);

    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("Trade confirmation not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
    //test sockJs
    stompClient = new WebSocketStompClient(sockJsClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.connect("http://localhost:{port}/services/websocket/sockJs", headers, handler, port);

    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("Trade confirmation not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }

}

From source file:org.springframework.samples.portfolio.web.tomcat.IntegrationPortfolioTests.java

@Test
public void executeTrade() throws Exception {

    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<Throwable>();

    StompSessionHandler handler = new AbstractTestSessionHandler(failure) {

        @Override//from w w  w.j  a  v  a  2s.  c  o  m
        public void afterConnected(final StompSession session, StompHeaders connectedHeaders) {
            session.subscribe("/user/queue/position-updates", new StompFrameHandler() {
                @Override
                public Type getPayloadType(StompHeaders headers) {
                    return PortfolioPosition.class;
                }

                @Override
                public void handleFrame(StompHeaders headers, Object payload) {
                    PortfolioPosition position = (PortfolioPosition) payload;
                    logger.debug("Got " + position);
                    try {
                        assertEquals(75, position.getShares());
                        assertEquals("Dell Inc.", position.getCompany());
                    } catch (Throwable t) {
                        failure.set(t);
                    } finally {
                        session.disconnect();
                        latch.countDown();
                    }
                }
            });

            try {
                Trade trade = new Trade();
                trade.setAction(Trade.TradeAction.Buy);
                trade.setTicker("DELL");
                trade.setShares(25);
                session.send("/app/trade", trade);
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }
    };

    WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.connect("ws://localhost:{port}/portfolio", headers, handler, port);

    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("Trade confirmation not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
}