Code running in background on Google App Engine

Asked

Viewed 114 times

0

I am making a "website" in GAE Java version to serve as a notifier when a source with results that I am waiting for goes online. You’re supposed to check the link in question every 24 hours. I already have everything working, but as usual, the site is "loading" until you pass the waiting time limit I set (for test purposes, set 10 seconds). Now if the site has to stay charging for 24 hours until it comes time to check the site again, the browser will give me timeout or else I have to have a connected computer uploading the website, which goes against the whole purpose!

It is possible to make the program run on background there on the GAE servers if I put a thread prosecuting?

I have the following code working:

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

       connection();

       timer();

      System.out.println("shit");
}

private void timer() throws IOException{

  if((lastcheckedDate + (10000)) > System.currentTimeMillis())
  {
      waiting();
  }
}

private void check () throws IOException{

                connection();
              if (code==200)
              {
                  sendMessage();
              }
              else if (code==404){
                     System.out.println("Still not available");
                     timer();
              }
}

private void waiting() throws IOException{
      while((lastcheckedDate  + (10000)) > System.currentTimeMillis())
      {

      }
      check();
}

private void sendMessage() throws UnsupportedEncodingException{
System.out.println("Sending email notification");
  Properties props = new Properties();
  Session session = Session.getDefaultInstance(props,null);
  String msgBody = "The Vulcanus in Japan Shortlist seems to be available. Check the following link: " +
        URL;
  try {
      Message msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress("[email protected]", "Notifier"));
      msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]", "ipunna"));
      msg.setSubject("Change in x link");
      msg.setText(msgBody);
      Transport.send(msg);
  }catch (AddressException e) {
      System.out.println("Address Exception occured");
  }catch (MessagingException e){
      System.out.println("Messaging Exception occured");
  }
}

private void connection() throws IOException{
     URL vjselec = new URL(URL);
      connection = (HttpURLConnection)selec.openConnection();
      connection.setRequestMethod("GET");
      connection.connect();

      code = connection.getResponseCode();


       lastcheckedDate = connection.getDate(); 
       System.out.println("first Date: " +  lastcheckedDate);
       connection.disconnect();
}
  • Have you seen how the Tasksqueues in GAE? I think it can help you, although I don’t quite understand what you want to do.

1 answer

1

The Task Queue Java API will help in what you need: https://developers.google.com/appengine/docs/java/taskqueue/

I put a few examples for you to understand how it works...

Tasks within transactions:

DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Queue queue = QueueFactory.getDefaultQueue();
try {
    Transaction txn = ds.beginTransaction();

    // ...

    queue.add(TaskOptions.Builder.withUrl("/path/to/my/worker"));

    // ...
    txn.commit();
} catch (DatastoreFailureException e) {
}

Eliminate tasks:

// Purge entire queue...
Queue queue = QueueFactory.getQueue("foo");
queue.purge();

// Delete an individual task...
Queue q = QueueFactory.getQueue("queue1");
q.deleteTask("foo")
  • Solved! For what you wanted, you didn’t even need to create tasks. I used Push Queues that, in the time interval I set, repeated the access to the URL where I have the essential code for execution until it returns 200 HTTP status. It simplified my code much more, without having to have those loops while that I had before.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.