Java tutorial
/* * Copyright 2012. Bloomberg Finance L.P. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: The above * copyright notice and this permission notice shall be included in all copies * or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ import java.io.BufferedReader; import org.json.JSONObject; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Locale; import java.util.concurrent.CountDownLatch; import com.bloomberglp.blpapi.CorrelationID; import com.bloomberglp.blpapi.Element; import com.bloomberglp.blpapi.Event; import com.bloomberglp.blpapi.Message; import com.bloomberglp.blpapi.MessageIterator; import com.bloomberglp.blpapi.Name; import com.bloomberglp.blpapi.Request; import com.bloomberglp.blpapi.Service; import com.bloomberglp.blpapi.Session; import com.bloomberglp.blpapi.SessionOptions; public class SimpleRefDataExample { protected static final Name SECURITY_DATA = new Name("securityData"); private static final Name SECURITY = new Name("security"); protected static final Name FIELD_DATA = new Name("fieldData"); protected static final Name FIELD_EXCEPTIONS = new Name("fieldExceptions"); protected static final Name FIELD_ID = new Name("fieldId"); protected static final Name ERROR_INFO = new Name("errorInfo"); protected CorrelationID d_cid; static JSONObject obj = new JSONObject(); public static void main(String[] args) throws Exception { SimpleRefDataExample example = new SimpleRefDataExample(); example.run(args); //System.out.println (tickers.toArray(new String[tickers.size()])); //System.out.println (obj); //System.out.println("Press ENTER to quit"); //System.in.read(); } protected void run(String[] args) throws Exception { String serverHost = "10.8.8.1"; int serverPort = 8194; SessionOptions sessionOptions = new SessionOptions(); sessionOptions.setServerHost(serverHost); sessionOptions.setServerPort(serverPort); Session session = new Session(sessionOptions); //System.out.println("Connecting to " + serverHost + ":" + serverPort); if (!session.start()) { System.err.println("Failed to start session."); return; } //System.out.println("Connected successfully."); if (!session.openService("//blp/refdata")) { System.err.println("Failed to open //blp/refdata"); return; } Service refDataService = session.getService("//blp/refdata"); Request request = refDataService.createRequest("ReferenceDataRequest"); // append securities to request Element securities = request.getElement("securities"); Element fields = request.getElement("fields"); //fields.appendValue("SHORT_NAME"); fields.appendValue("COUNTRY_FULL_NAME"); //fields.appendValue("NAME"); //fields.appendValue("SECURITY_DES"); fields.appendValue("PX_LAST"); //fields.appendValue("SECURITY_NAME_REALTIME"); // System.out.println("Sending Request: " + request); d_cid = session.sendRequest(request, null); while (true) { Event event = session.nextEvent(); MessageIterator msgIter = event.messageIterator(); while (msgIter.hasNext()) { Message msg = msgIter.next(); if (msg.correlationID() == d_cid) { processMessage(msg); } } if (event.eventType() == Event.EventType.RESPONSE) { break; } } } protected void processMessage(Message msg) throws Exception { HashMap<String, String> map = new HashMap<String, String>(); String[] locales = Locale.getISOCountries(); for (String countryCode : locales) { Locale loc = new Locale("", countryCode); map.put(loc.getDisplayCountry().toLowerCase(), loc.getCountry()); } //System.out.println (map); // Gson gson = null; //StringBuffer sb = new StringBuffer(); String s = null; Element securityDataArray = msg.getElement(SECURITY_DATA); int numSecurities = securityDataArray.numValues(); for (int i = 0; i < numSecurities; ++i) { Element securityData = securityDataArray.getValueAsElement(i); //System.out.println(securityData.getElementAsString(SECURITY)); Element fieldData = securityData.getElement(FIELD_DATA); for (int j = 0; j < fieldData.numElements(); ++j) { Element field = fieldData.getElement(j); if (field.isNull()) { System.out.println(field.name() + " is NULL."); } else { String str = field.getValueAsString(); if (j % 2 == 0) s = str; else { String countryCode = map.get(s.toLowerCase()); if (countryCode != null) { // System.out.println(str + " " + countryCode); //if (map.containsKey(s)) obj.put(countryCode, Double.parseDouble(str)); } } //field.getValueAsString().put(obj); //obj.put(str[0], str[1]); } } Element fieldExceptionArray = securityData.getElement(FIELD_EXCEPTIONS); for (int k = 0; k < fieldExceptionArray.numValues(); ++k) { Element fieldException = fieldExceptionArray.getValueAsElement(k); /* System.out.println( fieldException.getElement(ERROR_INFO).getElementAsString("category") + ": " + fieldException.getElementAsString(FIELD_ID));*/ } } } }