java - unsubscribe previous requests before subscribing -
in application, have field "last accessed time" in header gets updated every 5 seconds. have included header.jsp in other jsps. [link1.jsp , link2.jsp.refer screen shot.] now, on load fire request server , request gets suspended. // refer attached .js file , .java file works fine. response server every 5 seconds. now, when navigate other jsps clicking link, page gets reloaded i.e., header.jsp gets reloaded. now, response server instead of every 5 seconds, response requests initiated during initial page load. so, appears although field gets updated every 2 seconds instead of actual interval of 5 seconds. when keep navigating , forth clicking links, clock ticks continuously coz, there requests got fired during various intervals.
is there way avoid this?
i intend cancel previous requests when load header.jsp again. tried unsubscribe() in conjunction long-polling. didn't work.
also, tried invoking "/stop" url per link , on it's callback thought firing request can start requests afresh every time. didn't work. calling "/stop" stopped requests getting fired. callback never invoked.
i not sure if missing here.
please me out.
thanks.
js code
$("document").ready(function(){ firerequest(); }); function firerequest() { var socket = $.atmosphere; var subsocket; var websocketurl = "atmos/time"; var request = { url: websocketurl, contenttype : "application/json", loglevel : 'debug', datatype: 'json', shared:true, transport : 'websocket' , trackmessagelength : true, enableprotocol : true, reconnectinterval : 0, maxreconnectonclose : 3, dropatmosphereheaders : false, timeout : 10 * 60 * 1000, fallbacktransport: 'long-polling', connecttimeout: -1 }; request.onmessage = function (response) { try { var data = response.responsebody; $("#time").text(data); } catch(e) { console.log(e); } }; request.onopen = function(response) { console.log('onopen '+ response); }; request.onreconnect = function (request, response) { console.log('onreconnect ' + request); console.log('onreconnect ' + response); }; request.onclose = function(response) { if (response.state == "unsubscribe") { alert('window switch'); } console.log('onclose ' + response); }, request.onerror = function(response) { console.log('onerror ' + response); }; subsocket = socket.subscribe(request); }
atmosphereresource.java
@path("/{tagid}") @produces("text/html;charset=iso-8859-1") @singleton public class atmosresource { private static final logger logger = loggerfactory.getlogger(atmosresource.class); private final asynchttpclient asyncclient = new asynchttpclient(); private final concurrenthashmap<string, future<?>> futures = new concurrenthashmap<string, future<?>>(); private final countdownlatch suspendlatch = new countdownlatch(1); private int count = 1; @get public suspendresponse<string> search(final @pathparam("tagid") broadcaster feed, final @pathparam("tagid") string tagid, final @context atmosphereresource resource) { if (feed.getatmosphereresources().size() == 0) { final future<?> future = feed.schedulefixedbroadcast(new callable<string>() { public string call() throws exception { suspendlatch.await(); asyncclient.prepareget("http://localhost:7070/sample/rest/currenttime").execute( new asynccompletionhandler<object>() { @override public object oncompleted(response response) throws exception { string s = response.getresponsebody(); if (response.getstatuscode() != 200) { feed.resumeall(); feed.destroy(); return null; } feed.broadcast(s).get(); system.out.println("current count::: " + count); count ++; system.out.println("data:: " + new date().tostring()); return null; } }); return null; } }, 5, timeunit.seconds); futures.put(tagid, future); } return new suspendresponse.suspendresponsebuilder<string>().broadcaster(feed).outputcomments(true) .addlistener(new eventslogger() { @override public void onsuspend( final atmosphereresourceevent event) { super.onsuspend(event); feed.addatmosphereresource(resource); suspendlatch.countdown(); } // overriding method check when user //switches tabs/closes browser. //ref: https://github.com/atmosphere/atmosphere/wiki/detecting-browser-close%27s-situation-when-using-long-polling @override public void ondisconnect(final atmosphereresourceevent event) { string transport = event.getresource().getrequest().getheader(headerconfig.x_atmosphere_transport); if (transport != null && transport.equalsignorecase(headerconfig.disconnect)) { system.out.println("disconnect"); } else { system.out.println("long-polling connection resumed."); } } }).build(); } @get @path("stop") public string stopsearch(final @pathparam("tagid") broadcaster feed, final @pathparam("tagid") string tagid) { feed.resumeall(); if (futures.get(tagid) != null) { futures.get(tagid).cancel(true); } logger.info("stopping real time update {}", tagid); return "done"; } }
screen shot
i got working overriding atmosphereresourceeventlistener's ondisconnect method , attaching suspended response.
@override public void ondisconnect(final atmosphereresourceevent event) { if (event.iscancelled() || event.isclosedbyclient()) { feed.resumeall(); if (futures.get(tagid) != null) { futures.get(tagid).cancel(true); } } }
thanks
Comments
Post a Comment