0

Experience with Server Push?

asked 2012-11-22 17:32:39 +0800

avidD gravatar image avidD
166 2

Hi,
I am trying to use comet server push. But apparently I am doing something wrong. Has anyone here experience with this feature?
This is what I do:

public class PushContent {
  
  private final Desktop desktop;

  public PushContent(Desktop aDesktop) {
    desktop = aDesktop;
  }
  
  public void notifyUsers() throws DesktopUnavailableException, InterruptedException {
    // note: push has been activated for the desktop at session startup (assuming there is only one desktop per session) 
    // nevertheless I get an illegal state exception and activate it again
    desktop.enableServerPush(true);
    Executions.activate(desktop);
    for ( int i = 0; i < 5; i++ ) {      
      try {
        Clients.showBusy("Executing " + (i + 1) + " of 5");
      } finally {
        Executions.deactivate(desktop);
      }
      Thread.sleep(2000);
    }
    desktop.enableServerPush(false);
  }
}

In some afterCompose method I execute this to test whether it is working:

    final Desktop desktop = Executions.getCurrent().getDesktop();
    Runnable runnable = new Runnable() {
      @Override public void run() {
        try {
          new PushContent(desktop).notifyUsers();
        } catch ( DesktopUnavailableException e ) {
          e.printStackTrace();
        } catch ( InterruptedException e ) {
          e.printStackTrace();
        }
      }
    };
    Registry.threadPool().execute(runnable); // the thread pool is a standard executor service

So here is the actual initialization which seems to have no effect. Maybe its called too early?

public class SessionInitListener implements SessionInit {
  @Override
  public void init(Session session, Object request) throws Exception {
    // Enable AJAX push. This is required to notify users of external events.
    Executions.getCurrent().getDesktop().enableServerPush(true);

I get this exception:

Exception in thread "pool-1-thread-2" java.lang.IllegalStateException: Server Push cannot be started without execution
	at org.zkoss.zk.ui.impl.DesktopImpl.enableServerPush0(DesktopImpl.java:1348)
	at org.zkoss.zk.ui.impl.DesktopImpl.enableServerPush(DesktopImpl.java:1336)
	at com.tn_ag.tnp.tailor.gui.push.PushContent.notifyUsers(PushContent.java:19)

I see that server push apparently assumes that a connection is open, which is quite understandable. However, at what time should I get the connection and how do I keep it open?

I've read these few words but they don't actually explain what's to do
http://books.zkoss.org/wiki/Small_Talks/2012/February/New_Features_of_ZK_6#ZK_Comet_supports_Servlet_3_Asynchronous_Processing

Ultimately, I want to display a dialog to certain users if a certain event occurs externally.
Any help is highly appreciated.

Cheers
David

delete flag offensive retag edit
Be the first one to reply this discussion!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-11-22 17:32:39 +0800

Seen: 62 times

Last updated: Nov 22 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More