Class: Legion::Transport::Exchange

Inherits:
Exchange
  • Object
show all
Includes:
Common
Defined in:
lib/legion/transport/exchange.rb

Instance Method Summary collapse

Methods included from Common

#channel_open?, #close, #close!, #deep_merge, #generate_consumer_tag, #open_channel, #options_builder

Constructor Details

#initialize(exchange = exchange_name, options = {}) ⇒ Exchange

Returns a new instance of Exchange.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/legion/transport/exchange.rb', line 8

def initialize(exchange = exchange_name, options = {})
  @options = options
  @type = options[:type] || default_type
  super(channel, @type, exchange, options_builder(default_options, exchange_options, @options))
rescue Legion::Transport::CONNECTOR::PreconditionFailed, Legion::Transport::CONNECTOR::ChannelAlreadyClosed
  raise unless @retries.nil?

  @retries = 1
  delete_exchange(exchange)
  retry
end

Instance Method Details

#channelObject



59
60
61
62
63
64
# File 'lib/legion/transport/exchange.rb', line 59

def channel
  @channel ||= Legion::Transport::Connection.channel
rescue Legion::Transport::CONNECTOR::ChannelLevelException => e
  @channel = Legion::Transport::Connection.channel
  raise e unless @channel.open?
end

#default_optionsObject



26
27
28
29
30
31
32
33
# File 'lib/legion/transport/exchange.rb', line 26

def default_options
  hash = Concurrent::Hash.new
  hash[:durable] = true
  hash[:auto_delete] = false
  hash[:arguments] = {}
  hash[:passive] = passive?
  hash
end

#default_typeObject



55
56
57
# File 'lib/legion/transport/exchange.rb', line 55

def default_type
  'topic'
end

#delete(options = {}) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/legion/transport/exchange.rb', line 47

def delete(options = {})
  super
  true
rescue Legion::Transport::CONNECTOR::PreconditionFailed => e
  Legion::Logging.warn("Exchange#delete precondition failed: #{e.message}") if defined?(Legion::Logging)
  false
end

#delete_exchange(exchange) ⇒ Object



20
21
22
23
24
# File 'lib/legion/transport/exchange.rb', line 20

def delete_exchange(exchange)
  Legion::Transport.logger.warn "Exchange:#{exchange} exists with wrong parameters, deleting and creating"
  @channel = Legion::Transport::Connection.channel
  @channel.exchange_delete(exchange)
end

#exchange_nameObject



39
40
41
# File 'lib/legion/transport/exchange.rb', line 39

def exchange_name
  self.class.ancestors.first.to_s.split('::')[2].downcase
end

#exchange_optionsObject



43
44
45
# File 'lib/legion/transport/exchange.rb', line 43

def exchange_options
  Concurrent::Hash.new
end

#passive?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/legion/transport/exchange.rb', line 35

def passive?
  false
end