STOMP: sending data by means of:
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 26 Apr 2016 01:54:36 +0000 (03:54 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 26 Apr 2016 01:54:36 +0000 (03:54 +0200)
a) REST service (@RestController)
b) STOMP SEND command (@MessageMapping)

SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/stomp/controllers/MessageGreetingController.java [new file with mode: 0644]
SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/GreetingController.java [deleted file]
SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/RestGreetingController.java [new file with mode: 0644]
SpringJava/STOMP/spring-stomp-server/src/main/resources/spring-configuration/mvc/rest/rest-config.xml

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 (file)
index 0000000..d525208
--- /dev/null
@@ -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 (file)
index 58c6da4..0000000
+++ /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 (file)
index 0000000..5ce063e
--- /dev/null
@@ -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);
+    }
+
+}
index 54b8540..5ce72ef 100644 (file)
@@ -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">
    
        <!--
                I am declaring my beans without the automatic annotation. :/