I have three Producers P1,P2,P3 and Three consumers with single shared queue. Producer P1 will put/insert X1,X2,X3 into queue and it should be consumed only by Consumer C1 not other Consumers(C2,C3). Basically Consumers C1 should only consume values inserted by Producer P1. the same rule applies to rest of the consumers. C2-> P2 and C3->P3. How to solve this problem in Java.
0
votes
Create 3 queues. Why would you use a shared queue, if there is no sharing going on?
- Andreas
Are you trying to solve it purely with Java using data structures, multi-threading or can you use third party libraries? If yes then I would suggest Kafka and how you can use topic. Basically there is a queue (broker) with n topics with source (producers) and a sink (consumers). Your consumer can subscribe to a particular topic.
- nabster
all of them should use/share same queue. and producers insert data randomly into the queue. consumers should take item from respective producers. can we do this in java.
- kiran kumar Mudradi
Yes only with Java and data structures.
- kiran kumar Mudradi
Create a single consumer taking from the queue and distributing to the others after determining data item type.
- daniu
1 Answers
0
votes
The way this would be ultimately working is there would be a class like this
class Envelope<S, T, M> {
final S sender;
final T topic;
final M message;
...
};
All sent messages will be wrapped into it (like real letters in the envelopes) and posted into a MailBox. Dispatcher at the other end of the mailbox will look at topic/receiver fields of the envelope and apply a required policy to deliver to a correct recipient in a correct context (thread).
As to coding it yourself — I would only do it for fun. If I needed something quickly I'd take Akka that already has all required primitives and used them.