Search code examples
javaicefaces

Binding List to Datatable After Page Loads (Not Lazy Loading)


Our page has a datatable which shows records from a web service. The web service queries a database, and the query can take from 10 secs to 100 secs.

I was initially loading the arraylist binded to the datatable in the backing bean constructor:

private ArrayList myList;
public MyBean
{
     myList = WebServices.getList();
}

In this scenario, the entire page starts to render only after the web service returns all data.

Where should I call the webservice (i.e., do myList = WebServices.getList(); ) in order to have the rest of the page load in parallel, and show a progress bar or something in the datatable while the webservice runs?

I guess my concepts about the JSF/IceFaces lifecycle are not clear...

Also, this is not about lazy loading, because for that we would have to implement pagination in our datatabase query, too.

Thanks!


Solution

  • I found a work around using Threads.

    public class MyBean
    {
        private ArrayList myList;
        private PersistentFacesState state;
    
        public MyBean
        {
            state = PersistentFacesState.getInstance();
            myList = new ArrayList();
            new Thread(new RunService()).start();
        }
    
        public class RunService implements Runnable
        {
            public void run()
            {
                try
                {
                    list = WebServices.getList();
                    state.executeAndRender(); //Need to do this to update view
    
                }
                catch(Exception e)
                {
    
                }
            }
    
        }
    }
    

    Initally I was not aware of the PersistentFacesState and executeAndRender(); , so even though the thread would update the array list, the view wasn't being refreshed. This was because the JSF/IceFaces lifecycle had already ended.

    Persisting the view and then re-rendering it solved this problem, too.

    Hope this solution helps someone.