0

How can I get Parameter when I'm using ServerPush?

asked 2009-04-22 07:54:14 +0800

mathsniper gravatar image mathsniper
30 1

updated 2009-04-22 07:56:43 +0800

Please focus on this code below:

	public void run() {
		if (!desktop.isServerPushEnabled()) {
			desktop.enableServerPush(true);
		}
		try {
			while (!_ceased) {
				try {
					Executions.activate(desktop);
					// I wanna get parameters from URL
					process();// something doing with using URL parameters
					}
				} finally {
					Executions.deactivate(desktop);
				}
				Threads.sleep(2000);// Update each 2 seconds
			}
		} catch (InterruptedException ex) {
			ex.printStackTrace();
		} finally {
			if (desktop.isServerPushEnabled())
				desktop.enableServerPush(false);
		}

	}

sometimes I wanna get parameters from URL dynamically because I have other program that posting URL
to my core program with using Server Push providing by ZK. But I cannot get parameters before it's going
to push the data to server(It means I wanna get URL parameter before "process()" method.)
and this code return null ==> Executions.getCurrent().getParameter("data") before process() method and enable server push.

So how can I get the URL parameters dynamically when the program is running Server Push?
(ZK ver:3.60)

delete flag offensive retag edit

11 Replies

Sort by ยป oldest newest

answered 2009-04-22 16:18:01 +0800

hideokidd gravatar image hideokidd
750 1 2

updated 2009-04-22 16:19:03 +0800

Hi,

I'm not familiar with server push mechanism,
after studying the source code of smalltalk "chatroom",
http://www.zkoss.org/smalltalks/serverpush/
I think the global variable is accessed by the method "process()",
that is,
maybe you can make your URL with this way,
thus every client can access the same parameters.

This is just personal opinion, I haven't made this idea come true....

link publish delete flag offensive edit

answered 2009-04-23 01:30:44 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

to get parameter, make sure you are in the http request or zk event thread (not in your thread)
I think you should save the parameter that you want in some where(such as desktop) when page loading (ex, in onCreate).
And, then get it back in your control.

link publish delete flag offensive edit

answered 2009-04-23 04:19:57 +0800

mathsniper gravatar image mathsniper
30 1

I know, I can get parameter from desktop like "onCreate" but I wanna get the parameter "dynamically". It means the URL may be changed because other program may post to my program. So I need to get parameter when it's going to do server push.
place the parameter in desktop doesn't work here...

how can I do this? thanks.

link publish delete flag offensive edit

answered 2009-04-23 06:20:41 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

I think I have some miss understanding, could you provide some example code to show what you want to do.

link publish delete flag offensive edit

answered 2009-04-23 07:26:06 +0800

mathsniper gravatar image mathsniper
30 1

updated 2009-04-23 07:26:54 +0800

for example, now I have a program A that it will post URL to program B(using server push to get parameters from URL). Program A acts the post URL action when user clicks the button(program A will construct the data and post the URL to program B after button event has started). Then program B should get the parameter either URL have changed or not(changed by program A).

now the problem is, the parameter return null doing in the Thread Class. I need to get parameter when program B enables server push.

link publish delete flag offensive edit

answered 2009-04-23 08:00:33 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2009-04-23 08:01:02 +0800

I don't understand the "program A will post a URL to program B".
what does the 'program' mean in this case? a desktop with a button?
what does 'post a URL to B ' mean? change url of another desktop?

if you have the code, please do some filter(simplify) and post it here.
don't let us guess the code.

link publish delete flag offensive edit

answered 2009-04-23 09:18:09 +0800

mathsniper gravatar image mathsniper
30 1

updated 2009-04-23 09:22:16 +0800

I'm sorry that I have made a confusing of what is "program A and program B".
now I have two ZK web dynamic project(call it prj1 and prj2).
prj1 has a combobox. It posts URL when user selects the combobox.
here is a piece of prj1's index.uzl code

<combobox id="foodNameCombo" readonly="true">
	<zscript>
	String sessionKey = Executions.getCurrent().getParameter("session_key");
</zscript>
	<attribute name="onSelect"><![CDATA[
		org.web3.grapes.pojo.FoodInfo selectedFoodInfo = self.getSelectedItem().getValue(); 

		org.web3.grapes.Message msg = new org.web3.grapes.Message();
		msg.type = "org.web3.imhungry?food_data";
		msg.setData("food_name", selectedFoodInfo.getFoodName());
		msg.setData("restaurant_name", selectedFoodInfo.getRestaurantName());
		msg.setData("address", selectedFoodInfo.getAddress());
		msg.setData("description", selectedFoodInfo.getDescription());
		msg.setData("rating", selectedFoodInfo.getRating());
		msg.setData("flickr_text", selectedFoodInfo.getFlickrText());
		
		String appId = "org.web3.imhungry";
		System.out.println(sessionKey);
		String secret = "passw0rd";
		String serverUrl = "http://xx.xx.xx.xx";
		org.grapes.wine.api.client.rest.GrapesRestClient client = new org.grapes.wine.api.client.rest.GrapesRestClient(appId, sessionKey, secret, serverUrl);
		client.forwardMessage("org.web3.gmap", msg);
	]]></attribute>
	<comboitem forEach="${foodInfo}"
		label="${each.foodName}" value="${each}" />
</combobox>

GrapesRestClient is a API that it will post a URL via my system. I sure the forwardMessage function works correctly. It has been well test.
prj2 has a ZK gmap, it enables ZK server push because the google map should be changed when prj1 posts URL to prj2.
Here is a piece of prj2's index.zul code
<script
	src="http://maps.google.com/maps?file=api&v=2&sensor=false
   			&key=${desktopScope.gmapsKey}"
	unless="${empty desktopScope.gmapsKey}" type="text/javascript">
