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

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

Introduction

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

Prototype

void disconnect();

Source Link

Document

Disconnect the session by sending a DISCONNECT frame.

Usage

From source file:com.mycompany.trader.TradingConnect.java

private static void getPositions(AtomicReference<Throwable> failure) {
    StompSessionHandler handler = new AbstractTestSessionHandler(failure) {

        //this method is called when we get a connection successfully. It will auto- connect to the positions updates 
        @Override//  w  w w. jav  a 2  s .  com
        public void afterConnected(final StompSession session, StompHeaders connectedHeaders) {
            System.out.println("got connect");

            session.subscribe("/user/queue/user-position-updates", new StompFrameHandler() {
                @Override
                public Type getPayloadType(StompHeaders headers) {
                    return PortfolioPosition[].class;
                }

                @Override
                public void handleFrame(StompHeaders headers, Object payload) {
                    try {
                        PortfolioPosition[] updates = (PortfolioPosition[]) payload;
                        String[] updateArray = new String[updates.length];
                        int i = 0;
                        for (PortfolioPosition u : updates) {
                            updateArray[i] = u.toString();
                            System.out.println(u.toString());
                        }
                    } catch (Throwable t) {
                        t.printStackTrace();
                    } finally {
                        session.disconnect();
                    }
                }
            });
            System.out.println("got subscribe");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    stompClient.setMessageConverter(converter);
    stompClient.connect("ws://localhost:{port}/blueprint-trading-services/portfolio", headers, handler, port);
    while (true)
        ;
}

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 ww. j  ava2 s  .  co 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();
            }
        }
    };
    //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.appverse.web.framework.backend.frontfacade.websocket.IntegrationWebsocketTest.java

@Test
public void getPositions() throws Exception {

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

    StompSessionHandler handler = new AbstractTestSessionHandler(failure) {

        @Override/*from  w  ww. ja  v  a  2 s.  co m*/
        public void afterConnected(final StompSession session, StompHeaders connectedHeaders) {
            session.subscribe("/app/positions", new StompFrameHandler() {
                @Override
                public Type getPayloadType(StompHeaders headers) {
                    return byte[].class;
                }

                @Override
                public void handleFrame(StompHeaders headers, Object payload) {
                    String json = new String((byte[]) payload);
                    logger.debug("Got " + json);
                    try {
                        new JsonPathExpectationsHelper("$[0].company").assertValue(json,
                                "Citrix Systems, Inc.");
                        new JsonPathExpectationsHelper("$[1].company").assertValue(json, "Dell Inc.");
                        new JsonPathExpectationsHelper("$[2].company").assertValue(json, "Microsoft");
                        new JsonPathExpectationsHelper("$[3].company").assertValue(json, "Oracle");
                    } catch (Throwable t) {
                        failure.set(t);
                    } finally {
                        session.disconnect();
                        latch.countDown();
                    }
                }
            });
        }
    };

    WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
    stompClient.connect("http://localhost:{port}/services/websocket", this.headers, handler, port);

    if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }

    if (!latch.await(5, TimeUnit.SECONDS)) {
        fail("Portfolio positions not received");
    }
}

From source file:org.springframework.integration.stomp.AbstractStompSessionManager.java

@Override
public void destroy() {
    if (this.stompSessionListenableFuture != null) {
        if (this.reconnectFuture != null) {
            this.reconnectFuture.cancel(false);
            this.reconnectFuture = null;
        }// w w w . j a v  a2  s  .c  o  m
        this.stompSessionListenableFuture.addCallback(new ListenableFutureCallback<StompSession>() {

            @Override
            public void onFailure(Throwable ex) {
                AbstractStompSessionManager.this.connected = false;
            }

            @Override
            public void onSuccess(StompSession session) {
                session.disconnect();
                AbstractStompSessionManager.this.connected = false;
            }

        });
        this.stompSessionListenableFuture = null;
    }
}

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

@Test
public void getPositions() throws Exception {

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

    StompSessionHandler handler = new AbstractTestSessionHandler(failure) {

        @Override/*from w ww.  ja va2  s. c  om*/
        public void afterConnected(final StompSession session, StompHeaders connectedHeaders) {
            session.subscribe("/app/positions", new StompFrameHandler() {
                @Override
                public Type getPayloadType(StompHeaders headers) {
                    return byte[].class;
                }

                @Override
                public void handleFrame(StompHeaders headers, Object payload) {
                    String json = new String((byte[]) payload);
                    logger.debug("Got " + json);
                    try {
                        new JsonPathExpectationsHelper("$[0].company").assertValue(json,
                                "Citrix Systems, Inc.");
                        new JsonPathExpectationsHelper("$[1].company").assertValue(json, "Dell Inc.");
                        new JsonPathExpectationsHelper("$[2].company").assertValue(json, "Microsoft");
                        new JsonPathExpectationsHelper("$[3].company").assertValue(json, "Oracle");
                    } catch (Throwable t) {
                        failure.set(t);
                    } finally {
                        session.disconnect();
                        latch.countDown();
                    }
                }
            });
        }
    };

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

    if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }

    if (!latch.await(5, TimeUnit.SECONDS)) {
        fail("Portfolio positions not received");
    }
}

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  www. j av a  2 s . 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();
            }
        }
    };

    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());
    }
}