0

I'm struggling with this issue since some days ago and I'm not able to find a solution. I have a listener which receives market data (orders at bid and ask). If market is quiet (pre-market or post-market (low volatility)) everything works fine. But once the market is open the listener receives events too fast. So after a couple of minutes my app freezes. Right now the listener only assigns the received data to a var.

orderBookBid.getBuyOrders().addListener(new ObservableListModelListener<Order>() {
        @Override
        public void modelChanged(final Change<? extends Order> change) {
                System.out.println("bid event");
                   bidChange = change.getSource();
            }
    });

The program only freezes when uses real data. When market is closed and uses test data from a local file works fine.

Is there any way to set the maximum number of events per second? Or any way to ignore events for a short time period? Any idea on how can I handle this would be very appreciated.

Thanks.

fpinero
  • 5
  • 2

2 Answers2

1

You could put a load balancer in your application, that way it will create a queue and will not freeze the application.

If you want to let go some events, in the logic of your listener, you should have something that check if it's been X time since the last time you managed the event.

private long timeSinceLastEventManaged = 0;
private final static long MINIMUM_TIME = 2000; //2 seconds

In your listener

public void modelChanged(final Change<? extends Order> change) {
    long timeSystem = System.currentTimeMillis();
    if(timeSystem - timeSinceLastEventManaged > MINIMUM_TIME){
        //do your stuff
        timeSinceLastEventManaged  = timeSystem;
    }
}
TyMarc
  • 932
  • 7
  • 13
0

First of all you should get rid of the println as it is really slow

The rest depends on what you are doing. Right now it seems that you are just getting the value and writing it to a variable. You will only see the latest change that way and if that is what you want the solution @TyMarc suggested will work fine.

If what you showed us is just an example and you really need every change things get a bit more complicated. Your modelChanged method should be changed to add the current value to a queue (e.g a LinkedList or Stack).

public void modelChanged(final Change<? extends Order> change) 
        {
            syncronized(syncObject)
            {
               //add to your preffered queue
               syncObject.notifyAll()
            }
        }

This frees your listener from the real work and it can keep collecting data.

I added a syncronized as someone has to do the work. For this you can use a Thread that runs something like this:

Order current;
while(keeprunning)
{
   syncronized(syncObject)
   {
      if(queue.hasNext())
      {
        current = queue.getNext()
      }
      else
      {
        Thread.wait()
      }
   }

   //do the real work here
}

Now someone else has the problem. Literally. If the Thread can't handle the inflow of data the queue will grow in size until you run out of memory or hit some other limit. But that's another story.

And yes, nothing of this will compile as I only wanted to show an example

Community
  • 1
  • 1
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
  • I just need the last value. An Order is a snapshot with the best 60 to 100 orders at bid (at ask in the getSellOrders listener). The same snapshot is sent once and again if not new orders arrive. The println is not in the production code. The synchronized solution is something I've already tried with no success. I'll try the solution of @TyMarc, but 2 secs is to much, I'll try with 200 or 300ms. Now the market is closed and I can't check it. Everything works now. TY – fpinero Jun 19 '14 at 20:08
  • @fpinero Feel free to accept my answer if it works for you. Other people might find it useful too. – TyMarc Jun 20 '14 at 17:00
  • I accepted @TyMarc answer. It resolves the problem if you need to process less events. And that's what I asked. But the problem seems to be related with the ObservableListModelListener. Doing nothing in the listener, the program freezes. I've contacted with the API vendor, they are investigating the issue. Meanwhile I'm thinking if connect, disconnect the listener could help me to keep alive the app. – fpinero Jun 21 '14 at 16:42