Integrate Server Push with ListModel - SimpleListModelSharer

Dennis Chen, Engineer, Potix Corporation
October 12, 2007

Version

Applicable to zk-3.0.0-FL-2007-10-11 and later.

Introduction

In the latest version of ZK, 3.0 RC, we provide a new feature – Server Push, which let you access desktop and all of its children components out of a ZK Event, such as chat room, or a thread which monitors the outside data and updates the multiple desktops.

In this article, I will show you a convenient class called SimpleListModelSharer, which integrates Server Push and ListModel to share single ListModel to multiple desktops. You can just update one single ListModel in a thread, and all the concerned grid and listbox will be updated automatically in different desktops.

Concept

The concept of SimpleListModelSharer is proxy and asynchronized updating. It will create multiple coped list models called Proxy of ListModel which will be assigned to different grid or listbox in different desktop.

In each desktop, there will be an Operation Thread which monitors an operation queue. When the major data-processing thread is processing(1) and modify the global ListModel(2), the SimpleListModelSharer will receive a ListDataEvent(3), and it will put the corresponding operations into the operation queue of each desktop(4). Then, each thread in desktop will aware the operation and start processing. It will activate the desktop(5), execute the operation(6,7) on the proxy, and then deactivate the desktop(8). After the round trip, the OperationThread stays and waits for next run.

Live Demo

I demonstrate a Stock Quotes Board as the example, so anyone who browses the demo page will see the same stock quotes information. You can run it with http://localhost:8080/modelsharer/stockquotes.zul in demo war.


Note: the data in the example is randomly generated.

The Code

stockquotes.zul

<zk>
	<zscript>
		import org.zkoss.zulex.demo.stock.*; 
		
		if(!desktop.isServerPushEnabled()){
			desktop.enableServerPush(true);
		}
		StockUpdateService service = StockUpdateService.lookup();
		ListModel model = service.getProxy(desktop);
		StockRowRenderer renderer = new StockRowRenderer();
	</zscript>
	<hbox>
		Stock:
		<textbox id="stock" value="BCC"/>
		<button id="addbtn" label="add" onClick="service.addStock(stock.value)"/>
	</hbox>
	<separator/>
	<groupbox width="300px">
	<caption label="Live Stock Quotes Demo" />
	<grid model="${model}" rowRenderer="${renderer}" >
		<columns>
			<column label="Symbol" width="100px"/>
			<column label="Price" />
			<column label="Change" />
		</columns>
	</grid>
	</groupbox>
</zk>
  • Enable server push by desktop.enableServerPush(true);.
  • Get a Proxy of ListModel by call getProxy(desktop).
  • Call addStock(value) when onClick
  • Assign model to grid.

StockUpdateService.java

public class StockUpdateService {
	...
	private StockUpdateService(){
		stockModel = new ListModelList();
		modelSharer = new SimpleListModelSharer(stockModel);
		...
		new Thread(new UpdateRunnable()).start();
	}
	
	public ListModel getProxy(Desktop desktop){
		return modelSharer.getProxy(desktop);
	}
	
	...
	class UpdateRunnable implements Runnable{
		...
		public void run() {
			while(running){
				...
				StockInfo si = getCurrentStockQuotes(index);//get current stock quotes			
				stockModel.set(index,si);//and update it
				...
			}
		}
	}
}
  • New a SimpleModelSharer with stockModel.
  • Start a thread which will update stockModel periodically.
  • Modify stockModel without any tedious code, modelSharer will update all proxies automatically with Server Push.

Download

Summary

The SimpleListModelSharer integrates ServerPush and ListModel to share data to different desktops. By simply updating a single model, all its views on each desktop is updated automatically. That is the power of the server-centric Ajax framework, ZK. We hope you enjoy this feature and please feel free to give us any comments.

 

Comments
 
Jebberwocky
2007-10-23

Very Interesting

kiran shakya
2008-02-27

This SimpleListModelSharer idea is great.
But can i filter this shared model so that a user in his desktop can show only the items of his interest from the proxy. Like in this example, what if the user is interested in latest share value of BCC and JPM.

Gary
2008-03-02

Great stuff. This has lots of potential. It does look like updates are a bit on the slow side, although I am sure this can be improved in later versions.

By the way, ZK is probably one of the coolest new technologies around in my opinion, because you can pick it up and run with it quite quickly.

lee
2008-03-12

cool

Rotem
2008-04-01

Hi!

How does the TreeModelSharer deal go?

Rotem
2008-04-08

I really enjoy working with the ListModelSharer, and am wondering, will there be a TreeModelSharer? is it planned? maybe some guideline on how to implement one?

Thanks in advance,

Rotem.

minh
2008-07-22

how to run http://localhost:8080/modelsharer/stockquotes.zul in modelsharer.war

 
 
Leave a Reply
 
Name (required)
Mail (will not be published) (required)
Website
(Case Insensitive)
Bold textItalic textUnderLine textSource CodeHorizontal rulerExternal Link
Post
Preview