Reputation: 23
Suppose there is a System A which gives some output. This output is used as input by system B. The rate at which System A produces is faster than the rate at which system B consumes it. How can we implement this?
The System A sends video at 10mbps where system b can take up only 4mbps.how we can implement this.Also there should be continous streaming of video? communication happens thru socket. tcp/ip.
anybody know the logic in java ? there is a double buffer solution? i dont know.
Upvotes: 2
Views: 627
Reputation: 13916
How is the output of System A provided to System B? If they are in different JVM's you could use an Operating System pipe. Something like:
java SystemA | java SystemB
The OS will suspend SystemA automatically when it gets too far ahead of SystemB. If they're in the same JVM, use can use PipeInputStream and PipeOutputStream to achieve the same thing.
If the output of SystemA is not a stream then you'll have to use some kind of structured data approach as suggested in other answers.
Upvotes: 0
Reputation: 1647
Too general question. I suggest to review these frameworks: Apache Camel, Spring Integration
Upvotes: 0
Reputation: 23960
There are 2 distinct cases:
The second case can be solved by queueing (JMS), the first case is more difficult.
If you can scale up the hardware on B sufficiently, than that would be the way to go.
You could optimize the slowest part of B (optimize the code), but you'd have to do all kinds of testing again.
You could check if you can work with multiple B's (load balancing), but the application should support that (multiple instances updating the same data in the DB is not good:).
It all depends on the specific load distribution and the architecture of the apps.
Upvotes: 1
Reputation: 7021
You can use a solution like Java Messaging Service (JMS).
http://java.sun.com/products/jms/
This allows asynchronous communication via message queues. System A and B are completely independent from each other and do not have to process messages at the same speed.
Upvotes: 0
Reputation: 6795
If you study operating systems, telecommunications, or electronics, you'll learn that buffers are good for this.
They can be an elastic way to cope with temporary differences in speed. If the speed difference is permanent, and the buffer has a finite size, then System A will eventually have to pause or risk losing its output.
Upvotes: 1
Reputation: 32134
That depends. If System A continuously produces more output than System B can handle, you have two options:
If the rate at which System A produces output is temporarily larger than the rate at which System B can handle it, you should implement a queueing solution. System A stores its output in a queue and System B pops items of the queue.
Upvotes: 1
Reputation: 18570
This is known problem called Producer-Consumer: http://en.wikipedia.org/wiki/Producer-consumer_problem You can use threads as you question suggests - you can have System A run in different thread and when it makes sense, you can make that thread sleep. Other solution, known from hardware, could be to use a buffer, intermediary memory.
Upvotes: 5
Reputation: 5180
Improving system B performance or decreasing system A performance :-)
Upvotes: 0
Reputation: 22948
Well you either improve it or not, or you have system B wait for system A to send message or whatever .. you can use threads for that for waiting. Maybe if you could provide more details, someone would be able to answer. If you say how can I improve system B, what you want us to respond, we don't know anything about either system.
Upvotes: 0