Monetizing service invocations
You can set the cost of a service's invocation through a monitor rule.
This price gets logged as the invokeCost
property of invoke documents.
And using the MonitorMethods.getMonetizationBill(...)
one-liner, you can compute the total cost of
a given set of invoke documents (selected through a query).
In this section, we'll explore how to apply advanced monetization (costs) based on these rules.
Monitor rules have a field called Callback Service
. This corresponds to the
service that needs to be invoked when a request is matched and executed successfully. You can use this
facility to implement custom logic for computing the pricing schemes according to your organization's requirements.
Below is a service that adds a 1.5x surcharge to requests sent 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 rule the rule object that holds this service * @param builder builder for invoke document object */ void costAfterHours( MonitorRuleMetadata rule, InvokeDocument.Builder builder ) { def hour = new GregorianCalendar().get( Calendar.HOUR_OF_DAY ) if ( hour < 9 || hour > 16 ) { def multiplier = 1.5g def baseCost = rule.cost ?: BigDecimal.ONE builder.invokeCost( baseCost * multiplier ) } } |
InvokeDocument.Builder
In this example, we set a new cost for our InvokeDocument
through the
InvokeDocument.Builder
's invokeCost(int)
method.
Aside from invokeCost
, you can configure other document properties through the builder.