Class: MarchHare::BlockingCallbackConsumer

Inherits:
CallbackConsumer show all
Defined in:
lib/march_hare/consumers/blocking.rb

Constant Summary

POISON =
:__poison__

Instance Attribute Summary

Attributes inherited from BaseConsumer

#auto_ack, #consumer_tag

Instance Method Summary (collapse)

Methods inherited from BaseConsumer

#active?, #cancelled?, #handleCancel, #handleCancelOk, #handleDelivery, #terminated?

Constructor Details

- (BlockingCallbackConsumer) initialize(channel, queue, buffer_size, opts, callback)

A new instance of BlockingCallbackConsumer



7
8
9
10
11
12
13
14
# File 'lib/march_hare/consumers/blocking.rb', line 7

def initialize(channel, queue, buffer_size, opts, callback)
  super(channel, queue, opts, callback)
  if buffer_size
    @internal_queue = JavaConcurrent::ArrayBlockingQueue.new(buffer_size)
  else
    @internal_queue = JavaConcurrent::LinkedBlockingQueue.new
  end
end

Instance Method Details

- (Object) cancel



16
17
18
19
20
# File 'lib/march_hare/consumers/blocking.rb', line 16

def cancel
  if super
    @internal_queue.offer(POISON)
  end
end

- (Object) deliver(*pair)



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/march_hare/consumers/blocking.rb', line 53

def deliver(*pair)
  if (@cancelling.get || @cancelled.get) || JavaConcurrent::Thread.current_thread.interrupted?
    @internal_queue.offer(pair)
  else
    begin
      @internal_queue.put(pair)
    rescue JavaConcurrent::InterruptedException => e
      JavaConcurrent::Thread.current_thread.interrupt
    end
  end
end

- (Object) gracefully_shut_down



65
66
67
68
69
70
# File 'lib/march_hare/consumers/blocking.rb', line 65

def gracefully_shut_down
  @cancelling.set(true)
  @internal_queue.offer(POISON)

  @terminated.set(true)
end

- (Object) start



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/march_hare/consumers/blocking.rb', line 22

def start
  interrupted = false
  until (@cancelling.get || @cancelled.get) || JavaConcurrent::Thread.current_thread.interrupted?
    begin
      pair = @internal_queue.take
      if pair
        if pair == POISON
          @cancelling.set(true)
        else
          @callback.call(*pair)
        end
      end
    rescue JavaConcurrent::InterruptedException => e
      interrupted = true
    end
  end
  while (pair = @internal_queue.poll)
    if pair
      if pair == POISON
        @cancelling.set(true)
      else
        @callback.call(*pair)
      end
    end
  end
  @terminated.set(true)
  if interrupted
    JavaConcurrent::Thread.current_thread.interrupt
  end
end