• Post Reply Bookmark Topic Watch Topic
  • New Topic

Complex Data Structure Manipulation on an EJB Server

 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got yet another problem in my trading application. We have a set of limit orders, that is, a set of standings order to buy or sell X shares at price Y. Depending on the size of the market, we'll have anywehre from dozens to hundreds of buy limit orders and an equal number of sell limit orders.
When a new order comes in, we must compare it to the existing orders, to see if they cross (i.e. are willing to accept the buy/sell price of the other order) and if so, execute thr trade. If they do not cross, then we enter the new order into the limit order table, which is ordered by first by price, and then by order placement time.
The table will be changing very rapdily, as orders get placed every few milliseconds. Obviously, a Vector won't wory, because of the underlying array copy. Instead, I'll probably create my own tree structure for this. So far so good.
Now if I were writing my own server, here's what I would do (the issue, btw, is I'm not sure how to do this under J2EE). I'd have a single isnatnce of the tree, and make the root "public." I would have multiple OrderHandler objects. The OrderHandler would be repsonsibile for finding a match in the tree and executing the trade, or placing the order in the tree. To make it efficent, there would be a lock on each Order in the tree. The OrderHandlers could all read the tree, but in order to engage in using an exiting limit order in a trade, it must first acquire the lock. Obviously we need locks or an order may be executed twice. On the other hand, if a lock was needed in order to inspect each existing order, much time would be wasting waiting for locks on objects which are ultimately ignored.
OK, so I feel pretty confident about how to do that by myself. However, I'm using a EJB server which is supposed to make my life easy. How the heck do I do this?
1) I don't think I want each limit order to be a separate Entity EJB, because then I can't create a tree out of them, or order them in some way, right? Or do I make a tree of Entity EJBs? Do I store this tree in an Entity EJB or simply do this all with regular Java classes.
2) How do I lock the objects? I was under the impression that we're not supposed to use synchronized, because the contianer does all the synchronization for us. I'm not sure I can do that, since the underlying data isn't in the DB, and I can't use things like Value Objects. Is it safe to do the locking by hand? What if these are all regular Java classes, and not EJBs?
Any other thoughts or issues? Anyone have an exmaple of something like this? Surely other people have faced this issue.
--Mark

PS I've read some basic books on EJBs and have read through some exmaples on the web; however most seem "trivial" in the sense of taking some info, and reading/writing it to the DB. I haven't seen any exmaples doing the things I need, which is why I've posted so many questions in the last few weeks. If I'm asking basic questions and am missing a good reasource that can answer them, please let me know.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Believe it or not in the REAL WORLD (trust me -- I've designed trading systems for both the largest stock exchange in the U.S and the largest options exchange in the world) they use a database for this kind of information and let database locks do this. Really -- that's fast enough.
Kyle
[ December 16, 2002: Message edited by: Kyle Brown ]
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kyle Brown:
Believe it or not in the REAL WORLD (trust me -- I've designed trading systems for both the largest stock exchange in the U.S and the largest options exchange in the world) they use a database for this kind of information and let database locks do this. Really -- that's fast enough.


I'm dubious--not because I don't trust you, but because the companies for whom you did that work had no problem dropping an extra $10G for a kickass machine if that was necessary. The IT dept where I work, well, let's just say that despite having one of the top endowments in the world, professors who can buy a business class round-trip ticket to Europe, can't purchase a new computer except once every three years. So I expect that I'll be working with only mediocre machines, not top of the line back-end servers.

But I'm willing to take this gamble. So each order I get, I would put into the database in a limit order table, represented by Entity EJBs. The DB can do the locking, but what about sorting? Let's assume that my DB can sort the table entries for me, so I can safely start at the top and do an effivcent search; how do I capitalize on that behavior from the EJB server? How do I know which entity beans correspond to the lowest prices (or highest priced) table entries? Do I actually need to check each DB entry each and every time?
--Mark

PS If I haven't said so already, I *really* appeciate all your advice. :-)
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two options for sorting:
(1) Do the reading through a DAO that uses a SORT BY clause in the SELECT statement (probably still preferred even in EJB 2.0). This is called the "Fast Lane Reader" pattern in Floyd Marinescu's book.
(2) Use a vendor extension to EJB 2.0 (both WebSphere and Weblogic support it) for sorting in EJB-QL.
Kyle
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!