问题
I am working on designing trade system using Java Realtime ( Sun JRTS 2.2 ) and would like to few questions about best practices, because I am afraid of inventing the wheel and pretty sure that my task was already solved.
So I have thread that continuously reading the socket, parsing bytes and extracting messages (binary protocol ). Afterwards, I should send messages to the algorithm, that actually does some calculation and make decision to trade or not.
So I think the way I should design this system is to split it into 2 parts. Producer ( periodic(?) Realtime thread that extracts bytes from socket, parse it ) and Consumer ( Realtime thread (periodic/sporadic?) that extracts messages from producer, manipulates with them etc ).
So the first question is how to design high performance communication between these 2 threads ( producer/consumer )? Also I would love to hear the comments about the existing experience in designing such systems, advices and etc.
Thank you for your help!
回答1:
I am working with a similar problem but in a different domain:
Here is how I dealt with that:
public class Producer extends Thread{
private BlockingQueue<E> consumerQueue = null;
public setConsumerQueue(BlockingQueue<E> val){
consumerQueue = val;
}
// main method where data is received from socket...
public void run(){
while(!interrupted()){
data = socket.receive();// Receive data
if(consumerQueue!=null) consumerQueue.offer(data);
}
}
}
public class Consumer extends Thread{
private BlockingQueue<E> consumerQueue = new BlockingQueue<E>();
public Consumer (Producer val){
val.setConsumerQueue(consumerQueue);
}
public void run(){
while(!interrupted()){
data = consumerQueue.take();// block until there is data from producer
if(data !=null) processData(data);
}
}
}
回答2:
When deciding how to break up your application it is useful to have a good idea of how long each stage takes and what processing can be done in parallel. You want to time each stage in micro-seconds and have measure the distribution. The most interesting points are usually the 99% (worst 1%), 99.9% or 99.99%tile latencies.
I would have a look at the disruptor library. This is a fairly general purpose library designed for high throughput and low latency.
If you want something simpler there is a number of patterns which can exchange data between threads in sub-microsecond time, however these are point solutions which depend on your specific use case.
You might find this presentation interesting, though it mostly deals with testing low latency, high throughput components and communication over sockets. http://vanillajava.blogspot.com/2011/11/low-latency-slides.html
来源:https://stackoverflow.com/questions/8300851/producer-consumer-architecture-with-java-realtime