not able to rerurn JSON aaray in RESTAPI (local host )

public void onTextMessage(WebSocket websocket, String message) throws JSONException {


if(!message.isEmpty()) {


System.out.println("RECEIVED:");





JSONArray jsonArray = new JSONArray(message);





System.out.println(jsonArray.toString(2));





for (int i = 0; i < jsonArray.length(); ++i)

processMessage(websocket, jsonArray.getJSONObject(i));

}

}



i am able retrieve JSON from method, but not able to show it on webservice , as this is void methos plus its in anonymous class ,returning JSON from this and showing it in rest webservice is very difficult and i am unable to do that . i took that JSON array in list or map , but am unable to return that list with JSON ARRAY an show it on my localhost of webservice .This is only in case of JAVA .

Best Answer

  • Jirapongse
    Jirapongse admin
    Answer ✓

    @shyam.sanghvi

    I assume that you are referring to a Java example that uses nv-websocket-client to retrieve the data. The example just demonstrates how to use WebSocket API to retrieve the data from TREP. Handling and processing the data depends on the application.

    From the example, the onTextMessage callback function is called a thread internally created by the nv-websocket-client library while the main thread has slept.

    image

    Therefore, you need to process the data inside this callback or put the data into a queue to be processed by another thread. I suggest putting the data into a queue to be processed by another thread to avoid a slow consumer problem.


Answers

  • Hello @shyam.sanghvi

    Even though the nv-websocket-client WebSocket onTextMessage callback is a void method but you can call other methods or put data in to queue/others process/threads via on onTextMessage method as suggest by my colleague above.

    Additionally, I did a quick test for by global variable scenario by creating an ArrayList object to keep each JSONArray object to process later. The code can add all received JSONArray object into an ArrayList and get each JSON message back from an ArrayList object.

    public static ArrayList jsonArrayList =  new ArrayList<JSONArray>();
    ...
    public void onTextMessage(WebSocket websocket, String message) {
        System.out.println("RECEIVED:");
        JSONArray jsonArray = new JSONArray(message);

        jsonArrayList.add(jsonArray);
        ...
    }

    //Later use
    JSONArray laterJsonArray;
    Iterator<JSONArray> iter = jsonArrayList.iterator();
    while(iter.hasNext()){
        laterJsonArray = iter.next();
        // Process JSON message in laterJsonArray object
    }
  • @Wasin Waeosri Thanks For precious inputs .

    Only these methods looks Feasible as said by your collegue (queue/others process/threads ).

    Below method which you have tested , i have tried all the things , it doesnt work in restcontroller class , i have also created POJO and put the bid price and ask price in setter , added in map , It doent work still.

    I don know how it worked for you , could you please try your example( used iterator ) in rest webservice.


    Could you please give any example of adding it in queue ,or thread or process.

    Thank you Wasin Waeosri

  • jirapongse.phuriphanvichai

    Thanks for the valuable inputs

    Adding it queue is the only way i havent tried .

    Could you please guve example on how to use it with my Controller class


    Thanks again brother .

    I am really stuck in this since 2 weeks .

    Not able to process data with rest webservice.

    I am pointing on REST because ( through local host URL). i will be able fetch that URL in my front end which is in React jS.

    Please let me know how to add queues in controller class.

  • @shyam.sanghvi

    From my understanding, you need a synchronous function that accepts RIC and fields, and this function returns JSONObject to the caller. Therefore, the controller can call this function to retrieve the data. For example:


        public JSONObject GetData(String Ric, String[] fields) {      
          
        }


    Moreover, you just require the latest data, not tick by tick data. Is this correct?


  • jirapongse.phuriphanvichai

    Wasin Waeosri


    Thanks alot for inputs.

    I was able to retrieve the data in my rest local host through QUEUE implementation.


    Thank you Guys! Cheers

    happy New Year.

  • @Wasin Waeosri

    Thank You

    I was able to retrieve the data in my rest local host through QUEUE implementation.