0
@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);
    }
}

The above code publishes data to the UI at the scheduled delay. But if I comment out @Scheduler annotation and call this method from handleRequest, it doesn't publish data to UI. What could be the reason?

Vidya
  • 1
  • 1
  • Is that the actual code running in your use case? First, you're modifying a final instance of "response" in handleRequest. Also, this is a method parameter so the class instance (why static?) is never updated. I don't know what you're tring to achieve here but maybe you could take a look at https://github.com/rstoyanchev/spring-websocket-portfolio and see the messagingTemplate.convertAndSendToUser method and @SendTo annotation. – Brian Clozel Jul 10 '14 at 18:40
  • @BrianClozel, sorry I edited the variables of the actual code which was misleading. Can you please check now, its edited? Response is an object which I have created and it has the json data to be retrieved. My question is when I get a request to this API(handleRequest), I push the data to message broker(publishData()). It is not publishing the data to UI(websocket). And if the same code(publishData()) is tried(by adding a scheduler) without being called from handleRequest, it publishing data to UI. – Vidya Jul 11 '14 at 05:39

0 Answers0