FIX Edge Java provides a
RoutingRule unit as an abstraction for internal message routing element. FEJ supports pure Java and Groovy implementations for routing rules.
Groovy routing rules are flexible mechanism that allows to implement custom logic for messages transformation and handling events.
The rules can be embedded in a rules.groovy file:
The example represents two different types of rules - MessageRoutingRule and EventRoutingRule.
Groovy message routing rules syntax
messageRule helps to build a rule for message processing. It requires a few components for its instantiation:
messageRule(String description, SourceCondition sourceCondition, RuleCondition condition, RuleAction action)
description - String with free text description of rule
source filter - check if this rule should be applied to messages from certain source. This filter was added as a separate with propose of optimization process. Such filter can by applied on static basis without addition affect in runtime. Source filter is
SourceConditionimplementation and can be null if you’d like to ignore it.
context filter - dynamic filter, which can check in the same time appliance of this rule depends on message content and source attributes. Context filter is
RuleConditionimplementation and can be null if you’d like to ignore it.
action - implementation of
RuleActionwhich describes the main goal of this rule. It can be transformation, modification or just resending to required destination.
It is also possible to use "helper" method in case when one of several sections are not implemented
Free text description of rule.
It returns True or False.
Returning value is True or False, according to this value code in action section is called. Rule condition is usually used as a filter for routing rule.
Example bellow demonstrate a filter by message type. Action section will be called only for Order (D) messages in the example.
The variable ctx here is used to access to the context of the message, it has the following methods
|Parameters of endpoint which received the message.|
|Specifies not to execute any rules after execution current one.|
Defines the actions that will apply to the messages that correspondes to the SourceCondition and RuleCondition filters.
ctx variable here is the same as for RuleCondition.
Groovy event routing rules syntax
eventRule method helps to build a rule for event processing. It requires a few components for its instantiation:
eventRule(String description, Class<AppEvent> eventType, Predicate<AppEvent> ruleCondition, Consumer<AppEvent> ruleAction)
description - string with free test description of rule
- event type - type or subtype of event for which the rule is applied.
- rule condition - dynamic filter, which can check in the same time appliance of this rule depends on event content and source attributes. Context filter can be null if you’d like to ignore it.
rule action - describes the main goal of this rule.
Type of the events for which the rule is applied. Subtype or general type can be specified.
Available list of types and their methods
|Event category||Class of event||Methods||Description|
|The basic class of all events, can be used for handling all the events system throws.|
|Notifies about the changes in state of the FIX server.|
getSessionState(): CONNECTING, WAITING_FOR_LOGON, CONNECTED, WAITING_FOR_LOGOFF, DISCONNECTED, LOGON_RECEIVED, DEAD, DISCONNECTED_ABNORMALLY, RECONNECTING, WAITING_FOR_FORCED_LOGOFF, WAITING_FOR_FORCED_DISCONNECT
|Notifies about the changes in state of the FIX sessions.|
reject(String reason, boolean quiet)
The event is intended for accepting or rejecting new incoming session. The session will be rejected if reject method is called or accepted if not.
note: if reject method is called, the next rule for the event will not be processed.
|getId(): String||Event that is created by scheduler when the trigger executes.|
|SnFEvent||getSource(): MESSAGE_WAS_QUEUED, MESSAGE_WAS_SKIPPED|
|Notifies that there is an exception in some of the rule processed.|
|getMessage(): FIXFieldList||Notifies that there are no acceptable rules defined for the specified message.|
note: If BasicAppEvent is specified in eventRule method as a event type parameter, all the events above will be trigger the rule.
Example of scheduler settings - an event SchedulerEvent will be published every 10 seconds.
Groovy routing rules "helpers" methods
There is a static class with methods which name "helpers", they help end-user make logic shorter by encapsulating the details inside the methods.
returns UUID that is generated using a cryptographically strong pseudo random number generator.
def id = generateUniqueId()
|stringValue||String stringValue(FIXFieldList msg, int tag)|
returns string representation of value by provided tag
def destination = stringValue(msg, Header.DeliverToCompID)
RoutingRule messageRule(String description, SourceCondition sourceCondition, RuleCondition condition, RuleAction action)
short version for creating MessageRoutingRule
RoutingRuleBuilder messageRule(String description)
rule builder for creating MessageRoutingRule
EventRoutingRuleBuilder eventRule(String description)
rule builder for creating EventRoutingRule
|eventRule||RoutingRule eventRule(String description, Class<AppEvent> eventType,|
Predicate<AppEvent> ruleCondition, Consumer<AppEvent> ruleAction)
short version for creating EventRoutingRule
|removeRepGroup||void removeRepGroup(FIXFieldList msg, int leadingTag, int groupTags)|
removes the list of tags that corresponds to the repeating group.
removeRepGroup(msg, 453, [452, 448, 447])
|send||void send(RoutingContext rc, RuleContext ruleContext, String targetCompId)|
sends a message to the session by provided targetCompId
send(routingContext, msgCtx, destination)
|send||void send(RoutingContext rc, RuleContext ruleContext, String targetCompId, String qualifier)|
sends a message to the session by provided targetCompId and sessionQualifier
send(routingContext, ctx, destination, qualifier)
Import of the classes
It is not necessary to import classes directly in the groovy.rules file. The classes are described in the fej-routing.xml. The values bellow are predefined and can be extended by custom ones not to specify imports in rules.
This properties available for use in groovy rule.