@HandleAfterCreate public void newEmployee(Employee employee) { this.websocket.convertAndSend( MESSAGE_PREFIX + "/newEmployee", getPath(employee)); }
@HandleAfterDelete public void deleteEmployee(Employee employee) { this.websocket.convertAndSend( MESSAGE_PREFIX + "/deleteEmployee", getPath(employee)); }
@HandleAfterSave public void updateEmployee(Employee employee) { this.websocket.convertAndSend( MESSAGE_PREFIX + "/updateEmployee", getPath(employee)); }
@HandleAfterCreate public void newEmployee(Employee employee) { this.websocket.convertAndSend( MESSAGE_PREFIX + "/newEmployee", getPath(employee)); }
@HandleAfterDelete public void deleteEmployee(Employee employee) { this.websocket.convertAndSend( MESSAGE_PREFIX + "/deleteEmployee", getPath(employee)); }
@HandleAfterSave public void updateEmployee(Employee employee) { this.websocket.convertAndSend( MESSAGE_PREFIX + "/updateEmployee", getPath(employee)); }
public void sendMessage() throws Exception{ for(int i=0;i<10;i++) { Thread.sleep(1000); template.convertAndSend("/topic/getResponse",new Response("Welcome,yangyibo !"+i)); System.out.println("----------------------yangyibo"+i); } }
@Override public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException { convertAndSendToUser(user, destination, payload, (MessagePostProcessor) null); }
@Bean public SimpMessagingTemplate brokerMessagingTemplate() { SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel()); String prefix = getBrokerRegistry().getUserDestinationPrefix(); if (prefix != null) { template.setUserDestinationPrefix(prefix); } template.setMessageConverter(brokerMessageConverter()); return template; }
/** * If the headers of the given message already contain a * {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor#DESTINATION_HEADER * SimpMessageHeaderAccessor#DESTINATION_HEADER} then the message is sent without * further changes. * <p>If a destination header is not already present ,the message is sent * to the configured {@link #setDefaultDestination(Object) defaultDestination} * or an exception an {@code IllegalStateException} is raised if that isn't * configured. * @param message the message to send (never {@code null}) */ @Override public void send(Message<?> message) { Assert.notNull(message, "Message is required"); String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders()); if (destination != null) { sendInternal(message); return; } doSend(getRequiredDefaultDestination(), message); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(this.brokerChannel.send(any())).thenReturn(true); this.converter = new MappingJackson2MessageConverter(); SimpMessagingTemplate brokerTemplate = new SimpMessagingTemplate(this.brokerChannel); brokerTemplate.setMessageConverter(this.converter); this.localRegistry = mock(SimpUserRegistry.class); this.multiServerRegistry = new MultiServerUserRegistry(this.localRegistry); this.handler = new UserRegistryMessageHandler(this.multiServerRegistry, brokerTemplate, "/topic/simp-user-registry", this.taskScheduler); }
@Override public void handleFrame(StompHeaders headers, Object payload) { if (headers.getDestination() != null) { String destination = headers.getDestination(); if (logger.isDebugEnabled()) { logger.debug("Received " + payload + ", To " + headers.getDestination()); } Principal principal = userAgentSession.getPrincipal(); String userDestinationPrefix = messagingTemplate.getUserDestinationPrefix(); if (principal != null && destination.startsWith(userDestinationPrefix)) { destination = destination.substring(userDestinationPrefix.length()); destination = destination.startsWith("/") ? destination : "/" + destination; messagingTemplate.convertAndSendToUser(principal.getName(), destination, payload, copyHeaders(headers.toSingleValueMap())); } else { messagingTemplate.convertAndSend(destination, payload, copyHeaders(headers.toSingleValueMap())); } } }
@Autowired SimpMessagingTemplate template; //all users template.convertAndSend("/topic/mytopic/", "message") //especific user template.convertAndSendToUser("userId", "/topic/mytopic/", "message"));
stompHeaderAccessor.setLeaveMutable(true); messagingTemplate.send("/topic/myTopic", MessageBuilder.createMessage("{\"foo\": \"bar\"}".getBytes(), stompHeaderAccessor.getMessageHeaders())); messagingTemplate.convertAndSend("/topic/myTopic", "foo"); receive = this.errorChannel.receive(100); assertNull(receive); messagingTemplate.convertAndSend("/topic/myTopic", "foo"); receive = this.errorChannel.receive(10000); assertNotNull(receive); messagingTemplate.convertAndSend("/topic/myTopic", "foo"); receive = this.errorChannel.receive(10000); assertNotNull(receive);
@Test public void convertAndSendWithCustomDestinationPrefix() { this.messagingTemplate.setUserDestinationPrefix("/prefix"); this.messagingTemplate.convertAndSendToUser("joe", "/queue/foo", "data"); List<Message<byte[]>> messages = this.messageChannel.getMessages(); assertEquals(1, messages.size()); Message<byte[]> message = messages.get(0); SimpMessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class); assertNotNull(headerAccessor); assertEquals(SimpMessageType.MESSAGE, headerAccessor.getMessageType()); assertEquals("/prefix/joe/queue/foo", headerAccessor.getDestination()); }
SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel); SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(); accessor.setSessionId("sess1"); template.convertAndSendToUser("sess1", "queue.q1", "456".getBytes(), accessor.getMessageHeaders());
/** * Create an instance with the given client and broker channels subscribing * to handle messages from each and then sending any resolved messages to the * broker channel. * @param clientInboundChannel messages received from clients. * @param brokerChannel messages sent to the broker. * @param resolver the resolver for "user" destinations. */ public UserDestinationMessageHandler(SubscribableChannel clientInboundChannel, SubscribableChannel brokerChannel, UserDestinationResolver resolver) { Assert.notNull(clientInboundChannel, "'clientInChannel' must not be null"); Assert.notNull(brokerChannel, "'brokerChannel' must not be null"); Assert.notNull(resolver, "resolver must not be null"); this.clientInboundChannel = clientInboundChannel; this.brokerChannel = brokerChannel; this.messagingTemplate = new SimpMessagingTemplate(brokerChannel); this.destinationResolver = resolver; }
@Test public void doSendWithStompHeaders() { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE); accessor.setDestination("/user/queue/foo"); Message<?> message = MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders()); this.messagingTemplate.doSend("/queue/foo-user123", message); List<Message<byte[]>> messages = this.messageChannel.getMessages(); Message<byte[]> sentMessage = messages.get(0); MessageHeaderAccessor sentAccessor = MessageHeaderAccessor.getAccessor(sentMessage, MessageHeaderAccessor.class); assertEquals(StompHeaderAccessor.class, sentAccessor.getClass()); assertEquals("/queue/foo-user123", ((StompHeaderAccessor) sentAccessor).getDestination()); }
@Bean public SimpMessagingTemplate brokerMessagingTemplate() { SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel()); String prefix = getBrokerRegistry().getUserDestinationPrefix(); if (prefix != null) { template.setUserDestinationPrefix(prefix); }
/** * If the headers of the given message already contain a * {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor#DESTINATION_HEADER * SimpMessageHeaderAccessor#DESTINATION_HEADER} then the message is sent without * further changes. * <p>If a destination header is not already present ,the message is sent * to the configured {@link #setDefaultDestination(Object) defaultDestination} * or an exception an {@code IllegalStateException} is raised if that isn't * configured. * @param message the message to send (never {@code null}) */ @Override public void send(Message<?> message) { Assert.notNull(message, "Message is required"); String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders()); if (destination != null) { sendInternal(message); return; } doSend(getRequiredDefaultDestination(), message); }