App Engine Production Upgrading To 1.8.4

App Engine is currently upgrading all applications to the 1.8.4 runtime. Here’s how the upgrade process looks on a free tier application.

On the main administration screen, this notice appears:

Clicking the Details link brings you to the Instances page, where this shows up:

The first instance loaded under the 1.8.4 runtime was launched on August 25, 14:43 PDT:

Geolocating A Tweet In Java

Here’s a demonstration of how to use the twitter4j library to post a tweet and set a location from where it was posted.

This example requires a twitter4j.twitter object (the twitter variable) to be preconfigured with authentication tokens. Replace the text Orlando, FL with a string describing the user’s location, and the GPS coordinates with your user’s coordinates.

    StatusUpdate status = new StatusUpdate(tweet_text);
    /**
     * We'll add location information for this tweet. 
     * Here, we tell Twitter that this tweet was made from Orlando, FL.
     * The GPS coordinates here are for Walt Disney's Hollywood Studios theme park.
     */
    status.placeId("Orlando, FL");
    GeoLocation geolocate = new GeoLocation(28.355269, -81.559390);
    status.setLocation(geolocate);
    status.setDisplayCoordinates(true);
    //And finally, post this tweet.
    try {
        twitter.updateStatus(status);
    }
    catch (TwitterException e) {
        System.err.println("Problem accessing Twitter. Exception message: " + e.getMessage());
    }

Remember the appropriate import:

import twitter4j.*;

The Cron Log

The following is an example of a request originating from a cron:

It’s easy to tell this request originates from cron because (1) the originating IP is 0.1.0.1 (which is a reserved IP; cron requests come only from that address) and (2) the queue name is listed as cron .

Adding A Cookie To A Response In Java

Here’s a basic example of how to set a cookie into a servlet response.

Cookie_name and cookie_value is the name and value for the cookie to store. The variable resp represents a javax.servlet.http.HttpServletResponse reference. Notice setMaxAge(int expiry) – this method sets how long the cookie should last before it expires in seconds. In this example I set a value of 2 weeks (multiplying the following together: 60 seconds, 60 minutes, 24 hours, 7 days, 2 weeks).

Cookie cookie = new Cookie(cookie_name, cookie_value);
cookie.setMaxAge(2 * 7 * 24 * 60 * 60);//Two weeks to expire, in seconds.
resp.addCookie(cookie);

Remember to add the import (the package includes the Cookie class and HttpServletResponse ):

import javax.servlet.http.*;

Checking To See If An Application Is In Production

Sometimes an application needs to be able to tell if it’s in production (deployed on App Engine servers) or in development (deployed on the dev appserver on a developer’s local machine). You can test this by querying the SystemProperty class. Here’s a code example:

String runtime_env = SystemProperty.environment.value().toString(); 
if (runtime_env.equalsIgnoreCase("Production")) {
    //This application is in production. Do something special.
}
else {
    //This application is NOT in production (perhaps the dev appserver)
}

Remember to import the appropriate class:

import com.google.appengine.api.utils.SystemProperty;

Retweeting In Java

Earlier I posted examples of how to log into Twitter and post a tweet using the twitter4j library. Now here’s a function demonstrating how to retweet a post.

This function is entirely self-contained; all it requires is a global variable twitter, which represents a twitter4j.Twitter object preconfigured with the proper authentication details.

public void doRetweet() {
    /**
     * To demonstrate retweeting, we'll search Twitter for all tweets that contain 
     * the phrase "mail not working" (including quotation marks). We'll then select 
     * a random tweet, and retweet it into our stream.
     */
    try {
        //Search for tweets, then pull out a list of those tweets.
        //A Status is how twitter4j refers to an individual tweet.
        twitter4j.Query q = new twitter4j.Query("\"mail not working\"");
        q.count(100);
        List<Status> results = twitter.search(q).getTweets();
        //Randomly select a tweet
        Random generator = new Random();
        int pick = generator.nextInt(results.size());
        Status status_to_retweet = results.get(pick);
        //Log the tweet we're retweeting
        System.out.println("Now retweeting: " + status_to_retweet.toString());
        //And finally retweet that tweet
        long status_id = status_to_retweet.getId();
        twitter.retweetStatus(status_id);
    }
    catch (TwitterException e) {
        System.err.println("A TwitterException has occurred while attempting to retweet. Exception message: " + e.getMessage());
    }
}//end function

Querying The Datastore In Golang

Here’s a demonstration of how to query the datastore in Go.

In this example we filter on PropertyOne, requiring it to be equal to true. You can also set other inequalities such as greater than ( > ). Kind is the kind of the entities to query, and PropertyTwodemonstrates ordering by descending order. CustomStruct is the struct that was used to create the entity. Remember to put your entity processing code just before the last brace ( } ).

//Search the datastore for entities
q := datastore.NewQuery(kind).Filter("PropertyOne =", true).Order("-PropertyTwo")
//Loop through each returned entity.
for t := q.Run(c); ; {
    //This represents the entity currently being processed.
    var x CustomStruct
    key, err := t.Next(&x)
    if err == datastore.Done {
        //This "error" means that we're done going through entities
        //Since we're done, break out of this loop.
        break
    }
    if err != nil {
        //Some other error happened. Report error and panic.
        c.Infof("Error in querying datastore: %v", err)
        panic(err)
    }
    //DO SOMETHING HERE WITH ENTITY x
}//end for going through q.Run

Oops! We Couldn’t Retrieve Your List Of Kinds.

Occasionally you may see the error Oops! We couldn’t retrieve your list of kinds from the datastore viewer screen of the App Engine admin console:

Generally this is a transient error: it essentially means that the App Engine admin console is currently too busy to show a view of your datastore’s contents. Wait a few minutes and refresh the page, your datastore’s information should appear.

Seeing this error can also mean that the datastore is empty; for example, if it’s a just-created application.