jruby-jms 0.11.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.11.1 (2011-05-25)
2
+
3
+ * Fixes the condition where a bad session keeps being re-used in a session pool.
4
+ It is now removed from the pool anytime a JMS exception has occurred
5
+
1
6
  ## 0.11.0 (2011-04-18)
2
7
 
3
8
  * Compatibility with JRuby 1.6
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ desc "Build gem"
9
9
  task :gem do |t|
10
10
  gemspec = Gem::Specification.new do |s|
11
11
  s.name = 'jruby-jms'
12
- s.version = '0.11.0'
12
+ s.version = '0.11.1'
13
13
  s.author = 'Reid Morrison'
14
14
  s.email = 'rubywmq@gmail.com'
15
15
  s.homepage = 'https://github.com/reidmorrison/jruby-jms'
@@ -24,7 +24,7 @@ module JMS
24
24
  # Default: 5.0
25
25
  # :pool_logger Supply a logger that responds to #debug, #info, #warn and #debug?
26
26
  # For example: Rails.logger
27
- # Default: None
27
+ # Default: JMS.logger
28
28
  # Example:
29
29
  # session_pool = connection.create_session_pool(config)
30
30
  # session_pool.session do |session|
@@ -35,7 +35,7 @@ module JMS
35
35
  # Save Session params since it will be used every time a new session is
36
36
  # created in the pool
37
37
  session_params = params.nil? ? {} : params.dup
38
- logger = session_params[:pool_logger]
38
+ logger = session_params[:pool_logger] || JMS.logger
39
39
  # Define how GenePool can create new sessions
