Spring websocket消息传递模板不会在没有@Scheduler注释的情况下从Controller发布数据

时间:2014-07-10 13:10:45

标签: spring-websocket

@Controller
public class WebServiceController {
private static Logger logger = LoggerFactory.getLogger(WebServiceController.class.getName());

@Autowired
private SimpMessagingTemplate template;

public static Response response;
private final MessageSendingOperations<String> messagingTemplate;

@Autowired
private XmlCommandFactory xmlCommandFactory;

@Autowired
public WebServiceController(final MessageSendingOperations<String> messagingTemplate) {
    this.messagingTemplate = messagingTemplate;

}

@RequestMapping(value = "/handleResponse.htm/{sessionId}")
public void handleRequest(final HttpServletRequest request, final HttpServletResponse httpResponse,
        @PathVariable final String sessionId) throws Exception {
    httpResponse.setStatus(200);
    final StringBuilder buffer = new StringBuilder();
    final BufferedReader reader = request.getReader();
    String line = null;
    while ((line = reader.readLine()) != null) {
        buffer.append(line.trim());
    }
    if (!buffer.toString().isEmpty()) {
        try {
             response = xmlCommandFactory.createCommand(buffer.toString(), sessionId);
            publishData();
        } catch (final Exception ex) {
            logger.warn(" Error while processing response ", ex);
        }
    }
}

@Scheduled(fixedDelay = 2000)
public void publishData() {
    try {
        if (null != response && !response.getData().isEmpty()) {
            this.messagingTemplate.convertAndSend("/update/config/" + response.getSessionId(), response);
        }
    } catch (final Exception e) {
        logger.warn("exception  " + e);
    }
}

上述代码按计划的延迟向UI发布数据。但是如果我注释掉@Scheduler注释并从handleRequest调用这个方法,它就不会向UI发布数据。可能是什么原因?

0 个答案:

没有答案