Blog

How to Use Cloud Code Timers (Overview)

by on September 1, 2019

There are two types of custom business logic (Cloud Code) scripts supported by Backendless – API event handlers and timers. In this post, we will review the latter. A timer is a server-side program deployed to the Backendless server infrastructure which is scheduled to run on a pre-defined schedule.

Once the code is deployed, Backendless makes sure the code runs exactly accordingly to the schedule. We handle all the runtime aspects – finding an available host to run the code, loading all the dependencies, and making sure the code runs securely within an isolated sandbox. Backendless supports a variety of schedules. A timer may be scheduled to run only once, or once a day at the specified time, every day, a few times a week, a month, a year, etc. Additionally, a timer may have an expiration date/time.

Timer code must be in Java, and we’re working on adding support for other server-side languages. The code may use the Backendless SDK for Java/Android to interact with the application’s data. The example below is a basic timer. All configuration properties of a timer must be specified in the @BackendlessTimer annotation.

The annotation accepts a JSON object (as a string) with all the timer properties. For example, the timer below is configured to start on the date/time defined in the startDate property of the annotation. The frequency property sets the timer schedule. The code below requests the timer to run every two days.

package com.backendless.myapp.timers;
import com.backendless.servercode.annotation.BackendlessTimer;
@BackendlessTimer("{'startDate':1430195040000,'frequency':{'schedule':'daily','repeat':{'every':2}},'timername':'Cool'}")
public class CoolTimer extends com.backendless.servercode.extension.TimerExtender
{
  @Override
  public void execute( String appVersionId ) throws Exception
  {
    // add your code here
  }
}

There is a lot more to creating Timer code. There is a process for testing the timer and deploying to the Backendless servers. These features are reviewed in the linked posts.

Leave a Reply