SimpMessagingTemplate not sending messages in spring boot

Zahid Nisar picture Zahid Nisar · Apr 6, 2016 · Viewed 12.4k times · Source

Hi all I am trying to send messages to Stomp Endpoints but I not getting any.I am using spring boot with stomp following are my classes

@Controller
public class GreetingController {

  @MessageMapping("/hello")
  @SendTo("/topic/greetings")
  public Greeting greeting(HelloMessage message) throws Exception {
    System.out.println(message.getName());
    Thread.sleep(13000); // simulated delay
    return new Greeting("Hello, " + message.getName() + "!");
  }

}

@Controller                                                
public class Testcont {

  @Autowired
  private SimpMessagingTemplate messageSender;

  @RequestMapping(value="/Users/get",method=RequestMethod.POST)
  @ResponseBody
  public String getUser(@RequestParam(value = "userId") String userId, @RequestParam(value = "password") String password, @RequestParam(value="port") String port, HttpServletRequest request) {
    HelloMessage mess=new HelloMessage();
    mess.setName(userId);
    messageSender.convertAndSend("/app/hello",mess);
    return "Success";

}

and my Configuration for websocket

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

}

I am not getting any errors in consoles .the above code is working great with web browsers .

Answer

Artem Bilan picture Artem Bilan · Apr 6, 2016

The SimpMessagingTemplate bean is exactly for the Broker part (AbstractMessageBrokerConfiguration):

@Bean
public SimpMessagingTemplate brokerMessagingTemplate() {
    SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel());
    String prefix = getBrokerRegistry().getUserDestinationPrefix();
    if (prefix != null) {
        template.setUserDestinationPrefix(prefix);
    }

Since you send message not to the broker destination (/app/ in your case), such a message is just ignored by the AbstractBrokerMessageHandler.checkDestinationPrefix(destination).

If you'd like to handle that internal message by the same @MessageMapping, you should use the clientInboundChannel directly, which is supplied by the SimpAnnotationMethodMessageHandler:

@Bean
public SimpAnnotationMethodMessageHandler simpAnnotationMethodMessageHandler() {
    SimpAnnotationMethodMessageHandler handler = createAnnotationMethodMessageHandler();
    handler.setDestinationPrefixes(getBrokerRegistry().getApplicationDestinationPrefixes());

I guess you can create your own SimpMessagingTemplate instance for the clientInboundChannel, similar to that brokerMessagingTemplate bean. And you'll be fine.