Thursday, December 13, 2012

blpapi Client -- Java Example

In a previous post, we have shown how to implement blpapi and create an application that listens on port 8195. We now try to show how to utilize that app.

The client-server communication is via TCP/IP and exchange information using XML.

The server expects request schema:

<anybeginningtag>
     <Instrument>
     </Instrument>
     <Fields>
     </Fields>
     <Overrides>
          <Override></Override>
     </Overrides>
</anybeginningtag>

In return, expect response schema:

<BBCommAdapterResponse>
     <FLDS1></FLDS1>
     <FLDS2></FLDS2>
</BBCommAdapterResponse>

FLDS1, FLDS2 are the Bloomberg FLDS field names you have requested for in <Fields></Fields>.

Java Example

Create a model for which you will be holding values.

package scratch;
public class SampleModel {
 private String accDays;
 private String aiCurrent;
 private String price;
 private String yield;

 public String getAccDays() {
  return accDays;
 }
 public void setAccDays(String accDays) {
  this.accDays = accDays;
 }
 public String getAiCurrent() {
  return aiCurrent;
 }
 public void setAiCurrent(String aiCurrent) {
  this.aiCurrent = aiCurrent;
 }
 public String getPrice() {
  return price;
 }
 public void setPrice(String price) {
  this.price = price;
 }
 public String getYield() {
  return yield;
 }
 public void setYield(String yield) {
  this.yield = yield;
 }

 public void printContents() {
  StringBuilder sb = new StringBuilder();
  sb.append("accDays: " + accDays + "\t ");
  sb.append("aiCurrent: " + aiCurrent + "\t ");
  sb.append("price: " + price + "\t ");
  sb.append("yield: " + yield);
  System.out.println(sb.toString());
 }
}


Next, create the client.

package scratch;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;


public class ClientSocketProcessor extends DefaultHandler {

 private final int hostPort = 8195;
 private Socket connection;

 private SampleModel model;
 private String tmpValue;

 @Override
 public void startElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException {

  // the response from BBCommAdapter is enclosed with Element tag
  // <BBCommAdapterResponse></BBCommAdapterResponse>
  if (qName.equalsIgnoreCase("BBCommAdapterResponse")) {
   model = new SampleModel();
  }

 }
 @Override
 public void endElement(String uri, String localName, String qName)
   throws SAXException {
  // depending on what your model is
  // the Element tags are Bloomberg FLDS field names

  if (qName.equalsIgnoreCase("DAYS_ACC")) {
   model.setAccDays(tmpValue);
  }

  if (qName.equalsIgnoreCase("INT_ACC")) {
   model.setAiCurrent(tmpValue);
  }

  if (qName.equalsIgnoreCase("PX_ASK")) {
   model.setPrice(tmpValue);
  }

  if (qName.equalsIgnoreCase("YLD_CNV_ASK")) {
   model.setYield(tmpValue);
  }
 }

 @Override
 public void characters(char ch[], int start, int length)
   throws SAXException {
  tmpValue = new String(ch, start, length);
 }

 private void parseXMLAndSetValues(String xmlString) {
  System.out.println("XML: " + xmlString);
  InputSource inputSource = new InputSource();
  inputSource.setCharacterStream(new StringReader(xmlString));

  SAXParserFactory factory = SAXParserFactory.newInstance();
  try {
   SAXParser parser = factory.newSAXParser();
   XMLReader reader = parser.getXMLReader();
   reader.setContentHandler(this);
   reader.parse(inputSource);
 
  } catch (ParserConfigurationException e) {
   System.out.println("ParserConfigurationException encountered in ClientSocketProcessor::parseXMLAndSetValues.");
  } catch (SAXException e) {
   System.out.println("SAXException encountered in ClientSocketProcessor::parseXMLAndSetValues.");
  } catch (IOException e) {
   System.out.println("IOException encountered in ClientSocketProcessor::parseXMLAndSetValues.");
  }
 }

