Generating A JAR Application For Compute Engine
Packaging Java applications for Compute Engine is slightly different than for App Engine. The code, plus any associated libraries and resources, needs to be built into a single JAR file. Here’s how to do that in Eclipse.
First, go to File – Export:
Select Java – Runnable JAR file from the options presented:
This screen sets up the JAR options. The Launch Configuration option sets the class file to run when the JAR is executed (in other words, the file containing the main(String[] args) function to start the application). Export Destination sets the directory to store the finished JAR file in.
Press Finish when you’re done. You can now use gcutil to upload the JAR file to your Compute Engine machine and run it using the standard java command (remember to install a JRE first).
URL Path To Array In Golang
Here’s a short Go function that converts the URL path into an array; it’s useful if you want to implement REST style queries into a simple application.
func parseComponents(r *http.Request) []string {
//Creates an App Engine Context - required to access App Engine specific services
c := appengine.NewContext(r)
//The URL that the user queried.
path := r.URL.Path
path = strings.TrimSpace(path)
//Log the URL received:
c.Infof("URL: ", path)
//Cut off the leading and trailing forward slashes, if they exist.
//This cuts off the leading forward slash.
if strings.HasPrefix(path, "/") {
path = path[1:]
}
//This cuts off the trailing forward slash.
if strings.HasSuffix(path, "/") {
cut_off_last_char_len := len(path) - 1
path = path[:cut_off_last_char_len]
}
//We need to isolate the individual components of the path.
components := strings.Split(path, "/")
return components
}
Useful Commands In Google Compute Engine
I’m currently building a Java application on top of a Compute Engine machine. Here’s a few commands I find useful:
Authorizing Access To A Project & Setting A Default Project:
gcutil auth –project=projectname
gcutil getproject –project=projectname –cache_flag_values
Open Up Firewall Port:
gcutil addfirewall name-of-firewall –description=“description of firewall” –allowed=“tcp:portname”
For instance, the following firewall rule opens up port 25 and names the firewall smtp:
gcutil addfirewall smtp –description=“Allow Incoming Mail” –allowed=“tcp:25”
Upload To A Server: gcutil –project=projectname push servername local-path remote-path
SSH To A Server: gcutil –project=projectname ssh servername
Installing Java: sudo aptitude install openjdk-7-jre-headless
Running A Java JAR: sudo java -jar application-file.jar
Free Tier Performance
Two weeks ago I posted a picture of a free tier application serving close to 52,000 requests in 1 day. Soon after that, the same application exceeded that by processing over 53,000 requests within a single day. Here’s a few pictures demonstrating the capabilities of GAE’s free tier:
First off, here’s a picture of the Current Load screen:
A graph of milliseconds/request over the same time period:
The instances used:
And finally requests/second:
App Engine’s free tier is generally enough to host applications during development, testing, and low-level production traffic. With that said, it’s always a good idea to activate billing if you believe that your application will be receiving more than small amounts of traffic.
Reading In A File With Java
Inspecting Task Queue Logs
Here’s a sample view of a request from the Task Queue service. The log includes the name of the queue (in this case default), the task’s name (the long number starting with 1636460), and the instance that handled the request (the blue text on the bottom).
An important note: a quick and easy way to figure out if a request comes from the Task Queue is to look at the request’s IP address. All Task Queue requests come from the IP 0.1.0.2.
Generating MD5 Hashes In Java
Retrieving All Recently Created Entities
Error Code 202
On high traffic App Engine applications, you may occasionally see a request fail with error code 202. You’ll see the following text in your logs:
A problem was encountered with the process that handled this request, causing it to exit. This is likely to cause a new process to be used for the next request to your application. (Error code 202)
The request log will look similar to the picture below:
Error code 202 is an internal App Engine failure code: an unknown error occurred within App Engine or its associated services, not from your application. This is not an error that your application can catch or recover from, nor is the application at fault for this error. The current request will fail, but the client is free to retry the request. If this request is from the task queue service, the task queue will automatically retry the request if you set the appropriate settings while creating the task.
A note about error codes: the code 202 relates to App Engine; it is NOT a HTTP status code. When a request encounters this internal error, it fails with the HTTP status code 500 (as you can see from the above picture).