|
| 1 | +/* Copyright 2018. Bloomberg Finance L.P. |
| 2 | + * |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | + * of this software and associated documentation files (the "Software"), to |
| 5 | + * deal in the Software without restriction, including without limitation the |
| 6 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 8 | + * furnished to do so, subject to the following conditions: The above |
| 9 | + * copyright notice and this permission notice shall be included in all copies |
| 10 | + * or substantial portions of the Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 17 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 18 | + * IN THE SOFTWARE. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.bloomberg.emsx.samples; |
| 22 | + |
| 23 | +import com.bloomberglp.blpapi.Event; |
| 24 | +import com.bloomberglp.blpapi.EventHandler; |
| 25 | +import com.bloomberglp.blpapi.Message; |
| 26 | +import com.bloomberglp.blpapi.MessageIterator; |
| 27 | +import com.bloomberglp.blpapi.Name; |
| 28 | +import com.bloomberglp.blpapi.Session; |
| 29 | +import com.bloomberglp.blpapi.SessionOptions; |
| 30 | +import com.bloomberglp.blpapi.Request; |
| 31 | +import com.bloomberglp.blpapi.Service; |
| 32 | +import com.bloomberglp.blpapi.CorrelationID; |
| 33 | + |
| 34 | + |
| 35 | +public class CreateBasket { |
| 36 | + |
| 37 | + private static final Name SESSION_STARTED = new Name("SessionStarted"); |
| 38 | + private static final Name SESSION_STARTUP_FAILURE = new Name("SessionStartupFailure"); |
| 39 | + private static final Name SERVICE_OPENED = new Name("ServiceOpened"); |
| 40 | + private static final Name SERVICE_OPEN_FAILURE = new Name("ServiceOpenFailure"); |
| 41 | + |
| 42 | + private static final Name ERROR_INFO = new Name("ErrorInfo"); |
| 43 | + private static final Name CREATE_BASKET = new Name("CreateBasket"); |
| 44 | + |
| 45 | + private String d_service; |
| 46 | + private String d_host; |
| 47 | + private int d_port; |
| 48 | + |
| 49 | + private CorrelationID requestID; |
| 50 | + |
| 51 | + private static boolean quit=false; |
| 52 | + |
| 53 | + public static void main(String[] args) throws java.lang.Exception |
| 54 | + { |
| 55 | + System.out.println("Bloomberg - EMSX API Example - CreateBasket\n"); |
| 56 | + |
| 57 | + CreateBasket example = new CreateBasket(); |
| 58 | + example.run(args); |
| 59 | + |
| 60 | + while(!quit) { |
| 61 | + Thread.sleep(10); |
| 62 | + }; |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + public CreateBasket() |
| 67 | + { |
| 68 | + |
| 69 | + // Define the service required, in this case the EMSX beta service, |
| 70 | + // and the values to be used by the SessionOptions object |
| 71 | + // to identify IP/port for the back-end process. |
| 72 | + |
| 73 | + d_service = "//blp/emapisvc_beta"; |
| 74 | + //d_service = "//blp/emapisvc"; |
| 75 | + d_host = "localhost"; |
| 76 | + d_port = 8194; |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + private void run(String[] args) throws Exception |
| 81 | + { |
| 82 | + |
| 83 | + SessionOptions d_sessionOptions = new SessionOptions(); |
| 84 | + d_sessionOptions.setServerHost(d_host); |
| 85 | + d_sessionOptions.setServerPort(d_port); |
| 86 | + |
| 87 | + Session session = new Session(d_sessionOptions, new EMSXEventHandler()); |
| 88 | + |
| 89 | + session.startAsync(); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + class EMSXEventHandler implements EventHandler |
| 94 | + { |
| 95 | + public void processEvent(Event event, Session session) |
| 96 | + { |
| 97 | + try { |
| 98 | + switch (event.eventType().intValue()) |
| 99 | + { |
| 100 | + case Event.EventType.Constants.SESSION_STATUS: |
| 101 | + processSessionEvent(event, session); |
| 102 | + break; |
| 103 | + case Event.EventType.Constants.SERVICE_STATUS: |
| 104 | + processServiceEvent(event, session); |
| 105 | + break; |
| 106 | + case Event.EventType.Constants.RESPONSE: |
| 107 | + processResponseEvent(event, session); |
| 108 | + break; |
| 109 | + default: |
| 110 | + processMiscEvents(event, session); |
| 111 | + break; |
| 112 | + } |
| 113 | + } catch (Exception e) { |
| 114 | + e.printStackTrace(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private boolean processSessionEvent(Event event, Session session) throws Exception { |
| 119 | + |
| 120 | + System.out.println("Processing " + event.eventType().toString()); |
| 121 | + |
| 122 | + MessageIterator msgIter = event.messageIterator(); |
| 123 | + |
| 124 | + while (msgIter.hasNext()) { |
| 125 | + |
| 126 | + Message msg = msgIter.next(); |
| 127 | + |
| 128 | + if(msg.messageType().equals(SESSION_STARTED)) { |
| 129 | + System.out.println("Session started..."); |
| 130 | + session.openServiceAsync(d_service); |
| 131 | + } else if(msg.messageType().equals(SESSION_STARTUP_FAILURE)) { |
| 132 | + System.err.println("Error: Session startup failed"); |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + private boolean processServiceEvent(Event event, Session session) { |
| 140 | + |
| 141 | + System.out.println("Processing " + event.eventType().toString()); |
| 142 | + |
| 143 | + MessageIterator msgIter = event.messageIterator(); |
| 144 | + |
| 145 | + while (msgIter.hasNext()) { |
| 146 | + |
| 147 | + Message msg = msgIter.next(); |
| 148 | + |
| 149 | + if(msg.messageType().equals(SERVICE_OPENED)) { |
| 150 | + |
| 151 | + System.out.println("Service opened..."); |
| 152 | + |
| 153 | + Service service = session.getService(d_service); |
| 154 | + |
| 155 | + Request request = service.createRequest("CreateBasket"); |
| 156 | + |
| 157 | + // define the basket name |
| 158 | + request.set("EMSX_BASKET_NAME", "TestMSFTBasket"); |
| 159 | + |
| 160 | + // add any number of orders |
| 161 | + request.append("EMSX_SEQUENCE", 4317236); |
| 162 | + request.append("EMSX_SEQUENCE", 4317238); |
| 163 | + //request.append("EMSX_SEQUENCE", 4317239); |
| 164 | + |
| 165 | + System.out.println("Request: " + request.toString()); |
| 166 | + |
| 167 | + requestID = new CorrelationID(); |
| 168 | + |
| 169 | + // Submit the request |
| 170 | + try { |
| 171 | + session.sendRequest(request, requestID); |
| 172 | + } catch (Exception ex) { |
| 173 | + System.err.println("Failed to send the request"); |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + } else if(msg.messageType().equals(SERVICE_OPEN_FAILURE)) { |
| 178 | + System.err.println("Error: Service failed to open"); |
| 179 | + return false; |
| 180 | + } |
| 181 | + } |
| 182 | + return true; |
| 183 | + } |
| 184 | + |
| 185 | + private boolean processResponseEvent(Event event, Session session) throws Exception |
| 186 | + { |
| 187 | + System.out.println("Received Event: " + event.eventType().toString()); |
| 188 | + |
| 189 | + MessageIterator msgIter = event.messageIterator(); |
| 190 | + |
| 191 | + while(msgIter.hasNext()) |
| 192 | + { |
| 193 | + Message msg = msgIter.next(); |
| 194 | + |
| 195 | + System.out.println("MESSAGE: " + msg.toString()); |
| 196 | + System.out.println("CORRELATION ID: " + msg.correlationID()); |
| 197 | + |
| 198 | + if(event.eventType()==Event.EventType.RESPONSE && msg.correlationID()==requestID) { |
| 199 | + |
| 200 | + System.out.println("Message Type: " + msg.messageType()); |
| 201 | + if(msg.messageType().equals(ERROR_INFO)) { |
| 202 | + Integer errorCode = msg.getElementAsInt32("ERROR_CODE"); |
| 203 | + String errorMessage = msg.getElementAsString("ERROR_MESSAGE"); |
| 204 | + System.out.println("ERROR CODE: " + errorCode + "\tERROR MESSAGE: " + errorMessage); |
| 205 | + } else if(msg.messageType().equals(CREATE_BASKET)) { |
| 206 | + Integer emsx_sequence = msg.getElementAsInt32("EMSX_SEQUENCE"); |
| 207 | + String message = msg.getElementAsString("MESSAGE"); |
| 208 | + System.out.println("EMSX_SEQUENCE: " + emsx_sequence + "\tMESSAGE: " + message); |
| 209 | + } |
| 210 | + |
| 211 | + quit=true; |
| 212 | + session.stop(); |
| 213 | + } |
| 214 | + } |
| 215 | + return true; |
| 216 | + } |
| 217 | + |
| 218 | + private boolean processMiscEvents(Event event, Session session) throws Exception |
| 219 | + { |
| 220 | + System.out.println("Processing " + event.eventType().toString()); |
| 221 | + MessageIterator msgIter = event.messageIterator(); |
| 222 | + while (msgIter.hasNext()) { |
| 223 | + Message msg = msgIter.next(); |
| 224 | + System.out.println("MESSAGE: " + msg); |
| 225 | + } |
| 226 | + return true; |
| 227 | + } |
| 228 | + |
| 229 | + } |
| 230 | + |
| 231 | +} |
| 232 | + |
0 commit comments