Question

As in the newest Spring 4 tutorial with STOMP and SockJS, we find a RequestMapping of the following kind :

Homecontroller.java

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

now I tried to realize multichannel support by modifying this requestmapping:

@MessageMapping("/hello/{id}")
    @SendTo("/topic/greetings/{id}")
    public Greeting greetingMultichannel(HelloMessage message) throws Exception {
        this.info();
        Thread.sleep(300); // simulated delay
        return new Greeting(" Hello, " + message.getName() + "!");
    }

the MessageMapping does not work,sendTo does not deliver back to the sepecified URL. Although I did subscribe to the right channel and the message goes out without problems, as seen in this


debug log:

Opening Web Socket... stomp.js:130 Web Socket Opened... stomp.js:130

CONNECT accept-version:1.1,1.0 heart-beat:10000,10000

stomp.js:130 <<< CONNECTED heart-beat:0,0 version:1.1

stomp.js:130 connected to server undefined stomp.js:130 Connected: CONNECTED version:1.1 heart-beat:0,0

(index):23

SUBSCRIBE id:sub-0 destination:/topic/greetings/1

stomp.js:130

SEND destination:/app/hello/1 content-length:18

{"name":"textext"}


there should be a response but there is nothing coming back..

Can someone see what I am missing here?

cheers, Heinrich

Was it helpful?

Solution

There is currently no way to pass parameters to @SendTo/ @SendToUser, placeholders on @MessageMapping are not available in @SendTo / @SendToUser. There's an open Jira for that, keep an eye on it.

In the meantime, use the SimpMessagingTemplate:

@Autowired 
private SimpMessagingTemplate simpMessagingTemplate;

@MessageMapping("/hello/{id}")
public void greetingMultichannel(HelloMessage message, @DestinationVariable("id") Long id) throws Exception {
    this.info();
    Thread.sleep(300); // simulated delay

    simpMessagingTemplate.convertAndSend("/topic/greetings/" + id, new Greeting(" Hello, " + message.getName() + "!"));
}

Update 8 September 2015:

As of Spring 4.2, destination variable placeholders can be used in @SendTo / @SendToUser. This is now possible:

@MessageMapping("/hello/{id}")
@SendTo("/topic/greetings/{id}")
public Greeting greetingMultichannel(HelloMessage message) throws Exception {
    this.info();
    Thread.sleep(300); // simulated delay
    return new Greeting(" Hello, " + message.getName() + "!");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top