Question

I have a cron job that I want to run that calculates a popularity rating using the Entities in my Cloud Datastore.

Entity Excerpt:

     @Entity
public class Theme {

    @Id
    private String themeID;
    public int popularity;

public void setPopularity(int popularity) {
    this.popularity = popularity;
}

Inside my CronController:

@SuppressWarnings("serial")
public class CronController extends HttpServlet {
    private static final Logger _logger = Logger.getLogger(CronController.class.getName());
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        try {
            _logger.info("Cron Job has been executed");

            EntityManager mgr = getEntityManager();
            Theme theme = null;

                theme = mgr.find(Theme.class, "101");
                theme.setPopularity(3);
        }
        catch (Exception ex) {
            //Log any exceptions in your Cron Job
            _logger.info("error");
        }
    }

I know the cron job runs as required because if I change "101" to (long) 101 it fails stating a String is required in the log however if coded as above, the log file gives the following:

0.1.0.1 - - [19/Feb/2014:17:38:37 -0800] "GET /cron/popularity HTTP/1.1" 200 0 - "AppEngine-Google; (+http://code.google.com/appengine)" "themeviewer.appspot.com" ms=14 cpu_ms=233 queue_name=__cron task_name=243a771773a2bbc80eed32256a5b5043 app_engine_release=1.9.0 instance=00c61b117cabbfbf9dcbf5dedbeb449cc73567

I can't figure out how to make it log an error to tell me why the Entity "101" doesn't have a popularity value of 3 after this cron job runs. How can I debug this?

On a side note, the log files don't show "Cron Job has been executed" either... I am using the following website as a tutorial here

Was it helpful?

Solution

There is nothing wrong with the Cron Job execution. The reason that you are not seeing the data getting persisted is that, you not told the Entity Manager to persist it , etc.

I suggest to try out the following:

  • After you call theme.setPopularity(3); , please call mgr.persist(theme);
  • It is recommended to have a finally clause too, so that you can close the EntityManager instance i.e. mgr.close();

Refer to : https://developers.google.com/appengine/docs/java/datastore/jpa/overview

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top