@Test public void testHeadersToSend() throws Exception { Message<?> message = createMessage("sess1", "sub1", "/app", "/dest", null); SimpMessageSendingOperations messagingTemplate = Mockito.mock(SimpMessageSendingOperations.class); SendToMethodReturnValueHandler handler = new SendToMethodReturnValueHandler(messagingTemplate, false); handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, message); ArgumentCaptor<MessageHeaders> captor = ArgumentCaptor.forClass(MessageHeaders.class); verify(messagingTemplate).convertAndSend(eq("/topic/dest"), eq(PAYLOAD), captor.capture()); MessageHeaders headers = captor.getValue(); SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class); assertNotNull(accessor); assertTrue(accessor.isMutable()); assertEquals("sess1", accessor.getSessionId()); assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); assertEquals(this.noAnnotationsReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); }
destination = destinationHelper.expandTemplateVars(destination); if (broadcast) { this.messagingTemplate.convertAndSendToUser( user, destination, returnValue, createHeaders(null, returnType)); this.messagingTemplate.convertAndSendToUser( user, destination, returnValue, createHeaders(sessionId, returnType)); for (String destination : destinations) { destination = destinationHelper.expandTemplateVars(destination); this.messagingTemplate.convertAndSend(destination, returnValue, createHeaders(sessionId, returnType));
@ExceptionHandler public void handleException(Principal principal, Throwable exception) { messagingTemplate.convertAndSendToUser(principal.getName(), "/queue/errors", exception.getMessage()); }
@Scheduled(fixedDelay = 1500) public void sendTradeNotifications() { for (TradeResult result : this.tradeResults) { if (System.currentTimeMillis() >= result.timestamp + 1500) { logger.debug("Sending position update: " + result.position); this.messagingTemplate.convertAndSendToUser(result.user, "/queue/position-updates", result.position); this.tradeResults.remove(result); } } }
void onPGTransaction(Peer peer, Transaction tx) { this.messagingTemplate.convertAndSend("/topic/tx", tx); }
destination = destinationHelper.expandTemplateVars(destination); if (broadcast) { this.messagingTemplate.convertAndSendToUser( user, destination, returnValue, createHeaders(null, returnType)); this.messagingTemplate.convertAndSendToUser( user, destination, returnValue, createHeaders(sessionId, returnType)); for (String destination : destinations) { destination = destinationHelper.expandTemplateVars(destination); this.messagingTemplate.convertAndSend(destination, returnValue, createHeaders(sessionId, returnType));
@Scheduled(fixedDelay = 1500) public void sendTradeNotifications() { Map<String, Object> map = new HashMap<>(); map.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON); this.tradeResults.stream() .filter(result -> System.currentTimeMillis() >= (result.timestamp + 1500)) .forEach(result -> { logger.log(Level.INFO, "Sending position update: " + result.position); this.messagingTemplate.convertAndSendToUser(result.user, "/queue/position-updates", result.position, map); this.tradeResults.remove(result); }); }
@Scheduled(initialDelay = 1000, fixedDelay = 1000) public void driveBlueCar() { LatLng latLng = Route.routeBlue.get(this.blueRoute); this.blueRoute++; if (this.blueRoute >= Route.routeBlue.size()) { this.blueRoute = 0; } this.messagingTemplate.convertAndSend("/topic/map/blueCar", latLng); }
destination = destinationHelper.expandTemplateVars(destination); if (broadcast) { this.messagingTemplate.convertAndSendToUser( user, destination, returnValue, createHeaders(null, returnType)); this.messagingTemplate.convertAndSendToUser( user, destination, returnValue, createHeaders(sessionId, returnType)); for (String destination : destinations) { destination = destinationHelper.expandTemplateVars(destination); this.messagingTemplate.convertAndSend(destination, returnValue, createHeaders(sessionId, returnType));
private void publish(String username, TaskEvent event) { String type = event.getClass().getSimpleName(); this.messagingTemplate.convertAndSendToUser(username, "/queue/task-updates", new TaskEventNotification(type, event)); } }
@Scheduled(initialDelay = 2000, fixedDelay = 1200) public void driveRedCar() { LatLng latLng = Route.routeRed.get(this.redRoute); this.redRoute++; if (this.redRoute >= Route.routeRed.size()) { this.redRoute = 0; } this.messagingTemplate.convertAndSend("/topic/map/redCar", latLng); }
destination = destinationHelper.expandTemplateVars(destination); if (broadcast) { this.messagingTemplate.convertAndSendToUser( user, destination, returnValue, createHeaders(null, returnType)); this.messagingTemplate.convertAndSendToUser( user, destination, returnValue, createHeaders(sessionId, returnType)); for (String destination : destinations) { destination = destinationHelper.expandTemplateVars(destination); this.messagingTemplate.convertAndSend(destination, returnValue, createHeaders(sessionId, returnType));
public static void sendMessage(SimpMessageSendingOperations messagingTemplate, String user, String destination, Object payload){ messagingTemplate.convertAndSendToUser(user, destination, payload, createHeaders(user)); }
public void sendTopic(String topicUri, ProgressResponse progressResponse) { LOGGER.debug("Send Progress Topic : {}, {}", topicUri, progressResponse); try { messagingTemplate.convertAndSend(topicUri, GlobalObjectMapper.writeValueAsString(progressResponse)); } catch (Exception e) { LOGGER.error("Fail to send message : {}, {}", topicUri, progressResponse, e); } }
public void listPeers(Principal principal) { List<Peer> peers = peerGroup.getConnectedPeers(); this.messagingTemplate.convertAndSendToUser(principal.getName(), "/queue/peers", peers); }
public void sendTopic(String topicUri, ProgressResponse progressResponse) { LOGGER.debug("Send Progress Topic : {}, {}", topicUri, progressResponse); messagingTemplate.convertAndSend(topicUri, GlobalObjectMapper.writeValueAsString(progressResponse)); }
@Override public void executeTrade(Trade trade) { Portfolio portfolio = this.portfolioService.findPortfolio(trade.getUsername()); String ticker = trade.getTicker(); int sharesToTrade = trade.getShares(); PortfolioPosition newPosition = (trade.getAction() == Trade.TradeAction.Buy) ? portfolio.buy(ticker, sharesToTrade) : portfolio.sell(ticker, sharesToTrade); if (newPosition == null) { String payload = "Rejected trade " + trade; this.messagingTemplate.convertAndSendToUser(trade.getUsername(), "/queue/errors", payload); return; } this.tradeResults.add(new TradeResult(trade.getUsername(), newPosition)); }
@Service public class MessageService { @Autowired public SimpMessageSendingOperations messagingTemplate; public void sendMessage( String message ) { messagingTemplate.convertAndSend( "/topic/greetings", new Greeting("Some message") ); } }
/** * In real application a trade is probably executed in an external system, i.e. * asynchronously. */ @Override public void executeTrade(Trade trade) { Portfolio portfolio = this.portfolioService.findPortfolio(trade.getUsername()); String ticker = trade.getTicker(); int sharesToTrade = trade.getShares(); PortfolioPosition newPosition = trade.getAction() == TradeAction.Buy ? portfolio.buy(ticker, sharesToTrade) : portfolio.sell(ticker, sharesToTrade); if (newPosition == null) { String payload = "Rejected trade " + trade; this.messagingTemplate.convertAndSendToUser(trade.getUsername(), "/queue/errors", payload); return; } this.tradeResults.add(new TradeResult(trade.getUsername(), newPosition)); }
public void convertAndSend(final String s, final MessagePayload payload) throws MessagingException { final StompMessage stompMessage = StompMessage.wrap(payload); messageSendingOperations.convertAndSend(s, stompMessage); }