Вопрос

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:

  • The blocking queue is filled with three different types of work packet objects: A, B, C
  • Two consumers are waiting for work packet objects. Consumer 1 only wants type A and consumer 2 only wants type B or C

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

Это было полезно?

Решение

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?

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top