Skip to content

Overview

Development

Overview

IDEs

API Explorer

Monetizing Requests

In the previous section, we learned that we can throttle requests and fixed-priced costs to a request based on specific rules. In this section, we'll explore how to apply advanced monetization (costs) based on these rules.

When configuring rules, there is a field called service. This corresponds to the service that needs to be invoked when a request is matched. We can use this facility to implement custom logic for computing the pricing schemes according to your organization's requirements.

Below is a Groovy service that adds a 1.5x surcharge to any requests outside of working hours:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/**
 * Rule calculates such that requests outside 9-5 costs 1.5x the rule rate.
 * 
 * @param request the request that this rule matched
 * @param rule the rule object that holds this service
 */
void costAfterHours(HttpServletRequest request, MonitorRule rule) {
    GregorianCalendar gc = new GregorianCalendar()
    int hour = gc.get( Calendar.HOUR_OF_DAY )
    if ( hour < 9 || hour > 16 ) {
        Double ruleCost = monitorRule.getCost() != null ? monitorRule.getCost().doubleValue() : 1d
        request.setMonitorCost( ruleCost * 1.5d )
    }
}

HttpServletRequest#setMonitorCost(double)?

In the example, the HttpServletRequest is invoking a method called setMonitorCost(double cost). This is an extension method provided by TORO Integrate to simplify cost updates from your scripts.

Aside from using methods in MonitorMethods1, you can also add a request scope attribute to your request named ESB_MONITOR_COST to set the cost of the service invocation. You can directly add this attribute to your request or set it via code, like so:

1
2
double cost = 1d
request.setAttribute('ESB_MONITOR_COST', cost)

  1. Specifically setMonitorCost(...) and addMonitorCost(...)