Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Publish/Subscribe with multiple subscribers (like a jms topic)

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<edit>
It was pointed out that publisher/subscriber is not the right terminology for what I'm describing. Please substitute producer/consumer instead.
</edit>


Good afternoon,

I'm trying to fashion a module following the publish/subscribe methodology. The idea is to publish messages which ALL subscribers will consume (rather than just one subscriber). This is like a JMS topic but without the overhead of JMS.

I've tried doing this with wait()s and notifyAll()s but ran into trouble because synchronization is required to obtain the lock and that limits the messages to being processed by one subscriber.

I also looked into BlockingQueue which is nearly exactly what I want, although again the queue.take() will only alert one subscriber per message.

What do you think? Does anyone have any ideas? I'd like to do this with just one collection or resource if possible, rather than having one queue per subscriber, but I can go to that model if necessary.

Thanks for your ideas.
[ December 15, 2008: Message edited by: Cameron Dalton ]
 
lowercase baba
Posts: 13025
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not very knowledgeable on the subject, so take anything I say with a grain of salt.

I thought the whole idea of the publish-subscribe pattern was that you can have many subscribers to a single publisher. Each subscriber registers with the publisher, who maintains a list of who is interested.

Then, when a message comes out, the publisher send a copy of the message to everyone interested.

The way you are describing it, it sounds like your publisher is putting one copy of the message in one spot, and you want each subscriber to come and read that only copy. I think this is wrong.

Each subscriber should have a way to receive the message, just like each subscriber of a magazine has a mail box. It wouldn't make sense for "Time" magazine to only print one copy of the magazine and expect everyone to come to <insert city here> to read the only copy in existance, would it?
 
Cameron Dalton
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose a more accurate term might be producer/consumer rather than publisher/subscriber. The scenario I described is what I need because I need the subscribers to block until a message is produced, which excludes the possibility of using an event-type callback function (unless there's something to that I'm not aware of).

Thanks for your input - good catch on my bad terminology.
 
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I ask why you're staying away from JMS? It's designed to do exactly this
 
Cameron Dalton
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Martijn Verburg:
Can I ask why you're staying away from JMS? It's designed to do exactly this



I'm hoping to avoid all the overhead of JMS connections and all the extra threading that goes along with them. This will only take place on a single server, not across multiple servers.
 
Cameron Dalton
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I discovered a solution using wait() and notifyAll() that I overlooked originally.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic