Sample code for creating risk firewalls in multiple monitors
The following example shows the creation of risk firewalls in three different monitors.
The first monitor creates a risk firewall instance, registers a rule class, and adds a rule class instance. After the risk firewall is unlocked, the monitor tries to submit an order to it by using the risk firewall order sender. The set of events after creating the risk firewall are asynchronous and are pended until after the CMF Service Framework is activated, the risk firewall is created, the rule class is registered, and the rule class instances are added. OMS events are not pended by the risk firewall. Consequently, there is no attempt to send the new order immediately after the call to unlock the risk firewall.
The second monitor connects to the risk firewall created in the first monitor. The second monitor uses the risk firewall's order receiver to register a callback. This callback receives any new orders that the risk firewall approves and logs them.
The third monitor also connects to the risk firewall created in the first monitor. It queries the remotely connected risk firewall and logs any failures.
using com.apama.firewall.RiskFirewallFactory;
using com.apama.firewall.RiskFirewall;
using com.apama.firewall.QueryRequest;
using com.apama.firewall.CombinedQueryResponse;
using com.apama.firewall.QueryConstants;
using com.apama.utils.Params;
using com.apama.oms.NewOrder;
monitor RiskFirewallExample13 {
context mainContext := context.current();
action onload() {
// Use a risk firewall factory to create a new risk firewall
// instance called "MyFirewall" in the current context.
// Use the default configuration parameter settings.
RiskFirewall rfw :=
(new RiskFirewallFactory).create( mainContext, "MyFirewall" );
// Register a risk firewall rule class.
rfw.registerRuleClass( (new MyRiskFirewallRuleClass).create() );
// Define the configuration for the new rule class instance.
Params config := new Params;
config.addParam( "SYMBOL", "APMA.L" );
config.addParam( "PRICE_LIMIT", (22.2).toString() );
// Add the rule class instance to the risk firewall.
integer instanceId := rfw.addRuleInstance(
"MyRiskFirewallRuleClass", config );
// Unlock the risk firewall to allow orders to be processed.
rfw.unlockCb( cbOnFirewallUnlocked );
}
// This action is called when the risk firewall is unlocked.
action cbOnFirewallUnlocked ( RiskFirewall rfw ) {
// Send a new order to the risk firewall.
rfw.getOrderSender().sendOrder( NewOrder(
"orderId_1", "APMA.L", 1.0, "BUY", "MARKET", 10,
"TargetService", "", "", "TargetMarket", "", "TraderA",
new dictionary<string,string> ) );
}
}
// This monitor receives any approved OMS events from the
// risk firewall and logs them.
monitor RiskFirewallExample14 {
context mainContext := context.current();
action onload() {
// Using a risk firewall factory to connect to the
// risk firewall created in the previous monitor.
RiskFirewall rfwRemote :=
(new RiskFirewallFactory).connect( mainContext, "MyFirewall" );
// Add a receiver for orders approved by the risk firewall.
// Add a callback to be executed after an order is approved.
integer refId := rfwRemote.getOrderReceiver().
addAcceptedOrderCallback( cbOnApprovedNewOrders );
}
// Set up a callback handler for new orders that the
// risk firewall has approved.
action cbOnApprovedNewOrders ( NewOrder order ) {
log "Firewall has allowed the NewOrder: "+order.toString();
}
}
// This monitor checks for any queries that have failed and logs them
// This is similar to how an auditor part of an application might work.
monitor RiskFirewallExample15 {
context mainContext := context.current();
action onload() {
// Use a risk firewall factory to connect to the risk firewall
// that was created in the first monitor, "MyFirewall".
RiskFirewall rfwRemote :=
(new RiskFirewallFactory).connect( mainContext, "MyFirewall" );
// Add a query response callback to monitor the state of all queries
// in the risk firewall.
integer refId := rfwRemote.addQueryResponseCallback(
cbQueryResponseHandler );
}
// Set up a callback handler for queries that the risk firewall
// has processed.
action cbQueryResponseHandler(
RiskFirewall rfwRemote,
QueryRequest query,
CombinedQueryResponse response ) {
// Report any errors.
if( response.overallResponse = QueryConstants.QUERYRESPONSE_FAIL )
then {
log "The risk firewall rejected the following query:
"+query.toString() at ERROR;
}
}
}