jerryluk-rabbitmq-jruby-client 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +7 -1
- data/lib/rabbitmq_client.rb +18 -0
- data/spec/rabbitmq_client_spec.rb +21 -0
- metadata +1 -1
data/README
CHANGED
@@ -32,11 +32,17 @@ queue.publish('message body')
|
|
32
32
|
# Retrieve a message from the queue
|
33
33
|
message = queue.retrieve
|
34
34
|
|
35
|
-
# Subscribe to a queue with callback
|
35
|
+
# Subscribe to a queue with callback. The callback will be run in a new thread for each new message.
|
36
36
|
queue.subscribe do |message|
|
37
37
|
# do something with message
|
38
38
|
end
|
39
39
|
|
40
|
+
# Subscribe to a queue in a loop. No new thread will be created.
|
41
|
+
queue.loop_subscribe do |message|
|
42
|
+
# do something with message
|
43
|
+
end
|
44
|
+
|
45
|
+
|
40
46
|
Installing RabbitMQ on OS X
|
41
47
|
===========================
|
42
48
|
1. Install MacPorts
|
data/lib/rabbitmq_client.rb
CHANGED
@@ -12,7 +12,9 @@ class RabbitMQClient
|
|
12
12
|
include_class('com.rabbitmq.client.Channel')
|
13
13
|
include_class('com.rabbitmq.client.Consumer')
|
14
14
|
include_class('com.rabbitmq.client.DefaultConsumer')
|
15
|
+
include_class('com.rabbitmq.client.QueueingConsumer')
|
15
16
|
include_class('com.rabbitmq.client.MessageProperties')
|
17
|
+
include_class('java.lang.InterruptedException')
|
16
18
|
include_class('java.lang.String') { |package, name| "J#{name}" }
|
17
19
|
|
18
20
|
class RabbitMQClientError < StandardError;end
|
@@ -88,6 +90,22 @@ class RabbitMQClient
|
|
88
90
|
@channel.basic_consume(@name, no_ack, QueueConsumer.new(@channel, block))
|
89
91
|
end
|
90
92
|
|
93
|
+
def loop_subscribe(&block)
|
94
|
+
no_ack = false
|
95
|
+
consumer = QueueingConsumer.new(@channel)
|
96
|
+
@channel.basic_consume(@name, no_ack, consumer)
|
97
|
+
loop do
|
98
|
+
begin
|
99
|
+
delivery = consumer.next_delivery
|
100
|
+
message_body = Marshal.load(String.from_java_bytes(delivery.get_body))
|
101
|
+
block.call message_body
|
102
|
+
@channel.basic_ack(delivery.get_envelope.get_delivery_tag, false)
|
103
|
+
rescue InterruptedException => ie
|
104
|
+
next
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
91
109
|
protected
|
92
110
|
def auto_bind
|
93
111
|
unless @exchange
|
@@ -42,6 +42,8 @@ describe RabbitMQClient do
|
|
42
42
|
@queue.bind(@exchange)
|
43
43
|
@queue.publish('Hello World')
|
44
44
|
@queue.retrieve.should == 'Hello World'
|
45
|
+
@queue.publish('人大')
|
46
|
+
@queue.retrieve.should == '人大'
|
45
47
|
end
|
46
48
|
|
47
49
|
it "should able to subscribe with a callback function" do
|
@@ -56,6 +58,25 @@ describe RabbitMQClient do
|
|
56
58
|
a.should == 3
|
57
59
|
end
|
58
60
|
|
61
|
+
it "should able to subscribe to a queue using loop_subscribe" do
|
62
|
+
a = 0
|
63
|
+
@queue.bind(@exchange)
|
64
|
+
Thread.new do
|
65
|
+
begin
|
66
|
+
timeout(1) do
|
67
|
+
@queue.loop_subscribe do |v|
|
68
|
+
a += v.to_i
|
69
|
+
end
|
70
|
+
end
|
71
|
+
rescue Timeout::Error => e
|
72
|
+
end
|
73
|
+
end
|
74
|
+
@queue.publish("1")
|
75
|
+
@queue.publish("2")
|
76
|
+
sleep 2
|
77
|
+
a.should == 3
|
78
|
+
end
|
79
|
+
|
59
80
|
it "should raise an exception if binding a persistent queue with non-persistent exchange and vice versa" do
|
60
81
|
persistent_queue = @client.queue('test_queue1', true)
|
61
82
|
persistent_exchange = @client.exchange('test_exchange1', 'fanout', true)
|