Home > Java for Web > Push ad intervalli regolari con Icefaces

Push ad intervalli regolari con Icefaces

Possiamo aggiornare tutti i client collegati alla nostra applicazione web ad intervalli di tempo regolari, anche senza che avvenga nessuna richiesta da parte di qualsiasi client. Questo viene fatto istanziando un particolare bean nella pagina che vogliamo tenere aggiornata.

prima di tutto creiamo il bean che si occupa di tenere traccia di tutti i client collegati e che faranno parte di un gruppo renderabile:

package pushTryPack;

import com.icesoft.faces.async.render.IntervalRenderer;
import com.icesoft.faces.async.render.RenderManager;
import com.icesoft.faces.async.render.Renderable;
import com.icesoft.faces.webapp.xmlhttp.FatalRenderingException;
import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
import com.icesoft.faces.webapp.xmlhttp.RenderingException;
import com.icesoft.faces.webapp.xmlhttp.TransientRenderingException;
//import org.icefaces.x.core.push.SessionRenderer;

/**
 *
 * @author Lorenzo
 */
public class timerBean implements Renderable {

    private int pollingTimer = 1000; //tempo di refresh in millisecondi
    private PersistentFacesState state = null;
    private IntervalRenderer timer;
    public static final String GRUPPO_RENDERABILE = "renderGroup"; //gruppo dei client da tenere aggiornati
    private String caricato; //proprietà che serve solo a far caricare al client il bean di request

    /** costruttore timerBean */
    public timerBean() {
        state = PersistentFacesState.getInstance();
    }

    public PersistentFacesState getState() {
        return state;
    }

    public void renderingException(RenderingException renderingException) {
        if (renderingException instanceof TransientRenderingException) {
            System.out.println("timerBean Transient Rendering exception:" + renderingException.toString());
        } else if (renderingException instanceof FatalRenderingException) {
            System.out.println("timerBean Fatal rendering exception: " + renderingException.toString());
            performCleanup();
        }
    }

    /**
     * @return the pollingTimer
     */
    public int getPollingTimer() {
        return pollingTimer;
    }

    /**
     * @param pollingTimer the pollingTimer to set
     */
    public void setPollingTimer(int pollingTimer) {
        this.pollingTimer = pollingTimer;
    }

    //questo metodo sarà chiamato in fase di inizializzazione direttamente dal file di configurazione
    public void setRenderManager(RenderManager manager) {
        if (manager != null) {
            timer = manager.getIntervalRenderer(GRUPPO_RENDERABILE);
            if (timer.getInterval() != pollingTimer) {
                timer.setInterval(pollingTimer);
            }
        }
        timer.add(this);
        timer.requestRender();
    }

    //questo metodo serve solo per dare consistenza alla proprietà inizializzata nella configurazione
    public RenderManager getRenderManager() {
        return null;
    }

    protected boolean performCleanup() {
        try {
            if (timer != null && timer.contains(this)) {
                timer.remove(this);
            }
            return true;
        } catch (Exception failedCleanup) {

            System.out.println("Failed to cleanup a clock bean" + failedCleanup.toString());
        }
        return false;
    }

    public void dispose() throws Exception {
        System.out.println("timerBean Dispose called - cleaning up");
        performCleanup();
    }

    /**
     * @return the caricato
     */
    public String getCaricato() {
        if(caricato==null){
            caricato = "si";
        }
        return caricato;
    }

    /**
     * @param caricato the caricato to set
     */
    public void setCaricato(String caricato) {
        this.caricato = caricato;
    }
}

ora dobbiamo rendere il bean un request bean, quindi andiamo a configurare il file faces-config.xml in questo modo:

<managed-bean>
        <description>Gestore delle chiamate di rendering.</description>
        <managed-bean-name>rmanager</managed-bean-name>
        <managed-bean-class>com.icesoft.faces.async.render.RenderManager</managed-bean-class>
        <managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<managed-bean>
        <description>Il bean che si occupa dell'intervallo di rendering di tutti i client</description>
        <managed-bean-name>timerBean</managed-bean-name>
        <managed-bean-class>pushTryPack.timerBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>renderManager</property-name>
            <value>#{rmanager}</value>
        </managed-property>
</managed-bean>

Ci rimane solo di andare a dire alla pagina web che deve essere aggiornata, facendole usare nella sua request il bean timerBean; per far ciò inseriamo nel file vero e proprio della pagina web i seguenti tag:

<ice:form>
        <ice:inputHidden value="#{timerBean.caricato}"/>
</ice:form>

Ho provato questo codice usando le librerie IceFaces 1.7.2 e come application server  sia Tomcat 6 che glassFish versione V2 e V3

Java for Web , , , , , , , ,

  1. Nessun commento ancora...
  1. Nessun trackback ancora...