Class: Crossroads::Stomp

Inherits:
Object
  • Object
show all
Defined in:
lib/crossroads/stomp.rb

Defined Under Namespace

Classes: EventLogger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Stomp

Returns a new instance of Stomp.



36
37
38
39
40
# File 'lib/crossroads/stomp.rb', line 36

def initialize(config)
  connect(config)

  @subscriptions = []
end

Instance Attribute Details

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



3
4
5
# File 'lib/crossroads/stomp.rb', line 3

def subscriptions
  @subscriptions
end

Instance Method Details

#connect(config) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/crossroads/stomp.rb', line 42

def connect(config)
  hosts = []

  [config].flatten.each do |c|
    host = {}

    host[:host] = c[:server]
    host[:port] = c[:port]
    host[:login] = c[:user] if c[:user]
    host[:passcode] = c[:password] if c[:password]

    hosts << host
  end

  raise "No hosts defined for Stomp connection" if hosts.size == 0

  connection = {:hosts => hosts, :logger => EventLogger.new}

  @connection = ::Stomp::Connection.new(connection)
end

#publish(target, msg, headers) ⇒ Object



77
78
79
# File 'lib/crossroads/stomp.rb', line 77

def publish(target, msg, headers)
  @connection.publish(target, msg, headers)
end

#receiveObject



81
82
83
# File 'lib/crossroads/stomp.rb', line 81

def receive
  @connection.receive
end

#subscribe(source) ⇒ Object



63
64
65
66
67
68
# File 'lib/crossroads/stomp.rb', line 63

def subscribe(source)
  Log.info("Subscribing to #{source}")

  @connection.subscribe source
  @subscriptions << source
end

#unsubscribe(source) ⇒ Object



70
71
72
73
74
75
# File 'lib/crossroads/stomp.rb', line 70

def unsubscribe(source)
  Log.info("Unsubscribing from #{source}")

  @connection.unsubscribe source
  @subscriptions.delete source
end