</script>
<gmaps id="food_map" width="500px" height="320px"
	showSmallCtrl="true" lat="39.155622393423215" lng="116.927490234375"
	zoom="10" enableGoogleBar="true" showTypeCtrl="true" use="org.web3.imhungry.map.FoodMap">
	<gmarker id="centerMarker" lat="39.155622393423215" lng="116.927490234375"
		iconImage="http://www.google.com/mapfiles/markerZ.png">
	</gmarker>
	<attribute name="onMapClick"><![CDATA[
			org.zkoss.gmaps.Gmarker gmarker = event.getGmarker();
			if (gmarker != null) {
				gmarker.setOpen(true);
			}
	]]></attribute>

And here is part of the code of FoodMap class(gmaps control uses it)
public void init() {
	desktop = Executions.getCurrent().getDesktop();
	desktop.setAttribute("gmapsKey", gKey);
	Execution execution = Executions.getCurrent();
	String sessionKey = execution.getParameter("session_key");
	System.out.println("start: "+sessionKey);
	foodMap = this;
}

public void onCreate() {
	if (!foodMap.getChildren().isEmpty()) {
		Gmarker marker = (Gmarker) foodMap.getChildren().get(0);
		marker.setContent("<img src='images/food.png'/>");
		marker.setOpen(true);
	}
	desktop.enableServerPush(true);
	GmapUpdate gmapUpdate = new GmapUpdate(desktop);
	gmapUpdate.start();
}

the last is a piece of the code of GmapUpdate(called from onCreate method)
public void run() {
	if (!desktop.isServerPushEnabled()) {
		desktop.enableServerPush(true);
	}
	try {
		while (!_ceased) {
			try {
				Executions.activate(desktop);
				
				System.out.println(Executions.getCurrent().getParameter("session_key"));
				System.out.println("xml: "+Executions.getCurrent().getParameter("xml"));
				System.out.println("sk: "+desktop.getExecution().getParameter("session_key"));
				// Executions.getCurrent().getParameter always return null!
				if (canRun()) {
					process();
				}
			} finally {
				Executions.deactivate(desktop);
			}
			Threads.sleep(2000);// Update each 2 seconds
		}
	} catch (InterruptedException ex) {
		ex.printStackTrace();
	} finally {
		if (desktop.isServerPushEnabled())
			desktop.enableServerPush(false);
	}

}

The problem is Executions.getCurrent().getParameter("xxx") always return null. I test that the URL contains the "xxx" parameter.
So how can I get parameter in the process() method correctly? (the URL parameter maybe changed when user selects combobox in prj1)

link publish delete flag offensive edit

answered 2009-04-24 01:59:46 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2009-04-24 02:01:45 +0800

I still don't know the "project1 post a form to project2" mean. is this post trigger on server side back-end? not in client ?
let me make sure some scenario,
1. a user open a browser connect to page (has a combo box) in site 1 (project 1)
Client -> Site A
2. user select a value combobox, in server side onSelect, it (send)post a form to another page in another site.
Client -> Site A
3. in this send/post handler , it do some server push(I think it is server to server message, not relate to client)
Site A -> Site B (GMap page)

I think you are confused with ZK/Web App life cycle and concept and scope,
or I am confused with your case.

link publish delete flag offensive edit

answered 2009-04-24 02:48:29 +0800

mathsniper gravatar image mathsniper
30 1

updated 2009-04-24 02:52:52 +0800

thanks for your reply.
the step one and two are correct.
for step three, the site B(Gmap page) will receive the URL(do server push for updating Gmap). the client in site B will check the URL(actually, the URL has some parameter(like session_key) to specify which client can receive it). If the session_key and "message name"(provided by my API) is match, a client in site B can do process.
Here is a canRun() method code in GmapUpdate class.

	public boolean canRun() {
		boolean runProcess = true;
		String msgXml = //the message should be got from the newest parameter
				//(String) desktop.getAttribute("xml");
		if (msgXml != null) {
			Message msg = MessageUtils.getMessageFromXML(msgXml);
			if (msg.type.equals("org.web3.imhungry?food_data")) {
				foodInfo.setAddress((String) msg.getData("address"));
				foodInfo.setDescription((String) msg.getData("description"));
				foodInfo.setFlickrText((String) msg.getData("flickr_text"));
				foodInfo.setFoodName((String) msg.getData("food_name"));
				foodInfo.setRating(Integer.parseInt((String) msg
						.getData("rating")));
				foodInfo.setRestaurantName((String) msg
						.getData("restaurant_name"));
				
			}
		} else {
			runProcess = false;
		}
		return runProcess;
	}

server push is used for checking the URL by client in site B each time. maybe there's another solution but I still don't know.
So I need to get the newest parameter and update Gmap. I don't know why I get the parameter in GmapUpdate class is null. If I get the parameter successfully, all will be done.

link publish delete flag offensive edit

answered 2009-04-24 03:27:30 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2009-04-24 03:28:20 +0800

do you mean

User-> Client(A) ---------------=> Site A  --> a Message Post Process
                                                   | (A post a http request to Sib)
                                                   |
User-> Client(B) <--server push--> Site B <-- a Thread receive message and update desktop

link publish delete flag offensive edit
Your reply
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: 2009-04-22 07:54:14 +0800

Seen: 1,162 times

Last updated: Apr 24 '09

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