40
40
  @pool = GenePool.new(
41
41
  :name => session_params[:pool_name] || self.class.name,
@@ -54,10 +54,22 @@ module JMS
54
54
 
55
55
  # Obtain a session from the pool and pass it to the supplied block
56
56
  # The session is automatically returned to the pool once the block completes
57
+ #
58
+ # In the event a JMS Exception is thrown the session will be closed and removed
59
+ # from the pool to prevent re-using sessions that are no longer valid
57
60
  def session(&block)
58
- #TODO Check if session is open?
59
- @pool.with_connection &block
60
- #TODO Catch connection failures and release from pool?
61
+ s = nil
62
+ begin
63
+ s = @pool.checkout
64
+ block.call(s)
65
+ rescue javax.jms.JMSException => e
66
+ s.close rescue nil
67
+ @pool.remove(s)
68
+ s = nil # Do not check back in since we have removed it
69
+ raise e
70
+ ensure
71
+ @pool.checkin(s) if s
72
+ end
61
73
  end
62
74
 
63
75
  # Obtain a session from the pool and create a MessageConsumer.
@@ -133,13 +145,16 @@ module JMS
133
145
 
134
146
  # Immediately Close all sessions in the pool and release from the pool
135
147
  #
148
+ # Note: This is an immediate close, active sessions will be aborted
149
+ #
150
+ # Note: Once closed a session pool cannot be re-used. A new instance must
151
+ # be created
152
+ #
136
153
  # TODO: Allow an option to wait for active sessions to be returned before
137
154
  # closing
138
155
  def close
139
156
  @pool.each do |s|
140
- #@pool.remove(s)
141
157
  s.close
142
- #@pool.remove(s)
143
158
  end
144
159
  end
145
160
 
data/test/message_test.rb CHANGED
@@ -31,7 +31,7 @@ class JMSTest < Test::Unit::TestCase
31
31
  producer.send(session.message('Hello World'))
32
32
 
33
33
  # Consume Message
34
- session.consume(:destination => producer.destination) do |message|
34
+ session.consume(:destination => producer.destination, :timeout=>1000) do |message|
35
35
  assert_equal message.java_kind_of?(JMS::TextMessage), true
36
36
  data = message.data
37
37
  end
@@ -44,7 +44,7 @@ class JMSTest < Test::Unit::TestCase
44
44
  JMS::Connection.session(@config) do |session|
45
45
  assert_not_nil session
46
46
  data = :timed_out
47
- browse_data = nil
47
+ browse_data = :timed_out
48
48
  session.producer(:queue_name => @queue_name) do |producer|
49
49
  # Send Message
50
50
  producer.send(session.message('Hello World'))
@@ -56,7 +56,7 @@ class JMSTest < Test::Unit::TestCase
56
56
  end
57
57
 
58
58
  # Consume Message
59
- session.consume(:queue_name => @queue_name) do |message|
59
+ session.consume(:queue_name => @queue_name, :timeout=>1000) do |message|
60
60
  assert_equal message.java_kind_of?(JMS::TextMessage), true
61
61
  data = message.data
62
62
  end
@@ -91,7 +91,7 @@ class JMSTest < Test::Unit::TestCase
91
91
  producer.send(message)
92
92
 
93
93
  # Consume Message
94
- session.consume(:destination => producer.destination) do |message|
94
+ session.consume(:destination => producer.destination, :timeout=>1000) do |message|
95
95
  assert_equal message.java_kind_of?(JMS::TextMessage), true
96
96
  data = message.data
97
97
  #assert_equal :non_persistent, message.jms_delivery_mode
@@ -116,7 +116,7 @@ class JMSTest < Test::Unit::TestCase
116
116
  producer.send(message)
117
117
 
118
118
  # Consume Message
119
- session.consume(:destination => producer.destination) do |message|
119
+ session.consume(:destination => producer.destination, :timeout=>1000) do |message|
120
120
  assert_equal message.java_kind_of?(JMS::TextMessage), true
121
121
  data = message.data
122
122
  assert_equal :persistent, message.jms_delivery_mode_sym
@@ -0,0 +1,86 @@
1
+ # Allow examples to be run in-place without requiring a gem install
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+ require 'jms'
8
+ require 'yaml'
9
+
10
+ class JMSTest < Test::Unit::TestCase
11
+ context 'JMS Session' do
12
+ # Load configuration from jms.yml
13
+ setup do
14
+ # To change the JMS provider, edit jms.yml and change :default
15
+
16
+ # Load Connection parameters from configuration file
17
+ cfg = YAML.load_file(File.join(File.dirname(__FILE__), 'jms.yml'))
18
+ jms_provider = cfg['default']
19
+ @config = cfg[jms_provider]
20
+ raise "JMS Provider option:#{jms_provider} not found in jms.yml file" unless @config
21
+ @queue_name = @config.delete(:queue_name) || raise("Mandatory :queue_name missing from jms.yml")
22
+ @topic_name = @config.delete(:topic_name) || raise("Mandatory :topic_name missing from jms.yml")
23
+ @pool_params = {
24
+ :pool_name => 'Test::JMS::SessionPool',
25
+ :pool_size => 10,
26
+ :pool_warn_timeout => 5.0,
27
+ #:pool_logger =>
28
+ }
29
+ end
30
+
31
+ should 'create a session pool' do
32
+ JMS::Connection.start(@config) do |connection|
33
+ pool = connection.create_session_pool(@pool_params)
34
+ pool.session do |session|
35
+ assert_not_nil session
36
+ assert session.is_a?(javax.jms.Session)
37
+ end
38
+ pool.close
39
+ end
40
+ end
41
+
42
+ should 'remove bad session from pool' do
43
+ JMS::Connection.start(@config) do |connection|
44
+ pool = connection.create_session_pool(@pool_params.merge(:pool_size=>1))
45
+ s = nil
46
+ r = begin
47
+ pool.session do |session|
48
+ assert_not_nil session
49
+ assert session.is_a?(javax.jms.Session)
50
+ s = session
51
+ s.close
52
+ s.create_map_message
53
+ false
54
+ end
55
+ rescue javax.jms.IllegalStateException
56
+ true
57
+ end
58
+ assert r == true
59
+
60
+ # Now verify that the previous closed session was removed from the pool
61
+ pool.session do |session|
62
+ assert_not_nil session
63
+ assert session.is_a?(javax.jms.Session)
64
+ assert s != session
65
+ session.create_map_message
66
+ end
67
+ end
68
+ end
69
+
70
+ should 'allow multiple sessions to be used concurrently' do
71
+ JMS::Connection.start(@config) do |connection|
72
+ pool = connection.create_session_pool(@pool_params)
73
+ pool.session do |session|
74
+ assert_not_nil session
75
+ assert session.is_a?(javax.jms.Session)
76
+ pool.session do |session2|
77
+ assert_not_nil session2
78
+ assert session2.is_a?(javax.jms.Session)
79
+ assert session != session2
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ end
86
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jruby-jms
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.11.0
5
+ version: 0.11.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Reid Morrison
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-19 00:00:00 -04:00
13
+ date: 2011-05-25 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -92,6 +92,7 @@ files:
92
92
  - test/connection_test.rb
93
93
  - test/jms.yml
94
94
  - test/message_test.rb
95
+ - test/session_pool_test.rb
95
96
  - test/session_test.rb
96
97
  has_rdoc: true
97
98
  homepage: https://github.com/reidmorrison/jruby-jms