jerryluk-rabbitmq-jruby-client 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Jerry Luk <jerryluk@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,22 @@
1
+
2
+ This project is a JRuby wrapper of the Java RabbitMQ Client from Rabbit Technologies
3
+
4
+ http://www.rabbitmq.com/
5
+
6
+ Using this library does not require you to use EventMachine for subscribing a RabbitMQ queue.
7
+
8
+ To use RabbitMQ JRuby Client with Workling:
9
+ 1. Copy workling_rabbitmq_client.rb and workling_rabbitmq_subscriber.rb to lib
10
+ Create an initializer 'rabbitmq.rb' in 'config/initializers'
11
+
12
+ require 'workling_rabbitmq_client'
13
+ Workling::Remote.dispatcher = Workling::Remote::Runners::ClientRunner.new
14
+ Workling::Remote.dispatcher.client = Workling::Clients::WorklingRabbitMQClient.new
15
+
16
+ Optional: To use subscriber
17
+ Modify vendor/plugins/workling/script/listen.rb, added this after the require statements
18
+
19
+ require 'workling_rabbitmq_subscriber'
20
+
21
+ To start Workling: (You can use script/workling_client because it uses daemons)
22
+ jruby vendor/plugins/workling/script/listen.rb
Binary file
Binary file
data/lib/junit.jar ADDED
Binary file
Binary file
@@ -0,0 +1,151 @@
1
+ require 'java'
2
+ require File.dirname(__FILE__) + '/junit.jar'
3
+ require File.dirname(__FILE__) + '/commons-cli-1.1.jar'
4
+ require File.dirname(__FILE__) + '/commons-io-1.2.jar'
5
+ require File.dirname(__FILE__) + '/rabbitmq-client.jar'
6
+
7
+ class RabbitMQClient
8
+ include ObjectSpace
9
+ include_class('com.rabbitmq.client.Connection')
10
+ include_class('com.rabbitmq.client.ConnectionParameters')
11
+ include_class('com.rabbitmq.client.ConnectionFactory')
12
+ include_class('com.rabbitmq.client.Channel')
13
+ include_class('com.rabbitmq.client.Consumer')
14
+ include_class('com.rabbitmq.client.DefaultConsumer')
15
+ include_class('java.lang.String') { |package, name| "J#{name}" }
16
+
17
+ class QueueConsumer < DefaultConsumer
18
+ def initialize(channel, block)
19
+ @channel = channel
20
+ @block = block
21
+ super(channel)
22
+ end
23
+
24
+ def handleDelivery(consumer_tag, envelope, properties, body)
25
+ delivery_tag = envelope.get_delivery_tag
26
+ message_body = Marshal.load(String.from_java_bytes(body))
27
+ @block.call message_body
28
+ @channel.basic_ack(delivery_tag, false)
29
+ end
30
+ end
31
+
32
+ class Queue
33
+ def initialize(name, channel)
34
+ @name = name
35
+ @channel = channel
36
+ @channel.queue_declare(name)
37
+ self
38
+ end
39
+
40
+ def bind(exchange, routing_key=nil)
41
+ @routing_key = routing_key || "#{@name}_#{Time.new.to_i.to_s}_#{rand(100).to_s}"
42
+ @exchange = exchange
43
+ @channel.queue_bind(@name, @exchange.name, @routing_key)
44
+ self
45
+ end
46
+
47
+ def publish(message_body, props=nil)
48
+ auto_bind
49
+ message_body_byte = Marshal.dump(message_body).to_java_bytes
50
+ @channel.basic_publish(@exchange.name, @routing_key, props, message_body_byte)
51
+ message_body
52
+ end
53
+
54
+ def retrieve
55
+ auto_bind
56
+ message_body = nil
57
+ no_ack = false
58
+ response = @channel.basic_get(@name, no_ack)
59
+ if response
60
+ props = response.get_props
61
+ message_body = Marshal.load(String.from_java_bytes(response.get_body))
62
+ delivery_tag = response.get_envelope.get_delivery_tag
63
+ @channel.basic_ack(delivery_tag, false)
64
+ end
65
+ message_body
66
+ end
67
+
68
+ def subscribe(&block)
69
+ no_ack = false
70
+ @channel.basic_consume(@name, no_ack, QueueConsumer.new(@channel, block))
71
+ end
72
+
73
+ protected
74
+ def auto_bind
75
+ unless @exchange
76
+ exchange = Exchange.new("@name_exchange", 'fanout', @channel)
77
+ self.bind(exchange)
78
+ end
79
+ end
80
+ end
81
+
82
+ class Exchange
83
+ attr_reader :name
84
+
85
+ def initialize(name, type, channel)
86
+ @name = name
87
+ @type = type
88
+ @channel = channel
89
+ @channel.exchange_declare(@name, type.to_s)
90
+ self
91
+ end
92
+ end
93
+
94
+ # Class Methods
95
+ class << self
96
+ end
97
+
98
+ attr_reader :channel
99
+ attr_reader :connection
100
+
101
+ # Instance Methods
102
+ def initialize(options={})
103
+ # server address
104
+ @host = options[:host] || '127.0.0.1'
105
+ @port = options[:port] || 5672
106
+
107
+ # login details
108
+ @username = options[:username] || 'guest'
109
+ @password = options[:password] || 'guest'
110
+ @vhost = options[:vhost] || '/'
111
+
112
+ # queues and exchanges
113
+ @queues = {}
114
+ @exchanges = {}
115
+
116
+ connect
117
+ # Disconnect before the object is destroyed
118
+ define_finalizer(self, lambda {|id| self.disconnect if self.connected? })
119
+ self
120
+ end
121
+
122
+ def connect
123
+ params = ConnectionParameters.new
124
+ params.set_username(@username)
125
+ params.set_password(@password)
126
+ params.set_virtual_host(@vhost)
127
+ params.set_requested_heartbeat(0)
128
+ conn_factory = ConnectionFactory.new(params)
129
+ @connection = conn_factory.new_connection(@host, @port)
130
+ @channel = @connection.create_channel
131
+ end
132
+
133
+ def disconnect
134
+ @channel.close
135
+ @connection.close
136
+ @connection = nil
137
+ end
138
+
139
+ def connected?
140
+ @connection != nil
141
+ end
142
+
143
+ def queue(name)
144
+ @queues[name] ||= Queue.new(name, @channel)
145
+ end
146
+
147
+ def exchange(name, type='fanout')
148
+ @exchanges[name] ||= Exchange.new(name, type, @channel)
149
+ end
150
+ end
151
+
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require File.dirname(__FILE__) + '/../lib/rabbitmq_client'
4
+
5
+ describe RabbitMQClient do
6
+ before(:each) do
7
+ @client = RabbitMQClient.new
8
+ end
9
+
10
+ it "should able to create a connection" do
11
+ @client.connection.should_not be_nil
12
+ end
13
+
14
+ it "should able to create a channel" do
15
+ @client.channel.should_not be_nil
16
+ end
17
+
18
+ it "should be able to create a new exchange" do
19
+ exchange = @client.exchange('test_exchange', 'direct')
20
+ exchange.should_not be_nil
21
+ end
22
+
23
+ describe Queue do
24
+ before(:each) do
25
+ @queue = @client.queue('test_queue')
26
+ @exchange = @client.exchange('test_exchange', 'direct')
27
+ end
28
+
29
+ it "should able to create a queue" do
30
+ @queue.should_not be_nil
31
+ end
32
+
33
+ it "should able to bind to an exchange" do
34
+ @queue.bind(@exchange).should_not be_nil
35
+ end
36
+
37
+ it "should able to publish and retrieve a message" do
38
+ @queue.bind(@exchange)
39
+ @queue.publish('Hello World')
40
+ @queue.retrieve.should == 'Hello World'
41
+ end
42
+
43
+ it "should able to subscribe with a callback function" do
44
+ a = 0
45
+ @queue.bind(@exchange)
46
+ @queue.subscribe do |v|
47
+ a += v.to_i
48
+ end
49
+ @queue.publish("1")
50
+ @queue.publish("2")
51
+ sleep 1
52
+ a.should == 3
53
+ end
54
+ end
55
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jerryluk-rabbitmq-jruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jerry Luk
@@ -21,8 +21,14 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files: []
23
23
 
24
- files: []
25
-
24
+ files:
25
+ - README
26
+ - MIT-LICENSE
27
+ - lib/commons-cli-1.1.jar
28
+ - lib/commons-io-1.2.jar
29
+ - lib/rabbitmq-client.jar
30
+ - lib/junit.jar
31
+ - lib/rabbitmq_client.rb
26
32
  has_rdoc: false
27
33
  homepage: http://www.linkedin.com/in/jerryluk
28
34
  post_install_message:
@@ -49,5 +55,5 @@ rubygems_version: 1.2.0
49
55
  signing_key:
50
56
  specification_version: 2
51
57
  summary: A RabbitMQ client for JRuby
52
- test_files: []
53
-
58
+ test_files:
59
+ - spec/rabbitmq_client_spec.rb