java - Schedule task in a portlet -


i have portlet in connect database , display data. when auto-refresh enabled, load data db , refresh page (jsp) every x seconds.

however, altough udnerstand how timers , tasks work, dont quite how make them work in portlet.

i tried 2 different approaches none of them works expected. first 1 using thread.sleep(xxx); function

public void displaylog(final actionrequest actionrequest,             final actionresponse actionresponse) throws ioexception,             portletexception {          {             manager.setlastid(0);             redirecttolog(actionrequest, actionresponse);              try {                 thread.sleep(3000);             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }         } while(manager.isautorefresh().equals("y"));     } 

here redirect function:

public void redirecttolog(actionrequest actionrequest,             actionresponse actionresponse) throws ioexception, portletexception {          // load messages , display them         manager.loadfromdb();          // in case of error, redirect error page         if (manager.isconnerror()) {             actionresponse.setrenderparameter("jsppage",                     "/html/visual/error.jsp");             return;         }          // redirect         actionrequest.setattribute("messages", manager.getmessages());         actionrequest.setattribute("refresh", manager.isautorefresh());         actionrequest.setattribute("servicefilter", manager.getservicefilter());         actionresponse                 .setrenderparameter("jsppage", "/html/visual/display.jsp");      } 

the code gets executed every 3 seconds (if put print statements there, can see taht calls function). however, dont redirected jsp page. instead, "freezes" , while loop forever.

i tried second approach using timers:

class logdbtask extends timertask {     private dbmanager manager;     private portletvisual portlet;     private actionrequest actionrequest;     private actionresponse actionresponse;      public logdbtask(portletvisual portlet, actionrequest actionrequest, actionresponse actionresponse) {         this.portlet = portlet;         this.actionrequest = actionrequest;         this.actionresponse = actionresponse;         manager = portlet.getmanager();      }       public void run() {         if (manager.isautorefresh().equals("y")) {             try {                 manager.setlastid(0);                 portlet.redirecttolog(actionrequest, actionresponse);             } catch (ioexception e) {                 e.printstacktrace();             } catch (portletexception e) {                 e.printstacktrace();             }         } else {              //stop timer.             this.cancel();         }     } } 

which call using

timer timer = new timer("logdbtimer"); logdbtask task = new logdbtask(this, actionrequest, actionresponse); timer.schedule(task, 0, 3000); 

but problem stays same - never jsp page, altough function being called repeatedly. not expert on portelts, nor multi-threaded applications. doing wrong? there easy way fix this?

sorry exhausting question - tried put code examples in there. try specify quaestion more if not understood...

the thread.sleep approach keeps "freezing" because portlet never gets render view. sits in loop , never returns page display user.

the timer task approach flawed in design. user's browser has make page request portlet code send user. means timertask might execute time response has been sent user , can't update page more.

i suggest moving timer logic client side. can write javascript poll portlet using ajax , resourceurl using timer there. serveresource method return refreshed data , update view in javascript. helps prevent full page refresh reduce server load , make page refresh more efficiently.

the other option have use page refresh html metatag or javascript timer have browser full page refresh whenever want page update. reduces complexity of avoiding need ajax think less performant , less elegant solution.


Comments