Pivotal Knowledge Base

Follow

Calling Function Execute with timeout

Applies To

Tested on Gemfire 8.1

Purpose

The Function Execution framework allows for the clients to "time out" while they wait for a result.

Solution

The current Function Executor framework allows GemFire clients to submit code which can be executed on GemFire servers using GemFire server threads. In the Execution interface in the GemFire Java API documentation Java API, there is an execute() method, which can take a function ID that needs to be executed (for example, you can write a FunctionExecutor class that calls a function "FindCustomerByCity" that, in turn, is registered with the GemFire server (and is located on the GemFire server classpath).

public class FunctionExecutor {
  private ApplicationContext appContext;
  private Region<Integer, Customer> cusRegion;
  
  public static void main(String[] args) {
    FunctionExecutor exe = new FunctionExecutor();
    exe.getCustomerRegion();
    exe.execute();
  }
  
  public void execute(){
    Execution exe = FunctionService.onRegion(cusRegion).withArgs("San Francisco");
    ResultCollector result = exe.execute("com.pivotal.bookshop.functions.FindCustomerByCity");
    System.out.println("Result :"+result.getResult());
  }

  private void getCustomerRegion(){
    appContext = new ClassPathXmlApplicationContext("META-INF/spring/gemfire/spring-client-cache-config.xml");
    this.cusRegion = appContext.getBean("Customer",Region.class);
    
  }
}

Examining the execute() method, in the first line we create an Execution object by calling FunctionService.onRegion() and passing as a parameter on which "region" we intend to execute the function, as well as optionally passing any additional variables.

Execution exe = FunctionService.onRegion(cusRegion).withArgs("San Francisco");

After that we make a call to the Execution class to execute the function on the GemFire server. This is a blocking call. The execute() method waits for all the results from all the servers that are executing the method. With GemFire 8.1 a system property which can be used to "timeout" the execute methods is introduced, which, under the covers, sets a timeout on the socket it opens with the server. This timeout can be configured using the property gemfire.CLIENT_FUNCTION_TIMEOUT, where the time is given in milliseconds.

ResultCollector result = exe.execute("com.pivotal.bookshop.functions.FindCustomerByCity");

After that, we call the newly created ResultCollector object to get the results. This call collates all the results returned.

result.getResult(timeout, TimeUnit)

If a timeout occurs, this call will return a FunctionException which can be handled by the caller.

Comments

Powered by Zendesk