Spring Websockets创建bean messageBrokerSockJsScheduler时出错

时间:2014-11-10 14:57:25

标签: java spring websocket

我正在尝试编写一个非常基本的websocket示例,但遇到了一个我无法解决的问题。我已经包含了在初始化和我的代码中抛出的异常。任何帮助将不胜感激。感谢。

  • spring 4.1.1
  • jackson 2.1.0
  • servlet-api 6.0.36
  • JDK 1.6

异常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageBrokerSockJsScheduler': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'removeOnCancelPolicy' of bean class

控制器:

package com.example.pocProject.controller;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WebSocketController {

    @MessageMapping("/add")
    @SendTo("/topic/showResult")
    public int addNum(int input1, int input2) throws Exception {
        Thread.sleep(2000);
        int result = input1 + input2;
        return result;
    }

    @RequestMapping("/start")
    public String start() {
        return "addPage";
    }
}

app-Context-servlet.xml中的Websocket配置

<websocket:message-broker application-destination-prefix="/calcApp">
   <websocket:stomp-endpoint path="/add">
      <websocket:sockjs/>
   </websocket:stomp-endpoint>
   <websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>

JavaScript的:

<script type="text/javascript" src="<c:url value="/static/script/sockjs-0.3.4.js"/>"></script>
<script type="text/javascript" src="<c:url value="/static/script/stomp.js"/>"></script>
<script type="text/javascript">

    var stompClient = null;

    function websocketCall() {
        var num1 = 5;
        var num2 = 7;
        connect();
        stompClient.send("/pocProject/add", {}, '');
        disconnect();
    }

    function connect() {
        var socket = new SockJS('/pocProject/add');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            console.log('Connected: ' + frame);
            stompClient.subscribe('/topic/showResult', function(calResult) {
                showResult(JSON.parse(calResult.body).result);
            });
        });
    }

    function disconnect() {
        stompClient.disconnect();
        console.log("Disconnected");
    }

    function showResult(message) {
        var response = document.getElementById('calResponse');
        var p = document.createElement('p');
        p.style.wordWrap = 'break-word';
        p.appendChild(document.createTextNode(message));
        response.appendChild(p);
    }

</script>

0 个答案:

没有答案