@Override public void afterConnected(final StompSession session, StompHeaders connectedHeaders) { this.connectLatch.countDown(); session.setAutoReceipt(true); session .subscribe( "/topic/greeting", new StompFrameHandler() { @Override public Type getPayloadType(StompHeaders headers) { return String.class; } @Override public void handleFrame(StompHeaders headers, Object payload) { if (messageCount.incrementAndGet() == expectedMessageCount) { messageLatch.countDown(); disconnectLatch.countDown(); session.disconnect(); } } }) .addReceiptTask( new Runnable() { @Override public void run() { subscribeLatch.countDown(); } }); }
@Override public void afterConnected(StompSession session, StompHeaders connectedHeaders) { this.session = session; int i = 0; try { for (; i < this.numberOfMessagesToBroadcast; i++) { session.send("/app/greeting", "hello"); } } catch (Throwable t) { logger.error("Message sending failed at " + i, t); failure.set(t); } }
private static void start() throws Exception { List<Transport> transports = Collections.<Transport>singletonList(new WebSocketTransport(new StandardWebSocketClient())); WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(transports)); stompClient.setMessageConverter(new MappingJackson2MessageConverter()); StompSessionHandlerAdapter handler = new StompSessionHandlerAdapter() { @Override public void afterConnected(StompSession session, StompHeaders connectedHeaders) { LOG.info("Connected to {}", session); } @Override public void handleException( StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) { throw new RuntimeException(exception); } }; ListenableFuture<StompSession> lss = stompClient.connect(URL, handler); StompSession ss = lss.get(); ss.subscribe("/user/queue/greetings", new StringHandler()); ss.subscribe("/topic/latest/USD/EUR", new RatesHandler()); ss.subscribe("/topic/latest/BRE/USD", new RatesHandler()); ss.send("/app/hello", "dede"); LOG.info("hello done"); }
@Override protected void handleMessageInternal(final Message<?> message) throws Exception { try { connectIfNecessary(); } catch (Exception e) { throw new MessageDeliveryException( message, "The [" + this + "] could not deliver message.", e); } StompSession stompSession = this.stompSession; StompHeaders stompHeaders = new StompHeaders(); this.headerMapper.fromHeaders(message.getHeaders(), stompHeaders); if (stompHeaders.getDestination() == null) { Assert.state( this.destinationExpression != null, "One of 'destination' or 'destinationExpression' must be" + " provided, if message header doesn't supply 'destination' STOMP header."); String destination = this.destinationExpression.getValue(this.evaluationContext, message, String.class); stompHeaders.setDestination(destination); } final StompSession.Receiptable receiptable = stompSession.send(stompHeaders, message.getPayload()); if (receiptable.getReceiptId() != null) { final String destination = stompHeaders.getDestination(); if (this.applicationEventPublisher != null) { receiptable.addReceiptTask( new Runnable() { @Override public void run() { StompReceiptEvent event = new StompReceiptEvent( StompMessageHandler.this, destination, receiptable.getReceiptId(), StompCommand.SEND, false); event.setMessage(message); applicationEventPublisher.publishEvent(event); } }); } receiptable.addReceiptLostTask( new Runnable() { @Override public void run() { if (applicationEventPublisher != null) { StompReceiptEvent event = new StompReceiptEvent( StompMessageHandler.this, destination, receiptable.getReceiptId(), StompCommand.SEND, true); event.setMessage(message); applicationEventPublisher.publishEvent(event); } else { logger.error( "The receipt [" + receiptable.getReceiptId() + "] is lost for [" + message + "] on destination [" + destination + "]"); } } }); } }