根据@Luiggi Mendoza 的想法,我有点卡住了......
我在我的 jsf 中添加了以下内容:
<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>
我在会话范围的 bean 中调用 TimerTask:
@PostConstruct
public void initLazyModel() {
getTimerMB().startTimer(this);
System.out.println("Timer started");
}
我的计时器看起来像这样:
@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"));
}
}
嗯......什么都没有发生,但应该发生什么,不是吗?它应该给我一个“简单”和“测试”的通知,我猜......
更新:
谢谢路易吉·门多萨!以一种非常简单的方式管理它以显示我的对话框(称为服务器端)。我将以下 Servlet 添加到我的 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>