0

I want to know if there is any proper way to add timer for task schedule (it will be count up) in Spring 3 + Tiles that works accurate. I have tried many options like jquery timer + (Client side) Quartz (Server side Threading), But though it is not accurate and somewhat we can say it is bad practice for web application. What I exactly want (want to manage) is in my Web application(Spring 3 + Tiles), When user Click on Timer start, It should be started at client side and timer should be continued until user click on stop, however user could do any other things (Like navigation to any other pages) in web application but timer should be working in static way. There are many issues as if only I implement timer at client side (using cookies ,jquery session client side) than I have to manage if user navigate to another page then again timer will have to start from previous time that has been stored in cookies but doing this results in loss of seconds during request response processes.So I tried also to implement server side timer using quartz but again I have to sync it with client side timer at every click in web application . So again it is bad practice what I feel. So Is there any thing that I can introduce in Spring 3 + tiles that can be static and can hold timer in static way.

Thanx in Advance.

Milople Inc
  • 399
  • 1
  • 8
  • 34

1 Answers1

2

Ok so you need Server Push in simple words.You can use Atmosphere for acheving this. For integrating atmosphere with Spring MVC you can check this sample spring-web-mvc-atmosphere.after integration you just need to do this on your server side.

@RequestMapping(value = "/websockets", method = RequestMethod.GET)
    @ResponseBody
    public void websockets(final AtmosphereResource event) {

        AtmosphereUtils.suspend(event);
        final Broadcaster bc = event.getBroadcaster();
        bc.scheduleFixedBroadcast(new Callable<String>() {

            public String call() throws Exception {

                return (new Date()).toString();
            }
        }, 1, TimeUnit.SECONDS);


    }

And from client side:

function startTimer() {
var callbackAdded = false;

function callback(response)
{

    $.atmosphere.log('info', ["response.state: " + response.state]);
    $.atmosphere.log('info', ["response.transport: " + response.transport]);

    if (response.transport != 'polling' && response.state != 'connected' &&                  response.state != 'closed') {
        $.atmosphere.log('info', ["response.responseBody: " + response.responseBody]);
        if (response.status == 200) {
            var data = response.responseBody;

            if (data) {
                $("#date").text(data);
            }
        }
    }
}
$.atmosphere.subscribe("${pageContext.request.contextPath}/user/websockets",
    !callbackAdded? callback : null,
$.atmosphere.request = {transport: 'websocket'});
connectedEndpoint = $.atmosphere.response;
callbackAdded = true;
  };

Just suspend the get request and broadcast the current time perodically and you can extend this according to you need I have just given you a raw idea.Hope this helps.

Dangling Piyush
  • 3,658
  • 8
  • 37
  • 52