0

I have a question about Producer/consumer Design pattern , in fact my situation is :I have One class that produce multiple types of messages (notifications) and multiple consumers who consume those messages.

The complication is my Producer produce different types of messages and my consumers consume those messages .

So what is the best implementation of this situation? Is the Producer/Consumer design-pattern the best solution of this situation?

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
Zakaria
  • 1,675
  • 4
  • 21
  • 23
  • 1
    It's impossible to tell what you're asking. – Jonathon Faust Jun 25 '10 at 16:27
  • I think there are a lot of details necessary to make this decision that we aren't getting. My initial thought how ever would be a producer with multiple buffers and a consumer for each buffer... though there are a lot of 'what ifs' that arise... – Shaded Jun 25 '10 at 16:29
  • In fact that was my first try : is to have one producer with multiple buffers and a consumer for each buffer, but it's so complicated to handle. – Zakaria Jun 25 '10 at 16:38

2 Answers2

0

I think there are two independent problems here. One is getting a "message" object across. That is a producer/consumer problem, though if you want reliability you need to consider a variety of issues like threading, recovery, etc. If you actually do this between multiple machines, do yourself a favour and use an existing framework like JMS.

Separately from that is the problem of handling different types of messages. Usually you would have some type hierarchy of messages all subtyping a common "Message" ancesor. I'm not sure about the semantics of your message, but something along the lines of the command pattern might be a good fit.

You really need to clarify your question more for us to be able to provide better advice.

Uri
  • 88,451
  • 51
  • 221
  • 321
  • To clarify more those messages are totally different . I have a trading application who connect to a broker. This broker give me a lot of prices (this is the first type of message) and give execution report of trades(this is another type). – Zakaria Jun 25 '10 at 16:37
  • Those consumers are executed locally(Threads) and that to handle several markets in parallel. – Zakaria Jun 25 '10 at 16:42
0

Given that it sounds like you are coding both the producer and consumer, I would say it would be a good idea to use the produce/consumer pattern. As there are many types of messages, maybe it would be best to use the 'pull' data model (get the consumers to ask for the messages, rather than have every consumer get every message). Here is an outline that may help:

public interface BrokerProducer
{
    public void addConsumer(BrokerConsumer consumer);        
    public void removeConsumer(BrokerConsumer consumer);

    public Price getPrices();
    public Report getReport();
}

public interface BrokerConsumer
{
    public void update(BrokerProducer brokerProducer);
}

public class Broker implements BrokerProducer
{
    private Collection<BrokerConsumer> consumer = new HashSet<BrokerConsumer>();

    // Implement the interface

    // Notify all consumers- there are messages ready.
    public void notifyConsumers()
    {
        for(BrokerConsumer consumer : consumers)
            consumer.update(this);
    }       
}

public class PricesConsumer implements BrokerConsumer
{
    @Override
    public void update(BorkerProducer producer)
    {
        Prices prices = producer.getPrices();
        // Do something with the prices.
    }
}
Kevin
  • 4,070
  • 4
  • 45
  • 67