aspose file tools*
The moose likes Java in General and the fly likes Publish/Subscribe with multiple subscribers (like a jms topic) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Publish/Subscribe with multiple subscribers (like a jms topic)" Watch "Publish/Subscribe with multiple subscribers (like a jms topic)" New topic
Author

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

Cameron Dalton
Greenhorn

Joined: Nov 14, 2008
Posts: 19
<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 ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11318
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Cameron Dalton
Greenhorn

Joined: Nov 14, 2008
Posts: 19
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.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Can I ask why you're staying away from JMS? It's designed to do exactly this


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Cameron Dalton
Greenhorn

Joined: Nov 14, 2008
Posts: 19
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

Joined: Nov 14, 2008
Posts: 19
I discovered a solution using wait() and notifyAll() that I overlooked originally.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Publish/Subscribe with multiple subscribers (like a jms topic)