I have an application that shows some data in p:DataTable…. This table is accessible by many users. When one user modify a record in the table, (create, edit, delete), this action is notified to an @ApplicationScoped ManagedBean that notify all other session (opened by other users), that the items in the table are changed, then it must be reload from database.
As you can see this is the controller that notify to all session that the items are changed,
/**
* @author Simone Rondelli
*/
@ManagedBean(name="singleton")
@ApplicationScoped
public class SingletonBean {
private int count;
private HashMap<Class, List<AbstractController>> sessions;
public SingletonBean() {
sessions = new HashMap<Class, List<AbstractController>>();
}
public void addSession(AbstractController session, Class c) {
List<AbstractController> sessionList = sessions.get(c);
if(sessionList == null)
sessionList = new ArrayList<AbstractController>();
sessionList.add(session);
sessions.put(c, sessionList);
}
public void notifyItemsChanged(Class type) {
for(AbstractController a : sessions.get(type)) {
a.prepareList();
a.addWarningMessage("Attenzione i record sono stati modificati!!");
}
}
}
this is the code in AbstractController that “try” to show message
public void addWarningMessage(String msg) {
//JsfUtil.addWarningMessage(msg);
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_WARN, msg, msg);
FacesContext.getCurrentInstance().addMessage(null, facesMsg);
}
if you press f5, in any other page, the new records are showed… But i want that messages are sent to all other sessions… Now with my code the messages are showed in the page where the modify is done, many time as the number of opened sessions… So if i have 3 sessions with 3 users and one of these makes some change in the table, he will see 3 messages in his page, meanwhile the other users don’t see anything.
- How can i send Messages to all sessions??
I think that you should check Primefaces Push , take a look at the examples over there…
PrimePush
Or you can try the Ajax Poll AJAX Poll
Here a ref’ for Push in Glassfish question