Google App Engine

From Documentation




Get Your App Engine Account Ready

First, you have to sign up an App Engine account and download the App Engine SDK. Refer to the official website for details.

To use Google App Engine for Java, you have to take one additional step: sign up here.

Configure Your App Engine Project

Here we assume you created a App Engine project. If not, please refer here.

There are three files that you have to configure: web.xml, zk.xml and appengine-web.xml. They all reside in the WEB-INF directory.

The web.xml File

The content is similar to other ZK application except the AU engine has to be mapped to /zkau, too (in additions to /zkau/*. Otherwise, AU requests won't be sent to the AU engine. Here is is an example.

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
	<listener>
		<description>ZK listener for session cleanup</description>
		<listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>zkLoader</servlet-name>
		<servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
		<init-param>
			<param-name>update-uri</param-name>
			<param-value>/zkau</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet>
		<servlet-name>auEngine</servlet-name>
		<servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>zkLoader</servlet-name>
		<url-pattern>*.zul</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>auEngine</servlet-name>
		<url-pattern>/zkau/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>auEngine</servlet-name>
		<url-pattern>/zkau</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.zul</welcome-file>
		<welcome-file>index.zhtml</welcome-file>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

The zk.xml File

Google App Engine is a cloud service, so you have to enable the clustering: disable event threads and use serializable UI factory. In addition, Google App Engine doesn't allow users to create a working thread, so we have to disable the resend mechanism.

Here is an example.

<zk>
	<!-- clustering environment -->
	<system-config>
		<disable-event-thread/>
		<ui-factory-class>org.zkoss.zk.ui.http.SerializableUiFactory</ui-factory-class>
	</system-config>

	<!-- GAE doesn't allow user's thread -->
	<client-config>
		<resend-delay>-1</resend-delay>
	</client-config>
</zk>

The appengine-web.xml File

App Engine requires one addition configuration file named appengine-web.xml. It resides in the WEB-INF directory.

<sessions-enabled>true</sessions-enabled>

<static-files>
	<exclude path="/**.zul"/>
	<exclude path="/**.zhtml"/>
</static-files>
<resource-files>
	<include path="/**.zul"/>
	<include path="/**.zhtml"/>
</resource-files>

More Information

Due to the way App Engine serializes sessions, you have to use ZK 3.6.2 or later. In additions, there are some other limitations.

  • You cannot define functions in zscript, since BeanShell's method can not be serialized correctly[1].
  • You cannot use captcha due to the limit support of AWT[2]

  1. It runs correctly locally but not if uploaded.
  2. You will see a warning, ... Component captcha ignored., in the application log.

Session Memory Limitation

GAE limits the session memory to 1 mega bytes. If a user visits several pages (with different URLs) in the same browser session, there would be several desktops created and stored in the session, and it might run out the 1 mega bytes. To avoid this, you can implement DesktopInit to remove other desktops in the desktop cache. For example,

public class MyDesktopInit implements DesktopInit {
  public void init(Desktop desktop, Object req) throws Exception {    
    HttpServletRequest request = (HttpServletRequest) req;     
    //Remove old Desktop   
    String oldDesktopId = (String) request.getSession().getAttribute("currentDesktopId");   
    WebAppCtrl ctrl = (WebAppCtrl)Executions.getCurrent().getDesktop().getWebApp();   
    DesktopCache dc = ctrl.getDesktopCache(desktop.getSession());   
    dc.removeDesktop(dc.getDesktop(oldDesktopId));       
    //Add new Desktop
    request.getSession().setAttribute("currentDesktopId", desktop.getId()); 
  }
}

Sample

Download a sample application named zk-gae.

You can visit it at http://zk-gae.appspot.com/hello.zul

See Also

From forum

Composers on Google AppEngine?

Version History

Version Date Content
     



Last Update : 2011/03/12

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.