List of usage examples for org.springframework.web.socket.messaging WebSocketStompClient connect
public ListenableFuture<StompSession> connect(URI url, @Nullable WebSocketHttpHeaders handshakeHeaders,
@Nullable StompHeaders connectHeaders, StompSessionHandler sessionHandler)
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 . j a v a 2 s . c o m*/ 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 getPositions() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Throwable> failure = new AtomicReference<>(); StompSessionHandler handler = new AbstractTestSessionHandler(failure) { @Override//from www . j av a2 s . com 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.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/* w w w. j a v 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(); } } }; //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.load.StompWebSocketLoadTestClient.java
public static void main(String[] args) throws Exception { // Modify host and port below to match wherever StompWebSocketServer.java is running!! // When StompWebSocketServer starts it prints the selected available String host = "localhost"; if (args.length > 0) { host = args[0];/*from w w w. j a va 2 s. c om*/ } int port = 37232; if (args.length > 1) { port = Integer.valueOf(args[1]); } String homeUrl = "http://{host}:{port}/home"; logger.debug("Sending warm-up HTTP request to " + homeUrl); HttpStatus status = new RestTemplate().getForEntity(homeUrl, Void.class, host, port).getStatusCode(); Assert.state(status == HttpStatus.OK); final CountDownLatch connectLatch = new CountDownLatch(NUMBER_OF_USERS); final CountDownLatch subscribeLatch = new CountDownLatch(NUMBER_OF_USERS); final CountDownLatch messageLatch = new CountDownLatch(NUMBER_OF_USERS); final CountDownLatch disconnectLatch = new CountDownLatch(NUMBER_OF_USERS); final AtomicReference<Throwable> failure = new AtomicReference<>(); StandardWebSocketClient webSocketClient = new StandardWebSocketClient(); HttpClient jettyHttpClient = new HttpClient(); jettyHttpClient.setMaxConnectionsPerDestination(1000); jettyHttpClient.setExecutor(new QueuedThreadPool(1000)); jettyHttpClient.start(); List<Transport> transports = new ArrayList<>(); transports.add(new WebSocketTransport(webSocketClient)); transports.add(new JettyXhrTransport(jettyHttpClient)); SockJsClient sockJsClient = new SockJsClient(transports); try { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.afterPropertiesSet(); String stompUrl = "ws://{host}:{port}/stomp"; WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient); stompClient.setMessageConverter(new StringMessageConverter()); stompClient.setTaskScheduler(taskScheduler); stompClient.setDefaultHeartbeat(new long[] { 0, 0 }); logger.debug("Connecting and subscribing " + NUMBER_OF_USERS + " users "); StopWatch stopWatch = new StopWatch("STOMP Broker Relay WebSocket Load Tests"); stopWatch.start(); List<ConsumerStompSessionHandler> consumers = new ArrayList<>(); for (int i = 0; i < NUMBER_OF_USERS; i++) { consumers.add(new ConsumerStompSessionHandler(BROADCAST_MESSAGE_COUNT, connectLatch, subscribeLatch, messageLatch, disconnectLatch, failure)); stompClient.connect(stompUrl, consumers.get(i), host, port); } if (failure.get() != null) { throw new AssertionError("Test failed", failure.get()); } if (!connectLatch.await(5000, TimeUnit.MILLISECONDS)) { fail("Not all users connected, remaining: " + connectLatch.getCount()); } if (!subscribeLatch.await(5000, TimeUnit.MILLISECONDS)) { fail("Not all users subscribed, remaining: " + subscribeLatch.getCount()); } stopWatch.stop(); logger.debug("Finished: " + stopWatch.getLastTaskTimeMillis() + " millis"); logger.debug("Broadcasting " + BROADCAST_MESSAGE_COUNT + " messages to " + NUMBER_OF_USERS + " users "); stopWatch.start(); ProducerStompSessionHandler producer = new ProducerStompSessionHandler(BROADCAST_MESSAGE_COUNT, failure); stompClient.connect(stompUrl, producer, host, port); stompClient.setTaskScheduler(taskScheduler); if (failure.get() != null) { throw new AssertionError("Test failed", failure.get()); } if (!messageLatch.await(60 * 1000, TimeUnit.MILLISECONDS)) { for (ConsumerStompSessionHandler consumer : consumers) { if (consumer.messageCount.get() < consumer.expectedMessageCount) { logger.debug(consumer); } } } if (!messageLatch.await(60 * 1000, TimeUnit.MILLISECONDS)) { fail("Not all handlers received every message, remaining: " + messageLatch.getCount()); } producer.session.disconnect(); if (!disconnectLatch.await(5000, TimeUnit.MILLISECONDS)) { fail("Not all disconnects completed, remaining: " + disconnectLatch.getCount()); } stopWatch.stop(); logger.debug("Finished: " + stopWatch.getLastTaskTimeMillis() + " millis"); System.out.println("\nPress any key to exit..."); System.in.read(); } catch (Throwable t) { t.printStackTrace(); } finally { jettyHttpClient.stop(); } logger.debug("Exiting"); System.exit(0); }
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 v a2 s . com*/ 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 w ww . j av a 2 s. 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()); } }