1

I'm using Richfaces to develop some web pages, with a datatable I try to display some data information from remote server. But its quite slow to load all data in one time, so I use a cache to store data, Firstly my cache is empty and data table is empty.

The ideal goal is loading one row from server(let say 1mins for each row) and store into my cache, then append into data table's end, my question is how can I render the content of data table from managedbean once I retrieve some new data into cache?

I also use a timer to update the cache values from server during a fixed period (1hour), that means later, new data could be added into cache, and old data could be removed from cache, that all depend on server's latest data. same question when I get a fresh cache and need rerender data table content according to cache values.

Thanks,

user1684651
  • 390
  • 1
  • 8
  • 21

1 Answers1

2

The most easy way to do this is rerendering your table. There are two approaches to do this using RichFaces library:

Client Side

The a4j:poll component defines a way to periodically poll a server in order to trigger state changes, or update parts of your page. It uses a timer to trigger each N milliseconds.

You can use it to check your cache data on your server and then rerender your table.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">
    <h:form>
        <a4j:poll id="poll" interval="2000" enabled="#{pollBean.pollEnabled}" render="poll,grid" />
    </h:form>

    <h:form>
        <h:panelGrid columns="2" width="80%" id="grid">
            <h:panelGrid columns="1">     
                <h:outputText value="Polling Inactive" rendered="#{not pollBean.pollEnabled}"></h:outputText>     
                <h:outputText value="Polling Active" rendered="#{pollBean.pollEnabled}"></h:outputText>     
                <a4j:commandButton style="width:120px" id="control" value="#{pollBean.pollEnabled?'Stop':'Start'} Polling"
                    render="poll, grid">
                    <a4j:param name="polling" value="#{!pollBean.pollEnabled}" assignTo="#{pollBean.pollEnabled}" />
                </a4j:commandButton>     
            </h:panelGrid>     
            <h:outputText id="serverDate" style="font-size:16px" value="Server Date: #{pollBean.date}" />
        </h:panelGrid>
    </h:form>
</ui:composition>

More information on RichFaces a4j:poll.

Server Side

The a4j:push works as a consumer/producer architecture, which uses no timer, instead it uses a message that will instruct the client to re-render part of the page.

Using this component you will be able to have an impact on the client side (rerender HTML) from the java methods in your ManagedBean. Maybe the problem here will be to communicate your current cache architecture with your JSF Managed Bean.

More information on RichFaces a4j:push.

Regards,

Rodmar Conde
  • 956
  • 1
  • 12
  • 24
  • thanks, I saw the poll function on RichFaces Showcase, but I really want the data-table content updates simultaneously. As the following rechecking period is 1 hour+, if I use a small interval to check cache values, most of time it will get nothing add/remove, because cache is not updated, If I use a large interval, then user will not see "real-time" cache values. I'm not sure if there something in JSF that can re-render content from a managed-bean. – user1684651 May 14 '13 at 09:08
  • In that case you need the opposite component: **a4j:push** in which _the message instructs the client to re-render part of the page_. [Checkit](http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=push&sample=pushTopicsContext&skin=blueSky) – Rodmar Conde May 14 '13 at 09:18
  • is that the poll/push are message related? It seems that it still uses a fixed interval to work like a timer task for me. – user1684651 May 14 '13 at 11:16
  • **a4j:poll** doesn't needs any interval. Its a consumer/producer architecture. Whenever you want (for example, on your _cache's_ change event), you can call a java method that have an impact on the client side (rerender HTML). Maybe the problem for you is to communicate your current _cache_ architecture with your JSF Managed Bean. – Rodmar Conde May 14 '13 at 11:28
  • thanks for the clarification, I'll try to implement with that. – user1684651 May 15 '13 at 02:20