 private void transformString(BufferedReader socketReader) {
  StringBuilder sb = new StringBuilder();
  String response = null;
  try {
   while((response = socketReader.readLine()) != null) {
    sb.append(response);
   }
   // remember to disconnect
   socketReader.close();
  } catch (IOException e) {
   System.out.println("IOException encountered in ClientSocketProcessor::transformString.");
  }

  if (sb.toString().length() == 0) {
   return;
  } else {
   parseXMLAndSetValues(sb.toString());
  }
 }

 private String createSimpleRequest(String instrument) {
  StringBuilder sb = new StringBuilder();

  sb.append("<SimpleRequestTag>");

  sb.append("<Instrument>");
  sb.append(instrument);
  sb.append("</Instrument>");

  sb.append("<Fields>");
  sb.append("DAYS_ACC,INT_ACC");
  sb.append("</Fields>");
  sb.append("</SimpleRequestTag>");

  return sb.toString();
 }

 private String createRequestWithOverride1(String instrument, String settlementDate) {
  StringBuilder sb = new StringBuilder();

  sb.append("<RequestWithOverrideTag>");

  sb.append("<Instrument>");
  sb.append(instrument);
  sb.append("</Instrument>");

  sb.append("<Fields>");
  sb.append("DAYS_ACC,INT_ACC");
  sb.append("</Fields>");

  sb.append("<Overrides>");
  sb.append("<Override>");
  sb.append("SETTLE_DT," + settlementDate);
  sb.append("</Override>");
  sb.append("</Overrides>");

  sb.append("</RequestWithOverrideTag>");

  return sb.toString();
 }

 private String createRequestWithOverride2(String instrument, String settlementDate, String price) {
  StringBuilder sb = new StringBuilder();

  sb.append("<RequestWithOverrideTag>");

  sb.append("<Instrument>");
  sb.append(instrument);
  sb.append("</Instrument>");

  sb.append("<Fields>");
  sb.append("YLD_CNV_ASK");
  sb.append("</Fields>");

  sb.append("<Overrides>");

  sb.append("<Override>");
  sb.append("PX_ASK," + price);
  sb.append("</Override>");

  sb.append("<Override>");
  sb.append("SETTLE_DT," + settlementDate);
  sb.append("</Override>");

  sb.append("</Overrides>");

  sb.append("</RequestWithOverrideTag>");

  return sb.toString();
 }

 private String createRequestWithOverride3(String instrument, String settlementDate, String yield) {
  StringBuilder sb = new StringBuilder();

  sb.append("<RequestWithOverrideTag>");

  sb.append("<Instrument>");
  sb.append(instrument);
  sb.append("</Instrument>");

  sb.append("<Fields>");
  sb.append("PX_ASK");
  sb.append("</Fields>");

  sb.append("<Overrides>");

  sb.append("<Override>");
  sb.append("YLD_CNV_ASK," + yield);
  sb.append("</Override>");

  sb.append("<Override>");
  sb.append("SETTLE_DT," + settlementDate);
  sb.append("</Override>");

  sb.append("</Overrides>");

  sb.append("</RequestWithOverrideTag>");

  return sb.toString();
 }

 private SampleModel doSimpleRequest(String instrument) {
  String requestString = createSimpleRequest(instrument);

  try {
   InetAddress host = InetAddress.getLocalHost();
   String hostIp = host.getHostAddress();
   connection = new Socket(hostIp, hostPort);
   connection.setTcpNoDelay(true);
 
   // write text to stream
   BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
   socketWriter.write(requestString);
   socketWriter.flush();
 
   // read text from stream
   BufferedReader socketReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
   transformString(socketReader);
 
  } catch (UnknownHostException e) {
   System.out.println("UnknownHostException encountered in ClientSocketProcessor::doSimpleRequest.");
  } catch (IOException e) {
   System.out.println("IOException encountered in ClientSocketProcessor::doSimpleRequest.");
  }
  return model;
 }

