jms4r 0.1.0-java → 0.2.0-java
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/VERSION +1 -1
- data/jms4r.gemspec +4 -2
- data/lib/jms4r.rb +2 -2
- data/lib/jms4r/message.rb +47 -0
- data/lib/jms4r/receiver.rb +14 -2
- data/lib/jms4r/session.rb +8 -0
- data/samples/activemq/activemq.rb +16 -3
- data/samples/activemq/amq_slurp +86 -0
- metadata +4 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/jms4r.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{jms4r}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
s.platform = %q{java}
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.authors = ["Eric Monti"]
|
13
|
-
s.date = %q{2009-12-
|
13
|
+
s.date = %q{2009-12-23}
|
14
14
|
s.description = %q{A generalized JMS abstraction library for JRuby}
|
15
15
|
s.email = %q{emonti@matasano.com}
|
16
16
|
s.extra_rdoc_files = [
|
@@ -28,10 +28,12 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/jms4r.rb",
|
29
29
|
"lib/jms4r/browser.rb",
|
30
30
|
"lib/jms4r/listener.rb",
|
31
|
+
"lib/jms4r/message.rb",
|
31
32
|
"lib/jms4r/receiver.rb",
|
32
33
|
"lib/jms4r/sender.rb",
|
33
34
|
"lib/jms4r/session.rb",
|
34
35
|
"samples/activemq/activemq.rb",
|
36
|
+
"samples/activemq/amq_slurp",
|
35
37
|
"test/jms4r_test.rb",
|
36
38
|
"test/test_helper.rb"
|
37
39
|
]
|
data/lib/jms4r.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
module JMS
|
3
|
+
include_class 'javax.jms.Message'
|
4
|
+
|
5
|
+
module Message
|
6
|
+
def props
|
7
|
+
@props ||= Properties.new(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
class Properties
|
11
|
+
include Enumerable
|
12
|
+
|
13
|
+
def initialize(msg)
|
14
|
+
@msg = msg
|
15
|
+
end
|
16
|
+
|
17
|
+
def each_key(&block)
|
18
|
+
@msg.getPropertyNames().each &block
|
19
|
+
end
|
20
|
+
|
21
|
+
def <=> (a,b)
|
22
|
+
a[0] <=> b[0]
|
23
|
+
end
|
24
|
+
|
25
|
+
def each(&block)
|
26
|
+
each_key {|k| yield(k, @msg.getObjectProperty(k)) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def keys
|
30
|
+
@msg.getPropertyNames().to_a
|
31
|
+
end
|
32
|
+
|
33
|
+
def [](key)
|
34
|
+
@msg.getObjectProperty(key)
|
35
|
+
end
|
36
|
+
|
37
|
+
def []=(k, v)
|
38
|
+
@msg.setObjectProperty(k, v)
|
39
|
+
end
|
40
|
+
|
41
|
+
def size
|
42
|
+
keys.size
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/lib/jms4r/receiver.rb
CHANGED
@@ -9,12 +9,24 @@ module JMS
|
|
9
9
|
@receiver = @sess.createReceiver(parent.queue(qname), sel)
|
10
10
|
end
|
11
11
|
|
12
|
-
def while_receive(timeout)
|
13
|
-
|
12
|
+
def while_receive(timeout=nil)
|
13
|
+
prc =
|
14
|
+
if(timeout == 0)
|
15
|
+
lambda {|t| @receiver.receive() }
|
16
|
+
elsif timeout.nil?
|
17
|
+
lambda {|t| @receiver.receiveNoWait() }
|
18
|
+
else
|
19
|
+
lambda {|t| @receiver.receive(t) }
|
20
|
+
end
|
21
|
+
while msg=prc.call(timeout)
|
14
22
|
yield(msg)
|
15
23
|
end
|
16
24
|
end
|
17
25
|
|
26
|
+
def listen(&blk)
|
27
|
+
@receiver.setMessageListener( Listener.new(&blk) )
|
28
|
+
end
|
29
|
+
|
18
30
|
def method_missing(*args)
|
19
31
|
@receiver.__send__ *args
|
20
32
|
end
|
data/lib/jms4r/session.rb
CHANGED
@@ -47,6 +47,7 @@ module JMS
|
|
47
47
|
@sess.createQueue(q)
|
48
48
|
end
|
49
49
|
end
|
50
|
+
|
50
51
|
alias create_queue queue
|
51
52
|
|
52
53
|
# This method is called to resolve a topic name string to a Topic object
|
@@ -61,6 +62,7 @@ module JMS
|
|
61
62
|
@sess.createTopic(tname)
|
62
63
|
end
|
63
64
|
end
|
65
|
+
|
64
66
|
alias create_topic topic
|
65
67
|
|
66
68
|
# Creates a JMS Sender for a given queue which is
|
@@ -73,6 +75,7 @@ module JMS
|
|
73
75
|
def create_sender(*args)
|
74
76
|
Sender.new(self, *args)
|
75
77
|
end
|
78
|
+
alias sender create_sender
|
76
79
|
|
77
80
|
# Creates a JMS Receiver for a given queue which is used to 'consume'
|
78
81
|
# or "receive and remove" messages from the queue.
|
@@ -89,6 +92,7 @@ module JMS
|
|
89
92
|
end
|
90
93
|
return r
|
91
94
|
end
|
95
|
+
alias receiver create_receiver
|
92
96
|
|
93
97
|
# Creates a JMS Receiver for a given queue.
|
94
98
|
#
|
@@ -101,10 +105,14 @@ module JMS
|
|
101
105
|
Browser.new(self, *args)
|
102
106
|
end
|
103
107
|
|
108
|
+
alias browser create_browser
|
109
|
+
|
104
110
|
def create_listener(&block)
|
105
111
|
Listener.new(&block)
|
106
112
|
end
|
107
113
|
|
114
|
+
alias listener create_listener
|
115
|
+
|
108
116
|
# Attempts to compose a populated JMS message typed based on msg type.
|
109
117
|
# Returns the message after it has been created (and populated with msg
|
110
118
|
# data). In some cases, you may want finer grained control, in which case,
|
@@ -21,7 +21,7 @@ module ActiveMQ
|
|
21
21
|
env.each {|k,v| e.put(k, v)} if env
|
22
22
|
|
23
23
|
@ctx = javax.naming.InitialContext.new(e)
|
24
|
-
@factory = ctx.lookup("ConnectionFactory")
|
24
|
+
@factory = @ctx.lookup("ConnectionFactory")
|
25
25
|
@conn = @factory.createConnection()
|
26
26
|
@sess = @conn.createSession(false, javax.jms.Session::AUTO_ACKNOWLEDGE)
|
27
27
|
end
|
@@ -39,14 +39,27 @@ end
|
|
39
39
|
|
40
40
|
if __FILE__ == $0
|
41
41
|
require 'irb'
|
42
|
+
|
43
|
+
unless host=ARGV.shift
|
44
|
+
STDERR.puts "Usage: #{File.basename $0} host [port] [queue]"
|
45
|
+
exit 1
|
46
|
+
end
|
47
|
+
|
48
|
+
port = ARGV.shift || 61616
|
49
|
+
qname = ARGV.shift || 'loopback_test'
|
50
|
+
|
42
51
|
@rcv, @snd, @brws = nil
|
43
|
-
@sess = ActiveMQ::Session.new('192.168.11.70',
|
44
|
-
qname = "loopback_test"
|
52
|
+
@sess = ActiveMQ::Session.new('192.168.11.70', port.to_i) do |this|
|
45
53
|
@snd = this.create_sender(qname)
|
46
54
|
@brws = this.create_browser(qname)
|
47
55
|
@rcv = this.create_receiver(qname)
|
48
56
|
end
|
49
57
|
|
58
|
+
STDERR.puts "@sess = #{@sess.inspect}",
|
59
|
+
"@rcv = #{@rcv.inspect}",
|
60
|
+
"@snd = #{@snd.inspect}",
|
61
|
+
"@brws= #{@brws.inspect}"
|
50
62
|
IRB.start
|
51
63
|
|
52
64
|
end
|
65
|
+
|
@@ -0,0 +1,86 @@
|
|
1
|
+
#!/usr/bin/env jruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'activemq'
|
5
|
+
|
6
|
+
$consume = nil
|
7
|
+
$save=nil
|
8
|
+
$loop=nil
|
9
|
+
$list=nil
|
10
|
+
|
11
|
+
opts = OptionParser.new do |o|
|
12
|
+
o.banner = "Usage: #{File.basename $0} [options] url qname"
|
13
|
+
|
14
|
+
o.on_tail("-h", "--help", "Show this message") do
|
15
|
+
raise o.to_s
|
16
|
+
end
|
17
|
+
|
18
|
+
o.on("-c", "--[no-]consume", "Consume or just browse messages.",
|
19
|
+
" (Default: browse)") { |c| $consume = c }
|
20
|
+
|
21
|
+
o.on("-s", "--save=PATH", "Save serialized messages to PATH.",
|
22
|
+
" (named with put time as an id)") {|s| $save = s }
|
23
|
+
|
24
|
+
o.on("-l", "--[no-]loop", "Loops endlessly consuming messages") {|l| $loop=l }
|
25
|
+
o.on("-D", "--dump", "Lists queues and exits",
|
26
|
+
" (not implemented)") { $list=true }
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
begin
|
31
|
+
opts.parse!(ARGV)
|
32
|
+
raise opts.banner unless \
|
33
|
+
url=ARGV.shift and
|
34
|
+
($list or qname=ARGV.shift) and
|
35
|
+
ARGV.shift.nil?
|
36
|
+
rescue
|
37
|
+
STDERR.puts $!
|
38
|
+
exit 1
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
# define a block method with which to slurp messages
|
43
|
+
def slurp(msg)
|
44
|
+
if $save
|
45
|
+
tstamp = (msg.props["MESSAGE_ID"] || Time.now.to_i)
|
46
|
+
fo = java.io.FileOutputStream.new(File.join($save, "#{tstamp}.ser"))
|
47
|
+
oo = java.io.ObjectOutputStream.new(fo)
|
48
|
+
oo.writeObject(msg)
|
49
|
+
oo.close
|
50
|
+
fo.close
|
51
|
+
end
|
52
|
+
|
53
|
+
puts "-="*30 + "-"
|
54
|
+
puts msg, msg.props.sort.map {|k,v| " #{k} = #{v}"}
|
55
|
+
puts "[*] Full Text:", msg.text if msg.respond_to?(:text)
|
56
|
+
puts
|
57
|
+
end
|
58
|
+
|
59
|
+
begin
|
60
|
+
# create our connection
|
61
|
+
sess = ActiveMQ::Session.new(url)
|
62
|
+
|
63
|
+
# lookup the queue name and get a 'destination'
|
64
|
+
queue = sess.lookup(qname)
|
65
|
+
rescue javax.naming.NameNotFoundException
|
66
|
+
sess.close if sess
|
67
|
+
STDERR.puts "Error: #{$!}"
|
68
|
+
exit 1
|
69
|
+
end
|
70
|
+
|
71
|
+
rcvr = sess.create_receiver(queue)
|
72
|
+
brws = sess.create_browser(queue)
|
73
|
+
|
74
|
+
# either consume or browse all messages in the queue
|
75
|
+
if $loop
|
76
|
+
puts "[*] Waiting for Messages (#{brws.messages.size} in queue):"
|
77
|
+
rcvr.while_receive 0, &method(:slurp)
|
78
|
+
elsif $consume
|
79
|
+
puts "[*] Consuming #{brws.messages.size} Messages:"
|
80
|
+
rcvr.while_receive nil, &method(:slurp)
|
81
|
+
else
|
82
|
+
puts "[*] Browsing #{brws.messages.size} Messages:"
|
83
|
+
brws.each_message &method(:slurp)
|
84
|
+
end
|
85
|
+
|
86
|
+
sess.close
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jms4r
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Eric Monti
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-23 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -33,10 +33,12 @@ files:
|
|
33
33
|
- lib/jms4r.rb
|
34
34
|
- lib/jms4r/browser.rb
|
35
35
|
- lib/jms4r/listener.rb
|
36
|
+
- lib/jms4r/message.rb
|
36
37
|
- lib/jms4r/receiver.rb
|
37
38
|
- lib/jms4r/sender.rb
|
38
39
|
- lib/jms4r/session.rb
|
39
40
|
- samples/activemq/activemq.rb
|
41
|
+
- samples/activemq/amq_slurp
|
40
42
|
- test/jms4r_test.rb
|
41
43
|
- test/test_helper.rb
|
42
44
|
has_rdoc: true
|