Configuring Functions

You can register GemFire functions for remote execution. You declare GemFire functions as Spring beans, which must implement the com.gemstone.gemfire.cache.execute.Function interface or extend the com.gemstone.gemfire.cache.execute.FunctionAdapter class.

For example, the following Spring application context XML configuration (from the basic/function example) declares a function:
 <gfe:function-service>
    	<gfe:function ref="calculateTotalSalesForProduct"/>
    </gfe:function-service>
    
The following Java class (from the basic/function example) extends the FunctionAdapter class to define the CalculateTotalSalesForProduct function:
public class CalculateTotalSalesForProduct extends FunctionAdapter {
	private static Log log = LogFactory.getLog(CalculateTotalSalesForProduct.class);

	@Resource(name="productTemplate")
	private GemfireTemplate productTemplate; 
	@Resource(name="Order")
	private Region<Long,Order> orderRegion;
	
	/* (non-Javadoc)
	 * @see com.gemstone.gemfire.cache.execute.FunctionAdapter\
           #execute(com.gemstone.gemfire.cache.execute.FunctionContext)
	 */
	@Override
	public void execute(FunctionContext functionContext) {
		ResultSender<BigDecimal> resultSender = functionContext.getResultSender();
		
		String productName = (String)functionContext.getArguments();
		
		log.debug("searching for product name '" + productName + "'");
		
		SelectResults<Product> results = 
    productTemplate.query("name = '" + productName + "'");

		if (results.isEmpty()) {
			log.warn("cannot find product '" + productName + "'");
			resultSender.lastResult(new BigDecimal(0.0));
			return;
		}
		
		Product product = results.asList().get(0);
		
		long productId = product.getId();
		
		BigDecimal total = new BigDecimal(0.0);
		
		for (Order order: orderRegion.values()) {
			for (LineItem lineItem: order.getLineItems()) {
				if (lineItem.getProductId() == productId) {
					total = total.add(lineItem.getTotal());
				}
			}
		}
		
		resultSender.lastResult(total.setScale(2,BigDecimal.ROUND_CEILING));
	}

	/* (non-Javadoc)
	 * @see com.gemstone.gemfire.cache.execute.FunctionAdapter#getId()
	 */
	@Override
	public String getId() {
		return getClass().getSimpleName();
	}
}

Additional Resources