 private SampleModel doRequestWithOverride1(String instrument, String settlementDate) {
  String requestString = createRequestWithOverride1(instrument, settlementDate);
  try {
   InetAddress host = InetAddress.getLocalHost();
   String hostIp = host.getHostAddress();
   connection = new Socket(hostIp, hostPort);
   connection.setTcpNoDelay(true);
 
   // write text to stream
   BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
   socketWriter.write(requestString);
   socketWriter.flush();
 
   // read text from stream
   BufferedReader socketReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
   transformString(socketReader);
 
  } catch (UnknownHostException e) {
   System.out.println("UnknownHostException encountered in ClientSocketProcessor::doRequestWithOverride1.");
  } catch (IOException e) {
   System.out.println("IOException encountered in ClientSocketProcessor::doRequestWithOverride1.");
  }
  return model;
 }

 private SampleModel doRequestWithOverride2(String instrument, String settlementDate, String price) {
  String requestString = createRequestWithOverride2(instrument, settlementDate, price);
  try {
   InetAddress host = InetAddress.getLocalHost();
   String hostIp = host.getHostAddress();
   connection = new Socket(hostIp, hostPort);
   connection.setTcpNoDelay(true);
 
   // write text to stream
   BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
   socketWriter.write(requestString);
   socketWriter.flush();
 
   // read text from stream
   BufferedReader socketReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
   transformString(socketReader);
 
  } catch (UnknownHostException e) {
   System.out.println("UnknownHostException encountered in ClientSocketProcessor::doRequestWithOverride2.");
  } catch (IOException e) {
   System.out.println("IOException encountered in ClientSocketProcessor::doRequestWithOverride2.");
  }
  return model;
 }

 private SampleModel doRequestWithOverride3(String instrument, String settlementDate, String yield) {
  String requestString = createRequestWithOverride3(instrument, settlementDate, yield);
  try {
   InetAddress host = InetAddress.getLocalHost();
   String hostIp = host.getHostAddress();
   connection = new Socket(hostIp, hostPort);
   connection.setTcpNoDelay(true);
 
   // write text to stream
   BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
   socketWriter.write(requestString);
   socketWriter.flush();
 
   // read text from stream
   BufferedReader socketReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
   transformString(socketReader);
 
  } catch (UnknownHostException e) {
   System.out.println("UnknownHostException encountered in ClientSocketProcessor::doRequestWithOverride3.");
  } catch (IOException e) {
   System.out.println("IOException encountered in ClientSocketProcessor::doRequestWithOverride3.");
  }
  return model;
 }

 public static void main(String[] args) {
  ClientSocketProcessor processor = new ClientSocketProcessor();
  SampleModel model1 = processor.doSimpleRequest("IDG000010406 Corp");
  SampleModel model2 = processor.doRequestWithOverride1("JPM 3.25 09/23/22 Corp", "20121221");
  SampleModel model3 = processor.doRequestWithOverride2("EJ3726316 Corp", "20121228", "99.23");
  SampleModel model4 = processor.doRequestWithOverride3("RPGB 8 07/31 Corp", "20121228", "7.12345");
  model1.printContents();
  model2.printContents();
  model3.printContents();
  model4.printContents();
 }
}


When this code is run, it should give below results:

XML: <BBCommAdapterResponse><DAYS_ACC>33</DAYS_ACC><INT_ACC>0.6039</INT_ACC></BBCommAdapterResponse>
XML: <BBCommAdapterResponse><DAYS_ACC>87</DAYS_ACC><INT_ACC>0.78541667</INT_ACC></BBCommAdapterResponse>
XML: <BBCommAdapterResponse><YLD_CNV_ASK>3.3429092</YLD_CNV_ASK></BBCommAdapterResponse>
XML: <BBCommAdapterResponse><PX_ASK>108.940976195453</PX_ASK></BBCommAdapterResponse>
accDays: 33  aiCurrent: 0.6039  price: null  yield: null
accDays: 87  aiCurrent: 0.78541667  price: null  yield: null
accDays: null  aiCurrent: null  price: null  yield: 3.3429092
accDays: null  aiCurrent: null  price: 108.940976195453  yield: null

No comments:

Post a Comment