hot_bunnies 1.4.0.pre2-java → 1.4.0.pre3-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -59,6 +59,9 @@ module HotBunnies
59
59
  end
60
60
 
61
61
  class Subscription
62
+ import 'java.util.concurrent.TimeUnit'
63
+ import 'java.util.concurrent.atomic.AtomicBoolean'
64
+
62
65
  attr_reader :channel, :queue_name, :consumer_tag
63
66
 
64
67
  def initialize(channel, queue_name, options={})
@@ -66,17 +69,22 @@ module HotBunnies
66
69
  @queue_name = queue_name
67
70
  @ack = options.fetch(:ack, false)
68
71
 
69
- @cancelled = java.util.concurrent.atomic.AtomicBoolean.new(false)
72
+ @cancelled = AtomicBoolean.new(false)
70
73
  end
71
74
 
72
75
  def each(options={}, &block)
73
76
  raise 'The subscription already has a message listener' if @consumer
74
- setup_consumer(options, block)
75
- @consumer.start
77
+ start(create_consumer(options, block))
76
78
  nil
77
79
  end
78
80
  alias_method :each_message, :each
79
81
 
82
+ def start(consumer)
83
+ @consumer = consumer
84
+ @consumer_tag = @channel.basic_consume(@queue_name, !@ack, @consumer)
85
+ @consumer.start
86
+ end
87
+
80
88
  def cancel
81
89
  raise 'Can\'t cancel: the subscriber haven\'t received an OK yet' if !self.active?
82
90
  @consumer.cancel
@@ -110,12 +118,15 @@ module HotBunnies
110
118
  def maybe_shutdown_executor
111
119
  if @executor && @shut_down_executor
112
120
  @executor.shutdown
121
+ unless @executor.await_termination(1, TimeUnit::SECONDS)
122
+ @executor.shutdown_now
123
+ end
113
124
  end
114
125
  end
115
126
 
116
- def setup_consumer(options, callback)
127
+ def create_consumer(options, callback)
117
128
  if options.fetch(:blocking, true)
118
- @consumer = BlockingCallbackConsumer.new(@channel, options[:buffer_size], callback)
129
+ BlockingCallbackConsumer.new(@channel, options[:buffer_size], callback)
119
130
  else
120
131
  if options[:executor]
121
132
  @shut_down_executor = false
@@ -124,21 +135,14 @@ module HotBunnies
124
135
  @shut_down_executor = true
125
136
  @executor = java.util.concurrent.Executors.new_single_thread_executor
126
137
  end
127
- @consumer = AsyncCallbackConsumer.new(@channel, callback, @executor)
138
+ AsyncCallbackConsumer.new(@channel, callback, @executor)
128
139
  end
129
- @consumer_tag = @channel.basic_consume(@queue_name, !@ack, @consumer)
130
140
  end
131
141
  end
132
142
 
133
- class CallbackConsumer < DefaultConsumer
134
- def initialize(channel, callback)
135
- super(channel)
136
- @callback = callback
137
- @callback_arity = @callback.arity
138
- @cancelled = false
139
- @cancelling = false
140
- end
143
+ public
141
144
 
145
+ class BaseConsumer < DefaultConsumer
142
146
  def handleDelivery(consumer_tag, envelope, properties, body)
143
147
  body = String.from_java_bytes(body)
144
148
  headers = Headers.new(channel, consumer_tag, envelope, properties)
@@ -156,13 +160,25 @@ module HotBunnies
156
160
  def start
157
161
  end
158
162
 
163
+ def deliver(headers, message)
164
+ raise NotImplementedError, 'To be implemented by a subclass'
165
+ end
166
+
159
167
  def cancel
160
168
  channel.basic_cancel(consumer_tag)
161
169
  @cancelling = true
162
170
  end
171
+ end
163
172
 
164
- def deliver(headers, message)
165
- raise NotImplementedError, 'To be implemented by a subclass'
173
+ private
174
+
175
+ class CallbackConsumer < BaseConsumer
176
+ def initialize(channel, callback)
177
+ super(channel)
178
+ @callback = callback
179
+ @callback_arity = @callback.arity
180
+ @cancelled = false
181
+ @cancelling = false
166
182
  end
167
183
 
168
184
  def callback(headers, message)
@@ -194,6 +210,7 @@ module HotBunnies
194
210
  import 'java.util.concurrent.LinkedBlockingQueue'
195
211
  import 'java.util.concurrent.ArrayBlockingQueue'
196
212
  import 'java.util.concurrent.TimeUnit'
213
+ import 'java.lang.InterruptedException'
197
214
 
198
215
  def initialize(channel, buffer_size, callback)
199
216
  super(channel, callback)
@@ -206,8 +223,12 @@ module HotBunnies
206
223
 
207
224
  def start
208
225
  until @cancelled
209
- pair = @internal_queue.poll(1, TimeUnit::SECONDS)
210
- callback(*pair) if pair
226
+ begin
227
+ pair = @internal_queue.take
228
+ callback(*pair) if pair
229
+ rescue InterruptedException => e
230
+ # time to stop
231
+ end
211
232
  end
212
233
  while (pair = @internal_queue.poll)
213
234
  callback(*pair)
@@ -218,8 +239,10 @@ module HotBunnies
218
239
  if @cancelling || @cancelled
219
240
  @internal_queue.offer(pair)
220
241
  else
221
- until @internal_queue.offer(pair, 1, TimeUnit::SECONDS)
222
- break if @cancelling || @cancelled
242
+ begin
243
+ @internal_queue.put(pair)
244
+ rescue InterruptedException => e
245
+ # time to stop
223
246
  end
224
247
  end
225
248
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module HotBunnies
4
- VERSION = '1.4.0.pre2'
4
+ VERSION = '1.4.0.pre3'
5
5
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: hot_bunnies
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 1.4.0.pre2
5
+ version: 1.4.0.pre3
6
6
  platform: java
7
7
  authors:
8
8
  - Theo Hultberg
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-06-27 00:00:00 Z
14
+ date: 2012-06-29 00:00:00 Z
15
15
  dependencies: []
16
16
 
17
17
  description: A object oriented interface to RabbitMQ that uses the Java driver under the hood