1 package de.spring.stomp.controllers;
3 import java.time.LocalDateTime;
5 import org.springframework.messaging.handler.annotation.MessageMapping;
6 import org.springframework.messaging.handler.annotation.SendTo;
7 import org.springframework.stereotype.Controller;
10 public class MessageGreetingController {
12 // Sending data to /app/greeting from STOMP client (client must first connect to endpoint, in my case portfolio)
13 // connecting to this URL -> http://172.17.0.3/spring-stomp-server/portfolio
14 // sending data to /app/greeting
16 // The data sent to /app/greeting will be retrieved by this method.
17 @MessageMapping("/greeting")
18 @SendTo("/topic/greeting")
19 public String handle(String greeting) {
20 // STOMP clients subscribed to /topic/greeting will receive the returned data from this method.
21 // Destination is selected based on a convention but can be overridden via @SendTo
22 // I will be using @SendTo. In my case, it is not required (because it is the same as the destination selected
23 // based on the convention) but I will be using it just for fun.
24 return "[" + LocalDateTime.now() + "]: " + greeting;