What is the purpose of Thread.sleep(60000) method in class com.thomsonreuters.ema.examples.training.

Best Answer

  • umer.nalla
    Answer ✓

    Hello @Chaitanya.Vishnubhotla,

    As explained by my colleague, the sleep(6000) is just allowing the app 60 secs to run and give it some time to receive back some data from the server. I suspect that when you use a shorter time value, you are not allowing enough time for the application to login to server, make the requests and receive back the responses.

    If you want to run the app for less than 60 seconds and exit as soon as all data has been received, then the USER_DISPATCH mode would be better suited - see the example130 mentioned earlier. Instead of looping for 60 seconds, you could instead use a variable to control the loop and keep calling dispatch() and looping until the flag variable changes state.

    In your RefreshMsg / StatusMsg callback handlers, you could track your responses and then change the flag variable once you have received a RefreshMsg (or StatusMsg) for all your requested items. You would expect to receive a RefreshMsg for all valid items you are permissioned for. For any invalid or non-permissioned items your would expect a StatusMsg.

    As per before, I do recommend you check out the Webinar and/or Tutorials to get a better understanding of how EMA works.

    Hope this helps.

Answers

  • Chaitanya.Vishnubhotla

    Because the example application uses the default operation model: API_DISPATCH where EMA manages the dispatching of events within a background thread. Hence, the application must block and wait for EMA thread to capture market data events; waiting for RefreshMsg comes to onRefreshMsg(..).

  • Pimchaya.Wongrukun

    Is there a way to increase the speed of response for dispatching the events, as we want only onRefreshMsg and onStatusMsg events to be dispatched as part of our requirement ?

  • Chaitanya.Vishnubhotla

    The API should dispatch events as soon as they become available. I am not sure what you mean about only wanting Status and Refresh messages...Since this his a Snapshot example you should only receive Refresh and Status messages.

    The dispatch is done behind the scenes via the API dispatch thread.

    The sleep(6000) is only there because this is just an example and we want the main application thread to run for 60 seconds before exiting - not something you would expect to do in a real application.

    If you want to control the dispatch yourselves, then you can use the USER_DISPATCH mode - which is covered by the example130__MarketPrice__UserDisp

    That example uses the application thread to loop for 60 seconds whilst calling the dispatch method from the application thread.

    while (startTime + 60000 > System.currentTimeMillis())
    consumer.dispatch(10);

    The dispatch(10) means the method will wait for 10µs for a new event to arrive before returning control back to the loop. If an event arrives before the 10ms it will be dispatched immediately.

    I recommend you refer to the EMA Java tutorials if you have not already done so to get a good high level understanding of how the API works:

    EMA Consumer - Introduction

    You can also refer to the Introduction to EMA Webinar - which again provides a good high level overview of the API - including the Dispatch mechanism etc.

    Webinar Recording: Introduction to Elektron Message API

    Hope that helps.

  • Hi @Nipat Kunvutipongsak/ @Wasin Waeosri / @altohbunhsiV.aynatiahC

    When I put a value of 6000 in Thread.sleep(6000) method, we are getting the expected results. But if I put a value less than 6000, we are getting weird results like for example for every fid RIC code will get an channel down message.

    Does giving sleep value of 6000 seconds is mandatory?

    Please let us know.

  • Hello @Chaitanya.Vishnubhotla,

    Please consider the example below:

    public class Consumer 
    {
    public static void main(String[] args)
    {
    OmmConsumer consumer = null;
    try
    {
    AppClient appClient = new AppClient();
    OmmConsumerConfig config = EmaFactory.createOmmConsumerConfig();
    consumer = EmaFactory.createOmmConsumer(config.host("localhost:14002").username("user"));


    ReqMsg reqMsg = EmaFactory.createReqMsg();
    consumer.registerClient(reqMsg.serviceName("DIRECT_FEED").name("IBM.N"), appClient);
    Thread.sleep(60000); // API calls onRefreshMsg(), onUpdateMsg() and onStatusMsg()
    }
    catch (InterruptedException | OmmException excp)
    {
    System.out.println(excp.getMessage());
    }
    finally
    {
    if (consumer != null) consumer.uninitialize();
    }
    }
    }

    When the application calls the consumer.registerClient(reqMsg.serviceName("DIRECT_FEED").name("IBM.N"), appClient); method. If the application doesn't wait for retrieving data back from the server, the next statement will be if (consumer != null) consumer.uninitialize(); (in the finally block), which means that the application will terminate the connection and exit the main method.

    Therefore, the meaning of this Thread.sleep(<wait time>); here is to let the application process lives for the timeout specified. During the sleeptime, the application layer may get data events through its callback methods such as onRefresh() or onUpdate() from EMA layer.

    So, Thread.sleep() may not be necessary if the application has another logic to hold itself enough (to receive data) before it exits the main() method and ends the process.

    Hope this helps.