Sprint STOMP: simple broker (in memory broker)
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 30 Mar 2016 00:12:06 +0000 (02:12 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 30 Mar 2016 00:12:06 +0000 (02:12 +0200)
SpringJava/STOMP/spring-stomp-server/src/main/java/de/spring/webservices/rest/controller/GreetingController.java
SpringJava/STOMP/spring-stomp-server/src/main/resources/spring-configuration/spring-configuration.xml

index 930aaf8..58c6da4 100644 (file)
@@ -2,16 +2,32 @@ package de.spring.webservices.rest.controller;
 
 import java.time.LocalDateTime;
 
-import org.springframework.messaging.handler.annotation.MessageMapping;
+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 {
 
-    @MessageMapping("/greeting")
-    public String handle(String greeting) {
-        return "[" + LocalDateTime.now() + ": " + greeting;
+       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);
+    }
+
 }
index 892d490..5917c07 100644 (file)
        in this way annotations like @Service, @Endpoint, etc, etc)
     -->
     <context:component-scan base-package="de.spring.stomp"/>
-    
-    <!-- SockJS -->
-    <websocket:handlers allowed-origins="*">
-       <websocket:mapping path="/myHandler" handler="sockJsHandler"/>
-       <!-- Using SockJS protocol -->
-       <websocket:sockjs/>
-       <websocket:handshake-interceptors>
-            <bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
-        </websocket:handshake-interceptors>
-    </websocket:handlers>
 
-    <bean id="sockJsHandler" class="org.springframework.web.socket.sockjs.transport.handler.SockJsWebSocketHandler"/>
-    
+
     <bean class="org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean">
         <property name="maxTextMessageBufferSize" value="8192"/>
         <property name="maxBinaryMessageBufferSize" value="8192"/>
     </bean>
     
-    
     <!-- STOMP -->
     <websocket:message-broker application-destination-prefix="/app">
         <websocket:stomp-endpoint path="/portfolio">
             <websocket:sockjs/>
         </websocket:stomp-endpoint>
-        <!-- Full-featured broker, see: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-handle-broker-relay -->
+        <!--
+               Full-featured broker, see: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-handle-broker-relay
         <websocket:stomp-broker-relay prefix="/topic,/queue" />
+        -->
+        <websocket:simple-broker prefix="/topic, /queue"/>
     </websocket:message-broker>
 
 </beans>