0

In JMS there are Queues and Topics. As I understand it so far queues are best used for producer/consumer scenarios, where as topics can be used for publish/subscribe. However in my scenario I need a way to combine both approaches and create a producer-consumer-observer architecture.

Particularly I have producers which write to some queues and workers, which read from these queues and process the messages in those queues, then write it to a different queue (or topic). Whenever a worker has done a job my GUI should be notified and update its representation of the current system state. Since workers and GUI are different processes I cannot apply a simple observer pattern or notify the GUI directly.

What is the best way to realize this using a combination of queues and/or topics? The GUI should always be notified, but it should never consume anything from a queue?

I would like to solve this with JMS directly and not use any additional technology such as RMI to implement the observer part.

To give a more concrete example:

  1. I have a queue with packages (PACKAGEQUEUE), produced by machine (PackageProducer)
  2. I have a worker which takes a package from the PACKAGEQUEUE adds an address and then writes it to a MAILQUEUE (AddressWorker)
  3. Another worker processes the MAILQUEUE and sends the packages out by mail (MailWorker).
  4. After step 2. when a message is written to the MAILQUEUE, I want to notify the GUI and update the status of the package. Of course the GUI should not consume the messages in the MAILQUEUE, only the MailWorker must consume them.
lanoxx
  • 12,249
  • 13
  • 87
  • 142

1 Answers1

1

You can use a combination of queue and topic for your solution.

Your GUI application can subscribe to a topic, say MAILQUEUE_NOTIFICATION. Every time (i.e at step 2) PackageProducer writes message to MAILQUEUE, a copy of that message should be published to MAILQUEUE_NOTIFICATION topic. Since the GUI application has subscribed to the topic, it will get that publication containing information on status of the package. GUI can be updated with the contents of that publication.

HTH

Shashi
  • 14,980
  • 2
  • 33
  • 52
  • Thanks, one more question: Is it always necessary to use a combination of queue and topic or can I for example change the `MAILQUEUE` into a `MAILTOPIC` and then subscribe both the GUI and the MailWorker? Are there any implications which I'm not seeing at the moment? – lanoxx May 12 '12 at 10:40
  • Not necessary. You can use a topic instead of queue and both GUI and MailWorker subscribe to the topic. In your solution the MailWorker will process the message while GUI will just view the message. MailWorker must create a durable subscription as it should not miss publications even when it's not running. For GUI, you can choose the type of subscription. – Shashi May 13 '12 at 00:42
  • I see, so the catch is, that if I subscribe both GUI and Worker to a single topic, and one of them is not running then the message is lost. While with separate topic and queue this does not happen. Right? – lanoxx May 15 '12 at 10:59
  • No, there is no catch. You can create durable subscriptions for both GUI and Worker then messages will not be lost. I suggested you to choose an appropriate subscription(durable or non-durable) for GUI based on whether GUI needs to be notified of all the messages that are sent to Worker or all notifications. It looks like GUI needs to be notified of all messages. You can use durable subscription for both GUI and worker. Hope this clarifies. – Shashi May 16 '12 at 03:48
  • Yes that clarifies it. Thanks a lot. – lanoxx May 16 '12 at 14:01