Revision history [back]

click to hide/show revision 1
initial version

answered 2011-11-03 18:20:41 +0800

gekkio gravatar image gekkio flag of Finland

http://gekkio.fi/blog

Here's some code for the ideas I presented earlier. This is all from the top of my head so it might not even compile, but these should show the basic building blocks you will need to implement your application.

AnalyticsThreadPool.java: This class manages the thread pool. In my example the pool size is 20, so that means that no more than 20 active requests are supported. Requests that cannot be started right away are placed into a queue so they will be eventually processed. Don't use a too large thread pool (more than 100 is definitely overkill). You need to register this class as a listener in zk.xml!

public class AnalyticsThreadPool implements WebAppInit, WebAppCleanup {
  private static ExecutorService EXECUTOR_SERVICE;

  public void init(WebApp webApp) {
    if (EXECUTOR_SERVICE == null) {
      EXECUTOR_SERVICE = Executors.newFixedThreadPool(20);
    }
  }
  public void cleanup(WebApp webApp) {
    EXECUTOR_SERVICE.shutdownNow();
    EXECUTOR_SERVICE = null;
  }
  public static void submit(Runnable task) {
    EXECUTOR_SERVICE.submit(task);
  }
}

AnalyticsTask.java: This class represents a single query and stores the desktop so that the query results can be asynchronously sent back to ZK.

public class AnalyticsTask implements Runnable {
  private final Desktop desktop;
  private final EventListener eventListener;
  private final AnalyticsParams queryParams;

  public AnalyticsTask(Desktop desktop, EventListener eventListener, AnalyticsParams queryParams) {
    this.desktop = desktop;
    this.eventListener = eventListener;
    this.queryParams = queryParams;
  }

  public void run() {
    AnalyticsResult result = ... // run the query
    if (desktop.isAlive()) {
      Executions.schedule(desktop, eventListener, new AnalyticsEvent(result));
    }
  }
}

AnalyticsEvent.java: This class is a custom event that contains results from a single query.

public class AnalyticsEvent extends Event {
  public static final String NAME = "onAnalyticsResult";

  public final AnalyticsResult result;

  public AnalyticsEvent(AnalyticsResult result) {
    super(NAME, null, null);

    this.result = result;
  }
}

WindowComposer.java: This class shows the basic stuff you need in your window composer.

public class WindowComposer extends ... {

  public void doAfterCompose ... {
    ...
    if (!desktop.isServerPushEnabled()) {
      desktop.enableServerPush(true);
    }
  }

  public void onClick$submit() { // Just an example. You can of course submit AnalyticsTasks from any method, not just onClick
    AnalyticsParams params = ... // Read all parameters from somewhere
    AnalyticsTask task = new AnalyticsTask(desktop, this, params);
    AnalyticsThreadPool.submit(task);
  }

  // This will be called when the results are ready
  public void onAnalyticsResult(AnalyticsEvent event) {
    AnalyticsResult result = event.result;
    // Do stuff with result
  }

}

AnalyticsParams.java: This is something you have to write yourself. Make a class that contains all the parameters you need to make a query. You must NOT include any ZK components or ZK stuff here.

public class AnalyticsParams {
  // ?
}

AnalyticsResult.java: This is also something you have to write. Make a class that contains all the results of a query.

public class AnalyticsResult {
  // ?
}
Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More