LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only

Java node + Bloomberg api

  • 1.  Java node + Bloomberg api

    Employee
    Posted 02-11-2014 03:17

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dialpemo

    Hi,

    I am trying to create a java node that would allow me to extract data from Bloomberg. I currently haven't got an account with Bloomberg however I have found a Java api emulator online (I have the .jar file but I couldn't attach it here I could email it if necessary). I have tested the emulator using NetBenas IDE, and I have been trying to replicate this using the Java node in Lavastorm.
    I have been transferring the code in small chunks to Lavastorm so that I can check that it is working, and also running the same code in NetBeans to compare if any errors are found.

    Please found attach the full code as well as the java node that I am running and to the point where I have encountered errors.
    I have encountered some problems I keep getting the following error :

    2014-02-11 09:58:53.212; ERROR: Error compiling the node.
    id: 6 chain: 0 group: 0
    javaExceptionDetail: com.lavastorm.tools.build.compiler.CompileExceptio n: 'Error compiling source unit.
    Error occurred attempting to compile source with entry type (com.lavastorm.tools.build.java.MultiJavaBuiltUnit ).
    Compilation diagnostics:
    bootstrap class path not set in conjunction with -source 1.6
    unreported exception java.lang.Exception; must be caught or declared to be thrown
    ';
    stack trace:
    com.lavastorm.tools.build.cache.CachingBuilder.bui ldAndLoad(CachingBuilder.java@139)
    com.lavastorm.tools.build.cache.CachingBuilder.bui ldAndLoad(CachingBuilder.java@80)
    com.lavastorm.tools.build.cache.CachingBuilder.bui ldAndLoad(CachingBuilder.java@75)
    com.lavastorm.brain.execution.java.node.NodeWrappe r.buildJavaNode(NodeWrapper.java@1400)
    com.lavastorm.brain.execution.java.node.NodeWrappe r.load(NodeWrapper.java@1355)
    com.lavastorm.brain.execution.java.node.NodeWrappe r.run(NodeWrapper.java@752)
    com.lavastorm.brain.execution.java.node.NodeWrappe r.main(NodeWrapper.java@1466)
    javaDetail: com.lavastorm.brain.execution.java.node.NodeWrappe r.buildJavaNode(NodeWrapper.java@1403)

    2014-02-11 09:58:53.228; ERROR: Node failed.
    id: 7 chain: 6 group: 0
    javaDetail: com.lavastorm.brain.execution.java.node.NodeWrappe r.run(NodeWrapper.java@799)

    Thank you for your time,

    Alejandro
    Attachments:
    bloomberg.brg
    bloomberg.brp
    RunHistoricalDataRequest.txt


  • 2.  RE: Java node + Bloomberg api

    Employee
    Posted 02-11-2014 11:14

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Tim Meagher

    Hey,

    Without the jar it's difficult to know exactly where the problem is occurring, but something in the 3rd party jar you are using is declared to throw an Exception.
    However, you are't catching the Exception, and the processAll method is declared only to throw a NodeFailedException.

    You should check the API you are using and make sure that you are correctly handling any declared exceptions that the API is throwing - and then, likely logging the error, and calling:
    throw fail();
    Which will ensure that in the case of such an exception, the node will have the information logged correctly, and will then fail.

    Further information on how to log errors and how the NodeFailedException (and throw fail()) works can be found in the Java Node Getting Started Guide in the docs directory of your installation.
    If you open in the docs/laeJavaApi/index.html in a browser you will also have access to the Java node API, which goes through all of the details of the various logging methods available.

    Regards,
    Tim.


  • 3.  RE: Java node + Bloomberg api

    Employee
    Posted 02-12-2014 07:46

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dialpemo

    ok I see I am not very confident in creating logs though. Going back to the Jar file, to import it as a Library the way I am using is that correct, I mean by referencing it from the class path, or which method could I used in order to use this jar file as a Library.


  • 4.  RE: Java node + Bloomberg api

    Employee
    Posted 02-12-2014 16:45

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Tim Meagher

    Hey,

    Adding the jar to the "Classpaths" parameter is the correct method, as outlined in section 9.2 of the Java Node Getting Started Guide "Classpath Modifications".

    Regards,
    Tim.


  • 5.  RE: Java node + Bloomberg api

    Employee
    Posted 02-17-2014 03:21

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dialpemo

    Dear Tim,

    I have manage to find the code that function is calling however, I cannot see why is it trowing an exception in Lavastorm and not in Netbeans.
    Thank you for your help so far
    The line in question is:

    Service service = session.getService("//blp/refdata");


    Which goes and checks in the �session� part of the bemu package as follow:

    public Service getService(String uri) throws Exception
    {
    if (uri.equals("//blp/refdata"))
    {
    this._sessionUri = SessionUriType.refData;
    return new ServiceRefData();
    }
    else
    return null;
    }

    the same uri was used in the if statements and if was fine.

    Please find attach an up to date version of my node and a copy of the Session code.
    Attachments:
    Session.txt
    apiv2.brg


  • 6.  RE: Java node + Bloomberg api

    Employee
    Posted 02-17-2014 04:18

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Tim Meagher

    From what I am seeing in the "summing_with_loop_and_writing_two_outputs" node, you are using code:

        Service service = session.getService("//blp/refdata");
    getService defines that it can throw Exceptions.
    It doesn't matter whether or not it actually throws them, your code needs to either handle them in a try/catch block, or to declare to throw them itself.

    Since you are doing this within the processAll method, and the processAll method can only throw NodeFailedExceptions by it's inheritence contract, you will need to go down the first path, and handle these within a try/catch block.
    It looks like you've done something similar in the setup method, but then commented it out.

    Regards,
    Tim.


  • 7.  RE: Java node + Bloomberg api

    Employee
    Posted 02-17-2014 04:56

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dialpemo

    Tim how would I use the other method as the end result is to grab data, to then be process and analyse within Lavastorm?

    Thank you for your help so far Tim.


  • 8.  RE: Java node + Bloomberg api

    Employee
    Posted 02-17-2014 05:17

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Tim Meagher

    Which other method are you referring to?

    I imagine what you want to do is to connect to the service, run some sort of query against the service, and then write the data to the node's output.
    It looks like so far you've got the code to setup the session and get the service, but not actually do anything with the service.
    I don't know anything about the 3rd party API you are trying to use, but I would imagine what you want to do now is to run a query against that service.
    I imagine that returns some sort of result - either an object that you can then inspect & extract data to write to the node's output, or some meta-information, from which you would need to run subsequent queries to get the data elements.

    All of this, however, is really something you'll need to figure out with the 3rd party API you are using.
    Once you have objects back from the API though, all you would need to do is take those and assign them to fields on your output (assuming you've setup your output correctly), then write the record.

    The mechanisms for setting up output metadata, creating records, setting fields on records, and writing those records is all pretty well outlined in our node API & getting started doc.

    It sounds like you might be having problems with the bloomberg API side of things, and with that, I think you've probably got to look for answers on how to do things with whoever provides that API.

    Tim.


  • 9.  RE: Java node + Bloomberg api

    Employee
    Posted 02-17-2014 06:52

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dialpemo

    So just to confirm, given that the code on the jar file has a "throw" as part of the code we must use the try and catch even if this is not throwing an error.
    Tim I have tried to use the example of logging an error, I used the following code
    catch(Exception e) { logger().error("Error value not found ("+e+") as a numeric value");
    throw fail(); }

    and I didn't get a fail that this means that there wasn't and error? and that I could write the rest of the code in the try and catch?


  • 10.  RE: Java node + Bloomberg api

    Employee
    Posted 02-17-2014 07:19

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Tim Meagher

    Hi,

    So just to confirm, given that the code on the jar file has a "throw" as part of the code we must use the try and catch even if this is not throwing an error. 
    
    Yes, assuming that the exception is anything which extends Exception, but isn't a RunnableException, this is true.
    Note that this has nothing to do with the Lavastorm Java Node, it is simply required by the Java language.
    For further reading, you should probably look at this page as a start:
    http://docs.oracle.com/javase/tutori...OrDeclare.html

    and I didn't get a fail that this means that there wasn't and error? and that I could write the rest of the code in the try and catch?
    
    This is also correct and a perfectly acceptable way to go, and probably the quickest way to get something working.

    In general, though, you probably want to be as specific as possible about the type of error that has occurred and provide as much information back to the person running the node so that they know why the error occurred and how to resolve it.

    For this reason, it is often better to either limit the scope of the try/catch block, or handle the different exception types differently.
    Since the API you are dealing with just appears to throw generic "Exception"s, handling different exceptions types differently isn't really an option.

    Therefore, if you put a try/catch block around everything, you won't be able to easily inform the person running the node which part of the node failed - without resorting to just the method calls and the lines in the stack trace.

    Therefore, it's probably better to do something whereby you catch the exceptions for each of the API calls separately, and can then provide more information.

    For instance, in your case, you could have something like (assuming that the SessionOptions methods don't throw exceptions) :

    SessionOptions options = new SessionOptions();
    options.setServerHost("127.0.0.1");
    options.setServerPort(8194);
    
    
    Session session = new Session(options);
    
    
    if (!session.start()) {
        //Assuming that not starting the session will cause other problems later on,
        //can log this and fail at this point
        logger().error("Error occurred starting the session.");
        throw fail();
    }
    
    
    if (!session.openService("//blp/refdata")) {
        //Assuming that not being able to open the service will cause other problems later on,
        //can log this and fail the node at this point
        logger().error("Unable to open the service: //blp/refdata");
        throw fail();
    }
    
    
    Service service = null;
    
    
    try {
        service = session.getService("//blp/refdata");
    }
    catch (Exception e) {
        logger().error(e, "An error occurred attempting to get the service: //blp/refdata.
    Error:"+e.getMessage());
        throw fail();
    }
    
    
    //Now we have the service, and can work with it
    //I don't know what you want to do here, so this is just dummy code
    //I don't know what things will return either, so that's just dummy code too
    try {
        SomeResult someResult = service.doSomething();
        
        processSomeResult(someResult);
    }
    catch (Exception e) {
        //Don't know what error could occur here, but I'll assume there is some error that can happen.
        //In which case, the exception handling for doing whatever operation it is has been 
        //separated from the exception handling for getting the service, providing better error messages back to the user
        //of the node.
        logger().error(e, "An error occurred while doing something.
    Error: "+e.getMessage());
        throw fail();
    }


  • 11.  RE: Java node + Bloomberg api

    Employee
    Posted 02-19-2014 03:57

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dialpemo

    apiv2.1.brgDear Tim,

    Thank you for all your advice, I have now managed to pull data from the simulator.
    The problem I got now is that I want to make sure that the user doesn't have a direct contact to the java node, but instead it can specified the parameters from a static data input.
    I have manage to pass data through however I am having trouble with one of the variables, I am trying to create a form of logical test to check what the user wants to request,
    when creating the if statement it doesn't seem to be working. the line number is 107 to 110
    the code is if (input_request == "bid") {request.append("fields", "BID"); } and I have other instance as well for the other cases; What I don't understand is what I am doing wrong as it assumes this is not true although it should be true.


  • 12.  RE: Java node + Bloomberg api

    Employee
    Posted 02-19-2014 04:03

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Tim Meagher

    Hi,

    The "==" comparator in Java does a direct object comparison.
    The two String objects you are comparing are not equal - however their contents are equal.
    What you should do instead is:
    if ("bid".equals(input_request)) { .... }
    I also just swapped the order of the comparison, which is done to guard against a null input_request.

    Regards,
    Tim.


  • 13.  RE: Java node + Bloomberg api

    Employee
    Posted 02-19-2014 04:33

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dialpemo

    that work perfectly thank you Tim, is there any way that I can make the output fields flexible to the requests, i.e. if the request is bid, only output the bid request, and if there is more than one request i.e. bid and ask. that would output both fields. I can control which one to output within my last while loop with the ifs statement but I am not sure how to actually make the output field flexible from that point, instead of getting just a null column when that request is not set.