Blog

How to use CodeRunner – Cloud Code Utility

by on May 1, 2015

In a previous post, I described how to use the custom business logic code generator to create Backendless timer code. The previous post left off at the step when the Backendless Console created the code. To download the project files with the source code click the Download button:

The downloaded file is a ZIP archive of the project. The contents of the archive would have the same structure as shown below:

There is a project file for IntelliJ IDEA, however if you use Eclipse, it is very easy to import the project in it as well. Open the project and you should be able to compile it without any changes. The code straight out of the code generator does not have any custom business logic –  it is a wrapper required by the Backendless framework. To add custom business logic open the timer class, it will be in the hierarchy of the /src directory. I have added custom code which stores an object in Backendless Data Store every time the timer runs:

package com.backendless.orderprocessing.timers;
import com.backendless.Backendless;
import com.backendless.messaging.BodyParts;
import com.backendless.orderprocessing.models.Person;
import com.backendless.servercode.annotation.BackendlessTimer;
/**
* ReminderTimer is a timer.
* It is executed according to the schedule defined in Backendless Console. The
* class becomes a timer by extending the TimerExtender class. The information
* about the timer, its name, schedule, expiration date/time is configured in
* the special annotation - BackendlessTimer. The annotation contains a JSON
* object which describes all properties of the timer.
*/
@BackendlessTimer("{'startDate':1430542380000,'frequency':{'schedule':'custom','repeat':{'every':60}},'timername':'Reminder'}")
public class ReminderTimer extends com.backendless.servercode.extension.TimerExtender
{
  @Override
  public void execute( String appVersionId ) throws Exception
  {
      System.out.println( "Running custom code" );
      try
      {
          long counter = Backendless.Counters.getAndIncrement( "age" );
          Person person = new Person();
          person.name = "Spiderman-" + counter;
          person.age = 30;
          Backendless.Data.save( person );
      }
      catch( Throwable t )
      {
          t.printStackTrace();
      }
  }
}

Now that I have a Backendless Timer with custom code in it, the question is how to run it. Obviously, the code can be deployed to the Backendless cloud, however, wouldn’t it be nice to be able to run it locally first for testing and debugging purposes? The answer is ‘yes’! This is exactly what Backendless CodeRunner for Java does. The best part is CodeRunner is already included in the ZIP file generated by Backendless Console (the file downloaded at the beginning of the post).

If you use IntelliJ IDEA, the project is automatically configured to compile the code into the /classes directory shown in the screenshot above. The directory is special – this is where CodeRunner looks for custom code classes.

To run CodeRunner, open a command prompt window and change the current directory to /bin (see it in the screenshot above). Run CodeRunner using the scripts in the /bin directory. For example on a Linux system, run the following:

./CodeRunner.sh

You should see the following output:

Listening for transport dt_socket at address: 5005
[INFO] CodeRunner(tm) Backendless Debugging Utility
[INFO] Copyright(C) 2015 Backendless Corp. All rights reserved.
[INFO] Version: 1.13 Build date: 20150320-1727
[INFO] Registering runner on: https://api.backendless.com with secretKey: XXXXXX-XXXXXX-XXXXXX
[INFO] Runner successfully registered
[INFO] Parsing event model...
[INFO] Build successfully: EventModel{timers=1, eventHandlers=0}
[INFO] Deploying model to server, and starting debug...
[INFO] Model successfully deployed...
[INFO] Waiting for events...

CodeRunner automatically inspects the compiled code, determines that is has one timer and registers it with the Backendless ‘mothership’.