RSS

Google Java App Engine

This entry was posted on May 13 2009

I just started working with the Google Java App Engine.

  • + It’s super easy to deploy to
  • -The persistence feels really unatural. You just create JPA objects and use their persister, no DB create scripts
  • + No DB create scripts, I think I’m going to like this once I’m used to it
  • - I’m not sure how data export and migration works

Pretty neat though, you should try it out if you can still sign up for the beta.

You need a persistent class.

package com.nthread.babelhelp.datamodel;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class HelloGDB {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	@Persistent
	private int count;

	public HelloGDB(int count) {
		this.count = count;
	}

	public Long getId() {
		return id;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}
}

You have to use a singleton to keep your static PersistenceManagerFactory - if you try to instantiate it twice, you will find your app pretty unhappy.

package com.nthread.babelhelp.model;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

public class Persistor {
	private static PersistenceManagerFactory pmfInstance =
		JDOHelper.getPersistenceManagerFactory("transactions-optional");

	public static PersistenceManager getManager() {
		return pmfInstance.getPersistenceManager();
	}
}

I just use these things from a Servlet, it’s pretty basic.

package com.nthread.babelhelp.control;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nthread.babelhelp.datamodel.HelloGDB;
import com.nthread.babelhelp.model.Persistor;

public class HelloWorldServlet extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
		doRequest(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
		doRequest(req, resp);
	}

	protected void doRequest(HttpServletRequest req, HttpServletResponse resp) {
		PersistenceManager pm = Persistor.getManager();

		String query = "select from " + HelloGDB.class.getName();
		List hellos = (List) pm.newQuery(query).execute();

		HelloGDB helloGDB = null;
		if (!hellos.isEmpty()) {
			helloGDB = hellos.get(0);
		} else {
			helloGDB = new HelloGDB(0);
		}
		helloGDB.setCount(helloGDB.getCount() + 1);
		try {
			pm.makePersistent(helloGDB);
		} finally {
			pm.close();
		}

		String str = "Current count is " + helloGDB.getCount();
		try {
			resp.getWriter().write("" + str + "");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Post a Comment