question

Upvotes
Accepted
9 2 0 5

How to parse OMMMsg in a simple way

i run the example code ,and receive the next text:

AttribInfo
		ServiceName: ELEKTRON_DD
		ServiceId: 257
		Name: JPY=
		NameType: 1 (RIC)
	Payload: 2081 bytes
		FIELD_LIST
			FIELD_ENTRY 1/PROD_PERM: 213
			FIELD_ENTRY 2/RDNDISPLAY: 153
			FIELD_ENTRY 3/DSPLY_NAME: MDM BANK     MOW
			FIELD_ENTRY 5/TIMACT: 07:31
			FIELD_ENTRY 11/NETCHNG_1: 0.17
			FIELD_ENTRY 12/HIGH_1: 108.91
			FIELD_ENTRY 13/LOW_1: 108.68
			FIELD_ENTRY 15/CURRENCY: JPY (392)
			FIELD_ENTRY 17/ACTIV_DATE: 24 APR 2018
			FIELD_ENTRY 19/OPEN_PRC: 108.69
			FIELD_ENTRY 21/HST_CLOSE: 108.70
			FIELD_ENTRY 22/BID: 108.87
			FIELD_ENTRY 23/BID_1: 108.87
			FIELD_ENTRY 24/BID_2: 108.86
			FIELD_ENTRY 25/ASK: 108.88
			FIELD_ENTRY 26/ASK_1: 108.88
			FIELD_ENTRY 27/ASK_2: 108.89

what i want is like JSON format :

{"CURRENCY": "JPY","BID":"108.87","ASK":"108.88"}  

I only want the three Fields "CURRENCY ,BID ,ASK", but the parse way is too hard to understand. I don't know how to fix the code.

Would you please give me a simply way to parse the OMMMsg?

treprfarfa-apiOMM
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
Accepted
9.5k 10 5 7

Hello @peng.xiaoliang

RFA Java package provides the GenericOMMParser class which is used to read and initialize dictionaries and parse any OMM messages. Hence, you may modify GenericOMMParser class to get your preference fields from the response and keep them in the map. Then, you can create them as your preference format i.e. JSON. The example source code is here:

 //Add the method to GenericOMMParser.java to get CURRENCY, BID and ASK field
    public static HashMap<String,String> parseOMMData(OMMData data)
    {
    	HashMap<String,String> preferredFields = new HashMap<String,String>();
    	String valueStr;
    	if(CURRENT_DICTIONARY==null) 
	//make sure that RDMdictionary is loaded already
    		CURRENT_DICTIONARY = getDictionary(1);
        for (Iterator iter = ((OMMIterable)data).iterator(); iter.hasNext();)
        {
            OMMEntry entry = (OMMEntry)iter.next();
            if(entry.getType()==OMMTypes.FIELD_ENTRY) {
            	OMMFieldEntry fe = (OMMFieldEntry)entry;
            	FidDef fiddef = CURRENT_DICTIONARY.getFidDef(fe.getFieldId());
            	OMMData aValue = null;
            	if(fe.getFieldId() == 22 || fe.getFieldId()==25) { 
		//BID or ASK field
            		aValue=  fe.getData(fiddef.getOMMType());
            		if(!aValue.isBlank()) 
			//not blank value, get value as a String
            			valueStr = aValue.toString();
            		else
            			valueStr = ""; //otherwise, set blank value
            		preferredFields.put(fiddef.getName(), valueStr);
            	}
            	else if(fe.getFieldId() == 15)//CURRENCY which is enum type
                {
            		aValue=fe.getData(fiddef.getOMMType());
            		if(!aValue.isBlank()) 
			//not blank value, get String of the enum value 
            			valueStr=CURRENT_DICTIONARY.expandedValueFor((short)15, ((OMMEnum)aValue).getValue());  
            		else
            			valueStr = ""; //otherwise, set blank value
            		preferredFields.put(fiddef.getName(), valueStr);
                }	
            }
        }
        return preferredFields;
    }
