Runtime components : Service components : Smart channel services : Smart data collector : Server collector : Sampling for the server collector
  
Sampling for the server collector
Sample
Smart data collector supports global rule and transaction rule. If the transaction rule is configured, it collects according to transaction rule; if the transaction rule is not configured, it checks the global rule; The collector performs collecting even if the transaction rule and global rule are neither configured. Refer to the general steps in Server collector, and configure the rule in global level or transaction level.
Following is the sample code for the sampling:
<SmartCollectorConfig
id="smartCollectorConfig"
sampleRateRuleServiceName="globalDataCollectorRuleService"
smartDAO="smartDAOImpl">
<ref Injection="transactionCollectorConfig"
refId="TransactionDataCollectorConfig"/>
</SmartCollectorConfig><java.util.ArrayList
id="TransactionDataCollectorConfig">
<SmartCollectorConfig4Transaction transactionID="accountTransferOp"
sampleRateRuleServiceName="transferDataCollectorRuleService"
extendBean="com.ibm.btt.poc.transfer.AccountTransferBehavior"
actionType="behavior">
<map Injection="dataMap">
<entry key="acctFrom" value="accountNumber"></entry>
<entry key="amount" value="amount"></entry>
</map>
</SmartCollectorConfig4Transaction>
</java.util.ArrayList></codeblock><p></p><codeblock><service.xml>
Both the global rule and transaction rule should be predefined in the UDTT service configuration file, such as service.xml.
<com.ibm.btt.poc.util.GlobalDataCollectorRuleService
id="globalDataCollectorRuleService"/>
<com.ibm.btt.poc.util.TransferDataCollectorRuleService
id="transferDataCollectorRuleService"/>
</service.xml>
The rule service class should extend the class com.ibm.btt.channel.ruleprovider.java.JavaCodeRuleProviderService, which is included in the bttruleprovider.jar. The rule service class should implement the method public Map<String> checkRule(Map<String> params). This method is invoked by the smart collector handler class to check if the transaction data should be collected. In the definition of the method public Map<String> checkRule(Map<String> params), you can get the extension data that defined in the smart dynamic datamap to define the sampling rule as following code:
public Map<String> checkRule(Map<String> params) { System.out.println("TransferDataCollectorRuleService Checking the channel rule");
if(params==null){
return null;
}
String amount= (String) params.get("amount");
Double money=new Double(amount);
if (money.doubleValue() >1000.0)
params.put( "collect" , Boolean.TRUE);
else
params.put( "collect" , Boolean.FALSE);
return params; }
</String></String>
You can also get the session context data to define the sampling rule as following code:
public class IsVIPRuleService4FVT extends JavaCodeRuleProviderService {
public Map<String> checkRule(Map<String> params) {
System.out.println("IsVIPRuleService4FVT Checking the channel rule");
Map<String> result=new HashMap<String>();
Context sessCtx=(Context) params.get(ChannelConstant.SESSION_CTX);
String userId=(String)sessCtx.tryGetValueAt("userId");
boolean isVIP=false;
System.out.println("======userId="+userId);
if(userId.equals("user01")||userId.equals("user02")){
isVIP=true;
}
if (isVIP)
result.put( "collect" , Boolean.TRUE);
else
result.put( "collect" , Boolean.FALSE);
return result; }
The userId field should be the session context data which is defined in the session context definition file. It should also be set value previously as following sample code:
Context sessionCtx = ContextFactory.createContext("sessionCtx");
sessionCtx.setValueAt("userId",getContext().getValueAt("userId"));