Class: Qup::Adapter::Kestrel::Topic

Inherits:
Destination show all
Includes:
TopicAPI
Defined in:
lib/qup/adapter/kestrel/topic.rb

Overview

Internal: The Topic implementation for the Kestrel Adapter

The topic delivers each Message that it is give to each and every Subscriber

Instance Attribute Summary

Attributes inherited from Destination

#name

Instance Method Summary collapse

Methods included from TopicAPI

#destroy, #name

Methods inherited from Destination

#destroy, #initialize, #ping

Constructor Details

This class inherits a constructor from Qup::Adapter::Kestrel::Destination

Instance Method Details

#publish(message) ⇒ Object

Internal: Publish a Message to all the Subscribers

message - the Object to send to all subscribers

Returns nothing



62
63
64
# File 'lib/qup/adapter/kestrel/topic.rb', line 62

def publish( message )
  @client.set( @name,  message ) # do not expire the message
end

#publisherObject

Internal : Creates a Publisher for the Topic

Returns a new Publisher



16
17
18
# File 'lib/qup/adapter/kestrel/topic.rb', line 16

def publisher
  ::Qup::Publisher.new( self )
end

#subscriber(name) ⇒ Object

Internal: Create a subscriber for the Topic

name - the String name of the subscriber

Creating a subscriber creates a new Subscriber that will receive a copy of every message that is published to the Topic.

Subscribers are unique by name, two subscribers with the same name will act as individual Consumers on a queue of their name.

Returns a Subscriber



31
32
33
# File 'lib/qup/adapter/kestrel/topic.rb', line 31

def subscriber( name )
  ::Qup::Subscriber.new( self, subscriber_queue( name )  )
end

#subscriber_countObject

Internal: Return the number of Subscribers to this Topic

We want the sub portion of the json document that is in the ‘counters’ section. The keys in the ‘counters’ section that represent queue counters are all prefixed with ‘q/<queue_name>/<stat>’. To count the number of subscribers to this topic, we just count the uniqe <queue_name> elements that start with this queue’s name and followed by a ‘+’

Returns integer



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/qup/adapter/kestrel/topic.rb', line 45

def subscriber_count
  c = Set.new

  stats['queues'].keys.each do |k|
    next unless k =~ %r{\A#{@name}\+}
    parts = k.split("+")
    c << parts[1]
  end

  return c.size
end