public class ReadFromQueue
{
order = orderFromQueue;
if(!Repository.ordersIdMap.containsKey(order.orderID))
{
Platform.runLater(new Runnable()
{
@Override public void run()
{
Repository.ordersCollection.add(order);
}
}
});
Repository.ordersIdMap.put(order.orderID, order);
}
So hi again, I made another question, cause the previous one.. was bad (sorry) Let me put you in this scenario. You have a reader that goes to a queue and gets an order (lets say its a order object, ready to use), this thing work so quick that gets a lot of orders per second.. (To be exact i get 40.000 orders in less than a minute).. I have a Repository that is a singleton class, in this class i have a ordersIdMap (ConcurrentHashMap of key String and value Order) and a ordersCollection that is a ObservableList (the source of my tableView). I CAN'T add the order if already exist in the collection so in my map i save the orderId (string) as the key, so that way if the same order cames again i must update it (the else code is not here but is not importan now). The thing is that calling the Platform.runLater for drawing to the UI is giving me problems.. why? cause if i go to get another order and the Platform.runLater hasn't finished.. the order doesn't get created on the map, so to my "reader" the order its new and create it again (in the case that the order has the same orderId), so i get the same order over and over again.. I must say that some times its quick enough and the order gets the update .. but most of the times is too slow and the order its created again. I also tried to put the "Repository.ordersIdMap.put(order.orderID, order);" right next to the if condition.. so that way the map will have the key no matter what..BUT still doesn't work (why..? dunno).. Also if i dont use the platform.runlater ..its works but i get a lot NullPointersException.. cause im trying to update the UI to quick..i think.. any answer is usefull!!.. THANKS! and sorry for my english.
EDIT: This is the entire code.. the execRpt is a like a "small order" and from that i can create a bigger order.. i receive many execRpt and then create the orders.. if the order exist update..if not add it, but it fails on updating (some times) and just add the order like if it was a new one.
package com.larrainvial.trading.trademonitor.listeners;
import com.larrainvial.trading.emp.Controller;
import com.larrainvial.trading.trademonitor.Repository;
import com.larrainvial.trading.emp.Event;
import com.larrainvial.trading.emp.Listener;
import com.larrainvial.trading.fix44.events.ReceivedExecutionReportEvent;
import com.larrainvial.trading.trademonitor.events.CalculatePositionsEvent;
import com.larrainvial.trading.trademonitor.vo.ExecRptVo;
import com.larrainvial.trading.trademonitor.vo.OrderVo;
import javafx.application.Platform;
import javafx.concurrent.Task;
import quickfix.FieldNotFound;
import quickfix.fix44.ExecutionReport;
public class ReceivedExecutionReportToMonitorListener implements Listener
{
private OrderVo orderVo;
private String ordStatus = "";
private String transactTime = "";
private String text = "";
private int qty = 0;
private int cumQty = 0;
private int lastQty = 0;
private int leavesQty = 0;
private double price = 0;
private double avgPx = 0;
private double lastPx = 0;
@Override
public void eventOccurred(Event event)
{
ExecutionReport executionReport = ((ReceivedExecutionReportEvent)event).message;
try
{
String settlType = "";
String orderID = executionReport.isSetOrderID()? String.valueOf(executionReport.getOrderID().getValue()) : "";
String execID = executionReport.isSetExecID()? String.valueOf(executionReport.getExecID().getValue()) : "";
String execType = executionReport.isSetExecType()? String.valueOf(executionReport.getExecType().getValue()) : "";
String clOrdID = executionReport.isSetClOrdID()? String.valueOf(executionReport.getClOrdID().getValue()) : "";
String clOrdLinkID = executionReport.isSetClOrdLinkID()? String.valueOf(executionReport.getClOrdLinkID().getValue()) : "";
transactTime = executionReport.isSetTransactTime() ? String.valueOf(executionReport.getTransactTime().getValue()) : "";
text = executionReport.isSetText() ? executionReport.getText().getValue().toString() : "";
String tif = executionReport.isSetTimeInForce() ? String.valueOf(executionReport.getTimeInForce().getValue()) : "";
String handlInst = executionReport.isSetHandlInst() ? String.valueOf(executionReport.getHandlInst().getValue()) : "";
String securityExchange = executionReport.isSetSecurityExchange()? String.valueOf(executionReport.getSecurityExchange().getValue()) : "";
String orderType = executionReport.isSetOrdType()? String.valueOf(executionReport.getOrdType().getValue()) : "";
String account = executionReport.isSetAccount() ? String.valueOf(executionReport.getAccount().getValue()) : "None";
ordStatus = String.valueOf(executionReport.getOrdStatus().getValue());
lastPx = executionReport.isSetLastPx()? executionReport.getLastPx().getValue() : 0;
price = executionReport.isSetPrice()? executionReport.getPrice().getValue() : 0;
avgPx = executionReport.isSetAvgPx()? executionReport.getAvgPx().getValue() : 0;
lastQty = executionReport.isSetLastQty()? (int)executionReport.getLastQty().getValue() : 0;
leavesQty = executionReport.isSetLeavesQty()? (int)executionReport.getLeavesQty().getValue() : 0;
cumQty = executionReport.isSetCumQty()? (int)executionReport.getCumQty().getValue() : 0;
qty = executionReport.isSetOrderQty()? (int)executionReport.getOrderQty().getValue() : 0;
ExecRptVo execRpt = new ExecRptVo(orderID,
execID,
execType,
ordStatus,
clOrdID,
clOrdLinkID,
securityExchange,
String.valueOf(executionReport.getSide().getValue()),
qty,
lastQty,
leavesQty,
cumQty,
executionReport.getSymbol().getValue().toString(),
orderType,
price,
lastPx,
avgPx,
tif,
"",
handlInst,
securityExchange,
settlType,
account,
text,
transactTime);
orderVo = new OrderVo(execRpt);
OrderVo orderExist = Repository.ordersIdMap.putIfAbsent(orderID, orderVo);
if(orderExist == null)
{
Platform.runLater(new Runnable()
{
@Override public void run()
{
Repository.ordersCollection.add(orderVo);
}
});
}
else
{
Repository.ordersIdMap.get(orderID).price.set(price);
Repository.ordersIdMap.get(orderID).qty.set(qty);
Repository.ordersIdMap.get(orderID).ordStatus.set(ordStatus);
Repository.ordersIdMap.get(orderID).transactTime.set(transactTime);
Repository.ordersIdMap.get(orderID).text.set(text);
if(avgPx > 0)
Repository.ordersIdMap.get(orderID).avgPx.set(avgPx);
if(cumQty > 0)
Repository.ordersIdMap.get(orderID).cumQty.set(cumQty);
if(lastQty > 0)
Repository.ordersIdMap.get(orderID).lastQty.set(lastQty);
if(lastPx > 0)
Repository.ordersIdMap.get(orderID).lastPx.set(lastPx);
if(leavesQty > 0)
Repository.ordersIdMap.get(orderID).leavesQty.set(leavesQty);
if(ordStatus.equals("8"))
Repository.ordersIdMap.get(orderID).rejected.set("1");
Repository.ordersIdMap.get(orderID).execRpts.add(execRpt);
}
if(execType.equals("1") || execType.equals("2") || execType.equals("F"))
{
CalculatePositionsEvent calculatePositionsEvent = new CalculatePositionsEvent(execRpt);
Controller.dispatchEvent(calculatePositionsEvent);
}
}
catch (FieldNotFound ex)
{
ex.printStackTrace();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}