Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpMessagingTemplate not sending messages in spring boot

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 .

like image 657
Zahid Nisar Avatar asked Apr 06 '16 16:04

Zahid Nisar


1 Answers

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.

like image 88
Artem Bilan Avatar answered Oct 10 '22 03:10

Artem Bilan