Andrew Klofas
Andrew Klofas

Reputation: 620

Producer-consumer blocking queue with consumer filtering

I'm trying to write a producer-consumer pattern in java. I'm a network client connected to many servers over a series of different connections. The servers produce varying 'work' packets that all get put into one blocking queue. What I'm trying to do is block my consumers in a way that only specific types of work packets get delivered to specific consumers.

Example:

Is there some standard class in java.util... or do I have to roll my own class? Also, whats the best way to do roll my own if needed?

Thanks in advance!

Andrew Klofas

Upvotes: 1

Views: 1861

Answers (2)

maasg
maasg

Reputation: 37435

You can get this behavior with standard JMS. Your producer creates messages that are placed on the JMS topic. Consumers subscribe to the topic using a filtered subscription: That way, subscriber X only gets type X messages. (More about subscription on the API )

A durable subscriber will get your requirement of queueing messages per subscriber covered.

This is a pub/sub model.

Upvotes: 2

joeblubaugh
joeblubaugh

Reputation: 1126

There isn't a standard way to do this in java.util, but here's a simple design:

I'm assuming A,B,C all implement interface D and that it doesn't matter if an A is processed out-of-order with the Bs and Cs.

Have a single "input" BlockingQueue with a set of sorting consumers. These consumers determine the type of the objects in the queue and feeds them into separate type-specific "output" queues that your real workers take from. Is there some constraint that requires you only have one queue?

Upvotes: 2

Related Questions