jms4r 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *.jar
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Eric Monti
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,22 @@
1
+ = jms4r
2
+
3
+ A generalized JMS abstraction library for JRuby
4
+
5
+ == Synopsis
6
+
7
+ See samples/
8
+
9
+ == Note on Patches/Pull Requests
10
+
11
+ * Fork the project.
12
+ * Make your feature addition or bug fix.
13
+ * Add tests for it. This is important so I don't break it in a
14
+ future version unintentionally.
15
+ * Commit, do not mess with rakefile, version, or history.
16
+ (if you want to have your own version, that is fine but
17
+ bump version in a commit by itself I can ignore when I pull)
18
+ * Send me a pull request. Bonus points for topic branches.
19
+
20
+ == Copyright
21
+
22
+ Copyright (c) 2009 Eric Monti. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
9
+ gem.name = "jms4r"
10
+ gem.summary = %Q{A generalized JMS abstraction library for JRuby}
11
+ gem.description = %Q{A generalized JMS abstraction library for JRuby}
12
+ gem.email = "emonti@matasano.com"
13
+ gem.homepage = "http://github.com/emonti/jms4r"
14
+ gem.authors = ["Eric Monti"]
15
+ gem.platform = 'java'
16
+ end
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ if File.exist?('VERSION')
48
+ version = File.read('VERSION')
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "jms4r #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/jms4r.gemspec ADDED
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{jms4r}
8
+ s.version = "0.1.0"
9
+ s.platform = %q{java}
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Eric Monti"]
13
+ s.date = %q{2009-12-10}
14
+ s.description = %q{A generalized JMS abstraction library for JRuby}
15
+ s.email = %q{emonti@matasano.com}
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "jms4r.gemspec",
28
+ "lib/jms4r.rb",
29
+ "lib/jms4r/browser.rb",
30
+ "lib/jms4r/listener.rb",
31
+ "lib/jms4r/receiver.rb",
32
+ "lib/jms4r/sender.rb",
33
+ "lib/jms4r/session.rb",
34
+ "samples/activemq/activemq.rb",
35
+ "test/jms4r_test.rb",
36
+ "test/test_helper.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/emonti/jms4r}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.5}
42
+ s.summary = %q{A generalized JMS abstraction library for JRuby}
43
+ s.test_files = [
44
+ "test/jms4r_test.rb",
45
+ "test/test_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ else
54
+ end
55
+ else
56
+ end
57
+ end
data/lib/jms4r.rb ADDED
@@ -0,0 +1,12 @@
1
+ include Java
2
+
3
+ module JMS
4
+
5
+ end
6
+
7
+ require 'jms4r/session.rb'
8
+ require 'jms4r/receiver.rb'
9
+ require 'jms4r/browser.rb'
10
+ require 'jms4r/sender.rb'
11
+ require 'jms4r/listener.rb'
12
+
@@ -0,0 +1,27 @@
1
+ require 'enumerator'
2
+
3
+ module JMS
4
+ class Browser
5
+ attr_accessor :browser, :parent
6
+
7
+ def initialize(parent, qname, sel=nil)
8
+ @parent = parent
9
+ @sess = parent.sess
10
+ @browser = @sess.createBrowser(parent.queue(qname), sel)
11
+ end
12
+
13
+ def each_message
14
+ enum = @browser.getEnumeration()
15
+ yield(enum.nextElement) while enum.hasMoreElements
16
+ end
17
+
18
+ def messages
19
+ self.to_enum(:each_message).to_a
20
+ end
21
+ end
22
+
23
+ def method_missing(*args)
24
+ @browser.__send__(*args)
25
+ end
26
+ end
27
+
@@ -0,0 +1,30 @@
1
+
2
+ module JMS
3
+
4
+ # Stolen pretty much verbatim from the jmesnil-jms4r JBoss JMS implementation.
5
+ #
6
+ # This is a general purpose javax.jms.MessageListener implementation
7
+ # which just passes messages to a block in the 'onMessage' handler.
8
+ #
9
+ # Usage:
10
+ #
11
+ # require "pp"
12
+ # # assuming consumer is a java.jmx.MessageConsumer...
13
+ # consumer.message_listener = JMS::Listener.new do |msg|
14
+ # pp [:text_msg, msg.text] if msg.respond_to? :text
15
+ # end
16
+ #
17
+ class Listener
18
+ include javax.jms.MessageListener
19
+
20
+ def initialize(&handler)
21
+ @handler = handler
22
+ end
23
+
24
+ def on_message message
25
+ @handler.call(message)
26
+ end
27
+ end
28
+
29
+ end
30
+
@@ -0,0 +1,24 @@
1
+
2
+ module JMS
3
+ class Receiver
4
+ attr_accessor :receiver, :parent
5
+
6
+ def initialize(parent, qname, sel=nil)
7
+ @parent = parent
8
+ @sess = parent.sess
9
+ @receiver = @sess.createReceiver(parent.queue(qname), sel)
10
+ end
11
+
12
+ def while_receive(timeout)
13
+ while msg=@receiver.receive(timeout)
14
+ yield(msg)
15
+ end
16
+ end
17
+
18
+ def method_missing(*args)
19
+ @receiver.__send__ *args
20
+ end
21
+
22
+ end
23
+ end
24
+
@@ -0,0 +1,20 @@
1
+ module JMS
2
+ class Sender
3
+ attr_accessor :sender, :parent
4
+
5
+ def initialize(parent, qname)
6
+ @parent = parent
7
+ @sess = parent.sess
8
+ @sender = @sess.createSender(parent.queue(qname))
9
+ end
10
+
11
+ def send_message(msg)
12
+ @sender.send(@parent.create_msg(msg))
13
+ end
14
+
15
+ def method_missing(*args)
16
+ @sender.__send__(*args)
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,160 @@
1
+
2
+ module JMS
3
+ class AbstractSession
4
+ attr_reader :conn, :sess
5
+
6
+ # Creates a new instance of the implemented Session and calls the start
7
+ # method on the connection. Arguments are passed directly to
8
+ # 'init_session'. See init_session for more information.
9
+ #
10
+ # Optionally yields(self) to a block if one is given.
11
+ def initialize(*args)
12
+ self.init_session(*args)
13
+ yield(self) if block_given?
14
+ @conn.start()
15
+ end
16
+
17
+ # This is an abstract method and must be defined in implementing classes.
18
+ # This method is called from 'new' to establish the @conn and @sess
19
+ # connection/session instance variables before anything else.
20
+ #
21
+ # Don't call super()!
22
+ def init_session(*args)
23
+ raise NotImplementedError,
24
+ "#{self.class}.init_session is unimplemented or the abstract was "+
25
+ "called with super(). Override this in your implementation."
26
+ end
27
+
28
+ # You may override this if you wish to perform any additional teardown
29
+ # steps, but be sure to call super().
30
+ #
31
+ # The method may be called when instance variables are nil or already
32
+ # closed and must ensure a No-OP for these cases.
33
+ def close
34
+ @sess.close if @sess
35
+ @conn.close if @conn
36
+ end
37
+
38
+ # This method is called to resolve a queue name string to a Queue object
39
+ # suitable for passing as an argument to various JMS API functions.
40
+ #
41
+ # Some implementations may want to override this to return a Destination
42
+ # instead, depending on the underlying JMS implementation.
43
+ def queue(q)
44
+ if q.kind_of? javax.jms.Queue
45
+ q
46
+ else
47
+ @sess.createQueue(q)
48
+ end
49
+ end
50
+ alias create_queue queue
51
+
52
+ # This method is called to resolve a topic name string to a Topic object
53
+ # suitable for passing as an argument to various JMS API functions.
54
+ #
55
+ # Some implementations may want to override this to return a Destination
56
+ # instead, depending on the underlying JMS implementation.
57
+ def topic(t)
58
+ if t.kind_of? javax.jms.Topic
59
+ t
60
+ else
61
+ @sess.createTopic(tname)
62
+ end
63
+ end
64
+ alias create_topic topic
65
+
66
+ # Creates a JMS Sender for a given queue which is
67
+ # used to put messages on the queue.
68
+ #
69
+ # See also Sender
70
+ #
71
+ # Most implementations take only one argument:
72
+ # qname: The name of the queue.
73
+ def create_sender(*args)
74
+ Sender.new(self, *args)
75
+ end
76
+
77
+ # Creates a JMS Receiver for a given queue which is used to 'consume'
78
+ # or "receive and remove" messages from the queue.
79
+ #
80
+ # See also Receiver
81
+ #
82
+ # Most implementations take two arguments:
83
+ # qname: The name of the queue.
84
+ # sel: an optional 'message selector' string.
85
+ def create_receiver(*args)
86
+ r=Receiver.new(self, *args)
87
+ if block_given?
88
+ r.message_listener = Listener.new {|msg| yield(msg)}
89
+ end
90
+ return r
91
+ end
92
+
93
+ # Creates a JMS Receiver for a given queue.
94
+ #
95
+ # See also Browser
96
+ #
97
+ # Most implementations take two arguments:
98
+ # qname: The name of the queue.
99
+ # sel: an optional 'message selector' string.
100
+ def create_browser(*args)
101
+ Browser.new(self, *args)
102
+ end
103
+
104
+ def create_listener(&block)
105
+ Listener.new(&block)
106
+ end
107
+
108
+ # Attempts to compose a populated JMS message typed based on msg type.
109
+ # Returns the message after it has been created (and populated with msg
110
+ # data). In some cases, you may want finer grained control, in which case,
111
+ # use the JMS @sess object directly to compose messages instead.
112
+ #
113
+ # * If msg is nil, an empty 'Message' containing only headers is returned.
114
+ # * If msg is a kind of javax.jms.Message, it is returned.
115
+ # * Pass a Ruby String, a TextMessage is returned.
116
+ # * For a ByteMessage, either pass a Java::byte[] object directly or
117
+ # {:bytes => [...]}. A ByteMessage is returned.
118
+ # * Pass a Ruby Hash a MapMessage is returned.
119
+ # * For all other objects, if they are java.io.Serializable, an
120
+ # ObjectMessage is returned.
121
+ #
122
+ # In all other cases, an ArgumentError will be raised.
123
+ def create_msg(msg=nil)
124
+ m=nil
125
+
126
+ case msg
127
+ when javax.jms.Message
128
+ return msg
129
+ when nil
130
+ m = @sess.createMessage()
131
+
132
+ when String
133
+ m = @sess.createTextMessage(msg)
134
+
135
+ when ( msg.kind_of?(Java::byte[]) or
136
+ (msg.kind_of?(Hash) and msg.keys == [:bytes]) )
137
+ m = @sess.createBytesMessage
138
+ if b.kind_of? Java::byte[]
139
+ m.readBytes(b)
140
+ elsif (bs=b[:bytes]).kind_of? Array
141
+ m.readBytes(bs.to_java(:byte))
142
+ else
143
+ raise(ArgumentError, "invalid argument for java byte[] array")
144
+ end
145
+
146
+ when Hash
147
+ m = @sess.createMapMessage
148
+ msg.each {|k,v| m.setObject(k.to_s, v)}
149
+
150
+ when msg.kind_of?(java.io.Serializable)
151
+ m = @sess.createObjectMessage(msg)
152
+ else
153
+ raise(ArgumentError, "Can't handle #{msg.class} msg objects")
154
+ end
155
+ return m
156
+ end
157
+ end
158
+
159
+ end
160
+
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ Dir['jars/*.jar'].each {|x| require(x) }
4
+
5
+ require 'jms4r'
6
+
7
+ module ActiveMQ
8
+ include JMS
9
+
10
+ class Session < AbstractSession
11
+
12
+ attr_accessor :ctx
13
+
14
+ def init_session(host, port, env = nil)
15
+ e = java.util.Hashtable.new
16
+ e.put( javax.naming.Context.INITIAL_CONTEXT_FACTORY,
17
+ 'org.apache.activemq.jndi.ActiveMQInitialContextFactory' )
18
+ e.put( javax.naming.Context.PROVIDER_URL,
19
+ "tcp://#{host}:#{port}")
20
+
21
+ env.each {|k,v| e.put(k, v)} if env
22
+
23
+ @ctx = javax.naming.InitialContext.new(e)
24
+ @factory = ctx.lookup("ConnectionFactory")
25
+ @conn = @factory.createConnection()
26
+ @sess = @conn.createSession(false, javax.jms.Session::AUTO_ACKNOWLEDGE)
27
+ end
28
+
29
+ def lookup(n)
30
+ @ctx.lookup(n)
31
+ end
32
+
33
+ def list(n)
34
+ @ctx.list(n).to_a
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+ if __FILE__ == $0
41
+ require 'irb'
42
+ @rcv, @snd, @brws = nil
43
+ @sess = ActiveMQ::Session.new('192.168.11.70', 61616) do |this|
44
+ qname = "loopback_test"
45
+ @snd = this.create_sender(qname)
46
+ @brws = this.create_browser(qname)
47
+ @rcv = this.create_receiver(qname)
48
+ end
49
+
50
+ IRB.start
51
+
52
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class Jms4rTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'jms4r'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jms4r
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: java
6
+ authors:
7
+ - Eric Monti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-10 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A generalized JMS abstraction library for JRuby
17
+ email: emonti@matasano.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - jms4r.gemspec
33
+ - lib/jms4r.rb
34
+ - lib/jms4r/browser.rb
35
+ - lib/jms4r/listener.rb
36
+ - lib/jms4r/receiver.rb
37
+ - lib/jms4r/sender.rb
38
+ - lib/jms4r/session.rb
39
+ - samples/activemq/activemq.rb
40
+ - test/jms4r_test.rb
41
+ - test/test_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/emonti/jms4r
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: A generalized JMS abstraction library for JRuby
70
+ test_files:
71
+ - test/jms4r_test.rb
72
+ - test/test_helper.rb