From d2367f7a7ee0de675f858c16f2260b7505da1bb9 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 26 Apr 2016 03:54:36 +0200 Subject: [PATCH] STOMP: sending data by means of: a) REST service (@RestController) b) STOMP SEND command (@MessageMapping) --- .../controllers/MessageGreetingController.java | 22 ++++++++++++++ .../rest/controller/GreetingController.java | 33 --------------------- .../rest/controller/RestGreetingController.java | 34 ++++++++++++++++++++++ .../spring-configuration/mvc/rest/rest-config.xml | 13 ++++++--- 4 files changed, 65 insertions(+), 37 deletions(-) create mode 100644 SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/stomp/controllers/MessageGreetingController.java delete mode 100644 SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/GreetingController.java create mode 100644 SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/RestGreetingController.java diff --git a/SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/stomp/controllers/MessageGreetingController.java b/SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/stomp/controllers/MessageGreetingController.java new file mode 100644 index 0000000..d525208 --- /dev/null +++ b/SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/stomp/controllers/MessageGreetingController.java @@ -0,0 +1,22 @@ +package de.spring.stomp.controllers; + +import java.time.LocalDateTime; + +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.stereotype.Controller; + +@Controller +public class MessageGreetingController { + + // Sending data to /app/greeting from STOMP client (client must first connect to endpoint, in my case portfolio) + // connecting to this URL -> http://172.17.0.3/spring-stomp-server/portfolio + // sending data to /app/greeting + + // The data sent to /app/greeting will retrieved by this method. + @MessageMapping("/greeting") + public String handle(String greeting) { + // STOMP clients subscribed to /topic/greeting will receive the returned data from this method. + // Destination is selected based on a convention but can be overridden via @SendTo + return "[" + LocalDateTime.now() + ": " + greeting; + } +} diff --git a/SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/GreetingController.java b/SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/GreetingController.java deleted file mode 100644 index 58c6da4..0000000 --- a/SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/GreetingController.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.spring.webservices.rest.controller; - -import java.time.LocalDateTime; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.messaging.simp.SimpMessagingTemplate; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; - - -@Controller -public class GreetingController { - - private SimpMessagingTemplate template; - - @Autowired - public GreetingController(SimpMessagingTemplate template) { - this.template = template; - } - -// @MessageMapping("/greeting") -// public String handle(String greeting) { -// return "[" + LocalDateTime.now() + ": " + greeting; -// } - - @RequestMapping(path="/greetings", method=RequestMethod.POST) - public void handle(String greeting) { - String text = "[" + LocalDateTime.now() + "]:" + greeting; - this.template.convertAndSend("/topic/greeting", text); - } - -} diff --git a/SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/RestGreetingController.java b/SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/RestGreetingController.java new file mode 100644 index 0000000..5ce063e --- /dev/null +++ b/SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/RestGreetingController.java @@ -0,0 +1,34 @@ +package de.spring.webservices.rest.controller; + +import java.time.LocalDateTime; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.simp.SimpMessagingTemplate; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +public class RestGreetingController { + + private SimpMessagingTemplate template; + + @Autowired + public RestGreetingController(SimpMessagingTemplate template) { + this.template = template; + } + + // Sending data to /topic/greeting from REST service. + // POST http://localhost:8080/spring-stomp-server/greetings + + @RequestMapping(path="/greetings", method=RequestMethod.POST) + public void handle(@RequestBody String greeting) { + String text = "[" + LocalDateTime.now() + "]:" + greeting; + + // STOMP clients subscribed to /topic/greeting will receive the data sent by the convertAndSend method. + this.template.convertAndSend("/topic/greeting", text); + } + +} diff --git a/SpringJava/STOMP/spring-stomp-server/src/main/resources/spring-configuration/mvc/rest/rest-config.xml b/SpringJava/STOMP/spring-stomp-server/src/main/resources/spring-configuration/mvc/rest/rest-config.xml index 54b8540..5ce72ef 100644 --- a/SpringJava/STOMP/spring-stomp-server/src/main/resources/spring-configuration/mvc/rest/rest-config.xml +++ b/SpringJava/STOMP/spring-stomp-server/src/main/resources/spring-configuration/mvc/rest/rest-config.xml @@ -4,10 +4,15 @@ xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd - http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> + xsi:schemaLocation=" + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/mvc + http://www.springframework.org/schema/mvc/spring-mvc.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context.xsd + http://www.springframework.org/schema/util + http://www.springframework.org/schema/util/spring-util.xsd">