...
//in your application at the method to process data(item events)
public void processEvent(Event event)
{
	...
	OMMItemEvent ie = (OMMItemEvent)event;
        OMMMsg respMsg = ie.getMsg();
        GenericOMMParser.parse(respMsg);
      //If it is Refresh or Update data message, get data
        if((respMsg.getMsgModelType() == RDMMsgTypes.MARKET_PRICE) && 
        		( respMsg.getMsgType() == OMMMsg.MsgType.REFRESH_RESP || respMsg.getMsgType() == OMMMsg.MsgType.UPDATE_RESP))
        {
        	//Get CURRENCY, BID and ASK field
        	HashMap<String,String> preferredFields= GenericOMMParser.parseOMMData(respMsg.getPayload());
        	//Create JSON format
        	//{"CURRENCY": "JPY","BID":"108.87","ASK":"108.88"}  
        	StringBuffer jsonDataFormatStrBuf = new StringBuffer("{");
        	if(preferredFields.containsKey("CURRENCY"))
        		jsonDataFormatStrBuf.append("\"CURRENCY\":\"" + preferredFields.get("CURRENCY")+"\",");
        	if(preferredFields.containsKey("BID")) 
        		jsonDataFormatStrBuf.append("\"BID\":\""+ preferredFields.get("BID"));
        	if(preferredFields.containsKey("ASK")) 	
        		jsonDataFormatStrBuf.append("\",\"ASK\":\"" + preferredFields.get("ASK") + "\"}");
        	if (jsonDataFormatStrBuf.toString().length() > 1) //contain at least 1 field
        		System.out.println("Json Data format is " + jsonDataFormatStrBuf.toString());	 
        } 

The example Refresh message output:

Msg Type: MsgType.REFRESH_RESP
	…
	Payload: 2060 bytes
		FIELD_LIST
			FIELD_ENTRY 1/PROD_PERM: 213
			FIELD_ENTRY 2/RDNDISPLAY: 153
			FIELD_ENTRY 3/DSPLY_NAME: BARCLAYS     LON
			FIELD_ENTRY 5/TIMACT: 09:34
			FIELD_ENTRY 11/NETCHNG_1: 0.20
			FIELD_ENTRY 12/HIGH_1: 109.26
			FIELD_ENTRY 13/LOW_1: 108.80
			FIELD_ENTRY 15/CURRENCY: JPY (392)
			FIELD_ENTRY 17/ACTIV_DATE: 25 APR 2018
			FIELD_ENTRY 19/OPEN_PRC: 108.80
			FIELD_ENTRY 21/HST_CLOSE: 108.81
			FIELD_ENTRY 22/BID: 109.01
			FIELD_ENTRY 23/BID_1: 109.02
			FIELD_ENTRY 24/BID_2: 109.02
			FIELD_ENTRY 25/ASK: 109.04
			FIELD_ENTRY 26/ASK_1: 109.04
			FIELD_ENTRY 27/ASK_2: 109.04
…
Json Data format is {"CURRENCY":"JPY","BID":"109.01","ASK":"109.04"}

The example Update message output:

	Msg Type: MsgType.UPDATE_RESP
	…
	Payload: 212 bytes
		FIELD_LIST
			FIELD_ENTRY 22/BID: 109.02
			FIELD_ENTRY 393/PRIMACT_1: 109.02
			FIELD_ENTRY 25/ASK: 109.05
			FIELD_ENTRY 275/SEC_ACT_1: 109.05
			…
Json Data format is {"BID":"109.02","ASK":"109.05"}

Note: an Update message contains only change fields so CURRENCY may not in the Update message.

If you would like to get only your preference fields(CURRENCY, BID and ASK) from the server which is ADS(one component of TREP), you can use Dynamic View feature which allows ADS sends only fields specified in the request message to your application. For more details and example application, please refer to How to request particular fields of an item (RIC) via RFA API

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

there is an problem, if the Msg Type is UPDATE_RESP, I can't fetch the CURRENCY field at the same time .

Is there any way to ensure the CURRENCY field existed in every notice.

Hello @peng.xiaoliang

As far as I know there is no way. Hence, you need to keep CURRENCY received from the Refresh message e.g. set in it a class/instance 's variable.

Before creating a new JSON data format containing CURRENCY field

  • get the CURRENCY kept in the class/instance's variable if the update message does not contain CURRENCY.

or

  • update the class/instance's variable with the new CURRENCY received from the update message.
Upvotes
120 3 3 7

I think the latest TREP software supports JSON format, so you get the market data updates already in JSON. But I have not still tested it.

In that case you do not use RFA but another mechanism to connect to TREP, send requests and get JSON data.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Click below to post an Idea Post Idea