الأربعاء، 16 أكتوبر 2013

Auto Refresh a Component in ADF

Auto refreshing a component/page in ADF

Scenario: Refreshing a page automatically after a specified interval.

Solution: 
To acheive the auto refresh feature on a page in ADF we have to use the af:poll. 

Step 1: Use in the jspx.

                       id="pollTimer"
                       pollListener="#{backingBeanScope.backing_stockinfo.onPollTimerExpired}"
                       interval="2000"/>




  • Specify the interval in milli seconds.
  • pollListener is the listener method which will be called every time after the specified interval.
Step 2:In a bean write the poll Listner method which will have the logic to refresh the page.


public void onPollTimerExpired(PollEvent pollEvent) {
         //Custom logic to update the data/data control.
        //Refresh the table/component.
        AdfFacesContext.getCurrentInstance().addPartialTarget(getT1());
  }

  • If we have requirement to refresh different components at different time intervals (like 2mins for a table, 5mins for a graph) then we can implement it by creating variable in view scope which will hold the last updated time and we can check it against the current time, and if the difference exceeds the specified time then we can refresh the component.
  • Below code snippet shows refreshing two tables at two different times (the Alert table will be refreshed after every 2mins and the symbol table will be refreshed after every 5mins.

 public void onPollTimerExpired(PollEvent pollEvent) {
        //Refresh for Alert table.
        Long lastRequery = (Long)AdfFacesContext.getCurrentInstance().getViewScope().get("lastRequery");
      //Updating data control
        ApplicationModule am = ADFUtils.getApplicationModuleForDataControl("AppModuleDataControl");
        ViewObject alertVO  = am.findViewObject("AlertRO1");
        alertVO.executeQuery();
        AdfFacesContext.getCurrentInstance().addPartialTarget(getT2());

        //Refresh for symbol table.
        if(lastRequery ==null || lastRequery <= System.currentTimeMillis()){
           
            ViewObject symbolVO = am.findViewObject("StockDataViewRVO1");
            symbolVO.executeQuery();
            AdfFacesContext.getCurrentInstance().addPartialTarget(getT1());
            Calendar c=Calendar.getInstance();
            c.add(Calendar.MINUTE, 5);
            AdfFacesContext.getCurrentInstance().getViewScope().put("lastRequery",c.getTimeInMillis());
        }
    }
source :