Code to run in background in Google App Engine

0

I'm doing a "site" in GAE Java version to serve as a notifier when a source with results that I'm expecting goes online. It is supposed to check the link in question every 24 hours. I already have everything working, but as is normal, the site is "loading" until the limit of the waiting time that I set has passed (for test purposes, set 10 seconds). Now if the site has to load for 24 hours until it's time to check the site again, the browser will give me timeout or else I have to have a connected computer to load the website, that goes against the whole purpose!

Is it possible to run the program in background there on the GAE servers if I put a thread to process?

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();
}
    
asked by anonymous 04.02.2014 / 14:24

1 answer

1

The Task Queue Java API will help you with what you need: link

I've put some examples to help you 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) {
}

Delete Tasks:

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

// Delete an individual task...
Queue q = QueueFactory.getQueue("queue1");
q.deleteTask("foo")
    
04.02.2014 / 17:06