According to @Luiggi Mendoza idea, i am stuck a bit ...
I added to my jsf the following:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
template="/templates/layoutUser.xhtml">
<ui:define name="content">
<p:growl widgetVar="growl" showDetail="true" />
<h:form>
Fanciest Page ever
</h:form>
<p:socket onMessage="handleMessage" channel="/notifications" />
<script type="text/javascript">
function handleMessage(facesmessage) {
facesmessage.severity = 'info';
growl.show([ facesmessage ]);
}
</script>
</ui:define>
</ui:composition>
I am calling my TimerTask in a bean which is sessionscoped:
@PostConstruct
public void initLazyModel() {
getTimerMB().startTimer(this);
System.out.println("Timer started");
}
My Timer looks like this:
@ManagedBean(name = "timerMB")
@SessionScoped
public class TimerManagedBean extends TimerTask {
private int counter;
private Timer timer;
private ArtikelManagedBean artikelMB;
public void startTimer(ArtikelManagedBean artikelMB){
this.artikelMB = artikelMB;
this.timer = new Timer();
Calendar date = Calendar.getInstance();
date.set(2012, 3, 28, 21, 28);
//execute every 10 seconds
timer.schedule(this, date.getTime(), 10000);
}
@PreDestroy
public void cancelTimer(){
this.timer.cancel();
System.out.println("UpdateTimer cancelled!");
}
@Override
public void run() {
counter++;
System.out.println("Upadatetimer started: "+ new Date()+" ("+counter+")");
PushContext pushContext = PushContextFactory.getDefault()
.getPushContext();
pushContext.push("/notifications", new FacesMessage("Simple",
"Test"));
}
}
Hmm ... Nothing happens, but anything should happen, shouldn't it ?
It should give me an notification with "simple" and "test", i guess...
UPDATE:
Thank you Luiggi Mendoza! Managed it in a very simple way to show my dialogs (call it serverside). I added the following Servlet to my web.xml.
<servlet>
<servlet-name>Push Servlet</servlet-name>
<servlet-class>org.primefaces.push.PushServlet</servlet-class>
<init-param>
<param-name>org.atmosphere.useBlocking</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.sessionSupport</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Push Servlet</servlet-name>
<url-pattern>/primepush/*</url-pattern>
</servlet-mapping>