Task Queue Generating TransientFailureException

Task queue operations may rarely generate a TransientFailureException, which will look similar to this:

com.google.appengine.api.taskqueue.TransientFailureException:
    at com.google.appengine.api.taskqueue.QueueApiHelper.translateError(QueueApiHelper.java:106)
    at com.google.appengine.api.taskqueue.QueueApiHelper.translateError(QueueApiHelper.java:153)

In general this exception indicates a temporary issue with the underlying App Engine infrastructure, not a failure of the application code. This problem should fix itself shortly.

To mitigate this exception, catch it with a try/catch block and attempt to requeue the task. If that fails, insert a delay of a few seconds before attempting to requeue the task. Redeploying the application may also help.

Aliasing Imports In Golang

A quick note: Go allows the aliasing of imports. For instance, the below line of code imports the appengine/datastore package under the name aedatastore :

import aedatastore "appengine/datastore"

Then the application can call datastore services under the alias name:

aedatastore.Get(c, key, &entity)

This aliasing is particularly useful when package names collide. Assume that an application needs to import the Go image and appengine/image packages. Since both packages are named image, the Go compiler will issue an image redeclared as package name error if both are imported. To avoid this, alias one of the imports:

import "image"
import aeimage "appengine/image"

Retrieving The User’s IP Address

Retrieving the originating IP address of the request is simple in most languages.

Here’s how to retrieve the IP address in Java ( req represents a javax.servlet.http.HttpServletRequestreference ):

String ip = req.getRemoteAddr();

Here’s the same line in Go ( r represents http.Request ):

ip := r.RemoteAddr

In PHP, the originating IP can be retrieved from the predefined variable SERVER :

$_SERVER['REMOTE_ADDR']

Handling Form Data In Go

Here’s how to retrieve the value of a form element named comment ( r represents http.Request ):

comment := r.FormValue("comment")

Suppose the form value is an integer. Here’s how to extract the value from the request and convert it from a string to an integer (if an error occurs when converting, the value -1 is stored):

index, err := strconv.Atoi(r.FormValue("index"))    
if err != nil {
    index = -1
}

If needed, the submitted form value can be written to logging by calling:

c := appengine.NewContext(r)
c.Infof("Submitted Value: %v", index)

Templating In Go

The Go language contains an extremely powerful templating engine which simplifies the creation of web pages. Here’s a quick overview of how it works.

First of all, create a set of template HTML files. Wherever dynamic content should be added, add a {{.name-of-property}} annotation. Here’s an example:

<div class="row">
    <div class="span12">{{.propertyone}}</div>
    <div class="span8">{{.propertytwo}}</div>
</div>

In the above example, propertyone and propertytwo represent keys which will be replaced by values later on.

Next, make these template files available to the Go application. This line of code pulls in an entire folder of template files (assuming that templates is a root level directory):

var templates, templates_err = template.ParseGlob("templates/*")

Alternately, a set of templates can be called in by name:

var templates = template.Must(template.ParseFiles("templates/one.html", "templates/two.html", "templates/three.html"))

Using template.Must ensures that all the templates files are loaded in – if it is unable to find a named template file, template.Must sets off a panic.

Next, create a map of strings to display on the page. We associate the keys (which were set within the HTML of the template file) with values that will replace the annotations:

//This map holds the data to display on the page.
display_data := make(map[string]string)
display_data["propertyone"] = string_one
display_data["propertytwo"] = string_two
display_data["propertythree"] = string_three

Finally, execute the template. W represents a http.ResponseWriter reference to write the completed template to, one.html represents the name of the template to use, and display_data is the map that defines the values to use when replacing the annotations:

//Put the data into the template and send to the user.
templates.ExecuteTemplate(w, "one.html", display_data)

Remember to import the html/template package before using this code.

Returning Status Codes In Golang

Here’s an overview of how to send HTTP status codes in Go. W represents a http.ResponseWriterreference, and r is a http.Request reference.

The following line tells the browser to redirect to the URL given (a 301 is a Moved Permanently redirect):

http.Redirect(w, r, "http://learntogoogleit.com/", 301)

Here is a standard Not Found error:

http.Error(w, http.StatusText(404), 404)

An Internal Server Error is indicated using a 500 status code:

http.Error(w, http.StatusText(500), 500)

The above examples use StatusText to retrieve standard error text, but you can set your own text if needed:

http.Error(w, "Too Many Requests", 429)

Extracting Files From A Zip File

Here’s a code snippet demonstrating how to download a zip file and extract the files contained within.

The variable zip_url_string represents the URL to download the zip file from. To read in each file, put your code after the BufferedReader statement.

/**
 * Our first task is to grab the file, and open up a ZipInputStream to decompress the file.
 * A ZipEntry processes each entry within the ZipInputStream.
 */
URL url_addr = new URL(zip_url_string);
HttpURLConnection con = (HttpURLConnection)url_addr.openConnection();
InputStream fis = con.getInputStream();
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry = zis.getNextEntry();
/**
 * We are going to loop through each ZipEntry until we get to a null entry (which 
 * represents the end of the ZipEntry list within the ZipInputStream).
 **/
while (entry != null) {
    //Collect the entry name. This is the filename, including any directory information.
    //Do any validation or processing needed.
    String entry_name = entry.getName().trim();
    //Create a BufferedReader to read in the contents of this file contained within the zip.
    InputStreamReader isr = new InputStreamReader(zis);
    BufferedReader reader = new BufferedReader(isr);
    /**
     * Do something here with the file. Use BufferedReader to read it in, then  
     * process it as necessary.
     */
    //Grab the next entry for processing
    entry = zis.getNextEntry();
}//end while loop going through ZipEntry entries

If the zip file being downloaded is especially large, you may want to add a longer timeout:

con.setConnectTimeout(milliseconds_to_wait);

Remember to add the appropriate import statements:

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.util.zip.*;

A Simple Backends.XML File For Java Deployments

Here’s a simple example of the XML required to set up a backend:

<backends>
    <backend name="worker">
        <class>B2</class>
        <options>
            <dynamic>true</dynamic>
            <public>true</public>
        </options>
    </backend>
</backends>

This sets up a B2 class backend named worker which can be addressed at the URL worker . APPLICATION-ID . appspot . com . Save this as backends.xml in the /war/WEB-INF/ directory.

App Engine Default Time & Date

The default time zone for the Java runtime on App Engine is UTC (frequently referred to as GMT). The following code will record the current time and date in UTC:

Date current_date = new Date();
String date = new java.text.SimpleDateFormat("MM-dd-yyyy").format(current_date);
String time = new java.text.SimpleDateFormat("hh:mm:ss aa").format(current_date);

For instance, date would be set to 09-21-2013 (September 21, 2013) and time would be set to 01:40:42 AM . If your application needs to record time in a different time zone, use the setTimeZone method of SimpleDateFormat .

Don’t forget to import the Date class:

import java.util.Date;