Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add file to queue and queue to file examples

  • Loading branch information...
commit 672fc1e93489f3e9c5421baba9a461d1168021ca 1 parent 0ee7d04
@reidmorrison authored
View
10 HISTORY.md
@@ -1,3 +1,13 @@
+## 0.11.2 (2011-06-01)
+
+* Issue #8 Add ability to set Producer delivery mode using a Symbol
+* Include ActiveMQ InVM working example along with Log4J properties file
+
+## 0.11.1 (2011-05-25)
+
+* Fixes the condition where a bad session keeps being re-used in a session pool.
+ It is now removed from the pool anytime a JMS exception has occurred
+
## 0.11.0 (2011-04-18)
* Compatibility with JRuby 1.6
View
2  Rakefile
@@ -9,7 +9,7 @@ desc "Build gem"
task :gem do |t|
gemspec = Gem::Specification.new do |s|
s.name = 'jruby-jms'
- s.version = '0.11.0'
+ s.version = '0.11.2'
s.author = 'Reid Morrison'
s.email = 'rubywmq@gmail.com'
s.homepage = 'https://github.com/reidmorrison/jruby-jms'
View
51 examples/file-to-q/files_to_q.rb
@@ -0,0 +1,51 @@
+#
+# Example : files_to_q : Place all files in a directory to a queue
+# Each file is written as a separate message
+# Place the data in a file ending with '.data'
+# and the header information in a file with same name, but with an
+# extension of '.yml'
+#
+# jruby files_to_q.rb activemq my_queue
+#
+
+# Allow examples to be run in-place without requiring a gem install
+$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
+
+require 'rubygems'
+require 'jms'
+require 'yaml'
+
+raise("Required Parameters: 'jms_provider' 'queue_name' 'input_directory'") unless ARGV.count >= 2
+jms_provider = ARGV[0]
+queue_name = ARGV[1]
+path = ARGV[2] || queue_name
+
+# Load Connection parameters from configuration file
+config = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'jms.yml'))[jms_provider]
+raise "JMS Provider option:#{jms_provider} not found in jms.yml file" unless config
+
+counter = 0
+# Consume all available messages on the queue
+JMS::Connection.session(config) do |session|
+ session.producer(:queue_name => queue_name) do |producer|
+ Dir.glob(File.join(path,'*.data')) do |filename|
+ unless File.directory?(filename)
+ printf("%5d: #{filename}\n",counter = counter + 1)
+ data = File.open(filename, 'rb') {|file| file.read }
+ header_filename = File.join(File.dirname(filename), File.basename(filename))
+ header_filename = header_filename[0, header_filename.length - '.data'.length] + '.yml'
+ header = File.exist?(header_filename) ? YAML.load_file(header_filename) : nil
+ message = session.message(data, :bytes)
+ if header
+ header[:attributes].each_pair do |k,v|
+ next if k == :jms_destination
+ message.send("#{k}=".to_sym, v) if message.respond_to?("#{k}=".to_sym)
+ end if header[:attributes]
+ message.properties = header[:properties] || {}
+ end
+ producer.send(message)
+ end
+ end
+ end
+end
+puts "Read #{counter} messages from #{path} and wrote to #{queue_name}"
View
44 examples/file-to-q/q_to_files.rb
@@ -0,0 +1,44 @@
+#
+# Example: q_to_files:
+# Copy all messages in a queue to separate files in a directory
+# The messages are left on the queue by
+#
+# jruby q_to_files.rb activemq my_queue
+#
+
+# Allow examples to be run in-place without requiring a gem install
+$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
+
+require 'rubygems'
+require 'jms'
+require 'yaml'
+require 'fileutils'
+
+raise("Required Parameters: 'jms_provider' 'queue_name' 'output_directory'") unless ARGV.count >= 2
+jms_provider = ARGV[0]
+queue_name = ARGV[1]
+path = ARGV[2] || queue_name
+
+# Load Connection parameters from configuration file
+config = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'jms.yml'))[jms_provider]
+raise "JMS Provider option:#{jms_provider} not found in jms.yml file" unless config
+
+# Create supplied path if it does not exist
+FileUtils.mkdir_p(path)
+
+counter = 0
+# Consume all available messages on the queue
+JMS::Connection.session(config) do |session|
+ session.browse(:queue_name => queue_name, :timeout=>1000) do |message|
+ counter += 1
+ filename = File.join(path, "message_%03d" % counter)
+ File.open(filename+'.data', 'wb') {|file| file.write(message.data) }
+ header = {
+ :attributes => message.attributes,
+ :properties => message.properties
+ }
+ File.open(filename+'.yml', 'wb') {|file| file.write(header.to_yaml) }
+ end
+end
+
+puts "Saved #{counter} messages to #{path}"
View
44 examples/invm/invm.rb
@@ -0,0 +1,44 @@
+#
+# Sample ActiveMQ InVM Example:
+# Write to a queue and then consume the message in a separate thread
+#
+# Note: This example only works with ActiveMQ
+# Update the jar files path in ../jms.yml to point to your ActiveMQ installation
+
+# Allow examples to be run in-place without requiring a gem install
+$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
+
+require 'rubygems'
+require 'yaml'
+require 'jms'
+require 'benchmark'
+
+# Set Log4J properties file so that it does not need to be in the CLASSPATH
+java.lang.System.properties['log4j.configuration'] = "file://#{File.join(File.dirname(__FILE__), 'log4j.properties')}"
+
+jms_provider = 'activemq-invm'
+
+# Load Connection parameters from configuration file
+config = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'jms.yml'))[jms_provider]
+raise "JMS Provider option:#{jms_provider} not found in jms.yml file" unless config
+
+JMS::Connection.start(config) do |connection|
+ # Consume messages in a separate thread
+ connection.on_message(:queue_name => 'ExampleQueue') do |message|
+ JMS::logger.info "Consumed message from ExampleQueue: '#{message.data}'"
+ end
+
+ # Send a single message within a new session
+ connection.session do |session|
+ session.producer(:queue_name => 'ExampleQueue') do |producer|
+ producer.send(session.message("Hello World. #{Time.now}"))
+ end
+ end
+
+ JMS::logger.info "Put message on ExampleQueue"
+
+ # Give the consume thread time to process the message before terminating
+ sleep 1
+
+ JMS::logger.info "Shutting down"
+end
View
58 examples/invm/log4j.properties
@@ -0,0 +1,58 @@
+#
+# This file controls most of the logging in ActiveMQ which is mainly based around
+# the commons logging API.
+#
+log4j.rootLogger=INFO, logfile, console
+log4j.logger.org.apache.activemq.spring=WARN
+log4j.logger.org.apache.activemq.web.handler=WARN
+log4j.logger.org.springframework=WARN
+log4j.logger.org.apache.xbean=WARN
+log4j.logger.org.apache.camel=INFO
+
+# When debugging or reporting problems to the ActiveMQ team,
+# comment out the above lines and uncomment the next.
+
+#log4j.rootLogger=DEBUG, logfile, console
+
+# Or for more fine grained debug logging uncomment one of these
+#log4j.logger.org.apache.activemq=DEBUG
+#log4j.logger.org.apache.camel=DEBUG
+
+# Console appender
+log4j.appender.console=org.apache.log4j.ConsoleAppender
+log4j.appender.console.layout=org.apache.log4j.PatternLayout
+log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %-5p: %m [%c %t]%n
+log4j.appender.console.threshold=INFO
+
+# File appender
+log4j.appender.logfile=org.apache.log4j.RollingFileAppender
+log4j.appender.logfile.file=activemq.log
+log4j.appender.logfile.maxFileSize=10240KB
+log4j.appender.logfile.maxBackupIndex=5
+log4j.appender.logfile.append=true
+log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
+log4j.appender.logfile.layout.ConversionPattern=%d{ISO8601} %-5p: %m [%c %t]%n
+# use some of the following patterns to see MDC logging data
+#
+# %X{activemq.broker}
+# %X{activemq.connector}
+# %X{activemq.destination}
+#
+# e.g.
+#
+# log4j.appender.logfile.layout.ConversionPattern=%d | %-20.20X{activemq.connector} | %-5p | %m | %c | %t%n
+
+###########
+# Audit log
+###########
+
+log4j.additivity.org.apache.activemq.audit=false
+log4j.logger.org.apache.activemq.audit=INFO, audit
+
+log4j.appender.audit=org.apache.log4j.FileAppender
+log4j.appender.audit.file=activemq-audit.log
+log4j.appender.logfile.maxFileSize=10240KB
+log4j.appender.logfile.maxBackupIndex=5
+log4j.appender.audit.append=true
+log4j.appender.audit.layout=org.apache.log4j.PatternLayout
+log4j.appender.audit.layout.ConversionPattern=%d{ISO8601} %-5p: %m [%c %t]%n
View
20 examples/jms.yml
@@ -14,26 +14,28 @@ activemq:
:username: system
:password: manager
:require_jars:
- - ~/Applications/apache-activemq-5.4.2-fuse-03-09/activemq-all-5.4.2-fuse-03-09.jar
-# ActiveMQ 5.5 Requires additional jar files
-# - ~/Applications/apache-activemq-5.5.0/activemq-all-5.5.0.jar
-# - ~/Applications/apache-activemq-5.5.0/lib/optional/slf4j-log4j12-1.5.11.jar
-# - ~/Applications/apache-activemq-5.5.0/lib/optional/log4j-1.2.14.jar
+ - ~/Applications/apache-activemq-5.5.0/activemq-all-5.5.0.jar
+ - ~/Applications/apache-activemq-5.5.0/lib/optional/slf4j-log4j12-1.5.11.jar
+ - ~/Applications/apache-activemq-5.5.0/lib/optional/log4j-1.2.14.jar
# ActiveMQ In VM Broker (Supports messaging within a JVM instance)
-activemq-vm:
+activemq-invm:
:factory: org.apache.activemq.ActiveMQConnectionFactory
- :broker_url: vm://127.0.0.1
+ :broker_url: vm://mybroker
:object_message_serialization_defered: true
:require_jars:
- - ~/Applications/apache-activemq-5.4.2-fuse-03-09/activemq-all-5.4.2-fuse-03-09.jar
+ - ~/Applications/apache-activemq-5.5.0/activemq-all-5.5.0.jar
+ - ~/Applications/apache-activemq-5.5.0/lib/optional/slf4j-log4j12-1.5.11.jar
+ - ~/Applications/apache-activemq-5.5.0/lib/optional/log4j-1.2.14.jar
# ActiveMQ with failover to slave instance
activemq-ha:
:factory: org.apache.activemq.ActiveMQConnectionFactory
:broker_url: failover://(tcp://msg1:61616,tcp://msg2:61616)?randomize=false&timeout=30000&initialReconnectDelay=100&useExponentialBackOff=true
:require_jars:
- - ~/Applications/apache-activemq-5.4.2-fuse-03-09/activemq-all-5.4.2-fuse-03-09.jar
+ - ~/Applications/apache-activemq-5.5.0/activemq-all-5.5.0.jar
+ - ~/Applications/apache-activemq-5.5.0/lib/optional/slf4j-log4j12-1.5.11.jar
+ - ~/Applications/apache-activemq-5.5.0/lib/optional/log4j-1.2.14.jar
# JBoss 4 Messaging
:jndi_name: ConnectionFactory
View
2  examples/producer-consumer/producer.rb
@@ -18,7 +18,7 @@
JMS::Connection.session(config) do |session|
session.producer(:queue_name => 'ExampleQueue') do |producer|
- producer.delivery_mode = JMS::DeliveryMode::NON_PERSISTENT
+ producer.delivery_mode_sym = :non_persistent
producer.send(session.message("Hello World: #{Time.now}"))
JMS::logger.info "Successfully sent one message to queue ExampleQueue"
end
View
1  lib/jms/connection.rb
@@ -121,6 +121,7 @@ def fetch_dependencies(jar_list)
require 'jms/object_message'
require 'jms/session'
require 'jms/message_consumer'
+ require 'jms/message_producer'
require 'jms/queue_browser'
require 'jms/oracle_a_q_connection_factory'
end
View
4 lib/jms/message.rb
@@ -225,8 +225,8 @@ def jms_delivery_mode_sym=(mode)
def attributes
{
:jms_correlation_id => jms_correlation_id,
- :jms_delivery_mode => jms_delivery_mode_sym,
- :jms_destination => jms_destination,
+ :jms_delivery_mode_sym => jms_delivery_mode_sym,
+ :jms_destination => jms_destination.nil? ? nil : jms_destination.to_string,
:jms_expiration => jms_expiration,
:jms_message_id => jms_message_id,
:jms_priority => jms_priority,
View
59 lib/jms/message_producer.rb
@@ -0,0 +1,59 @@
+################################################################################
+# Copyright 2008, 2009, 2010, 2011 J. Reid Morrison
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+################################################################################
+
+# Extend JMS Message Producer Interface with Ruby methods
+#
+# For further help on javax.jms.Message
+# http://download.oracle.com/javaee/6/api/javax/jms/MessageProducer.html
+#
+# Interface javax.jms.Producer
+module JMS::MessageProducer
+
+ # Return the Delivery Mode as a Ruby symbol
+ # :persistent
+ # :non_persistent
+ # nil if unknown
+ def delivery_mode_sym
+ case delivery_mode
+ when JMS::DeliveryMode::PERSISTENT
+ :persistent
+ when JMS::DeliveryMode::NON_PERSISTENT
+ :non_persistent
+ else
+ nil
+ end
+ end
+
+ # Set the JMS Delivery Mode from a Ruby Symbol
+ # Valid values for mode
+ # :persistent
+ # :non_persistent
+ #
+ # Example:
+ # producer.delivery_mode_sym = :persistent
+ def delivery_mode_sym=(mode)
+ val = case mode
+ when :persistent
+ JMS::DeliveryMode::PERSISTENT
+ when :non_persistent
+ JMS::DeliveryMode::NON_PERSISTENT
+ else
+ raise "Unknown delivery mode symbol: #{mode}"
+ end
+ self.delivery_mode = val
+ end
+
+end
View
47 lib/jms/session.rb
@@ -115,16 +115,34 @@ module JMS::Session
# Duck typing is used to determine the type. If the class responds
# to :to_str then it is considered a String. Similarly if it responds to
# :each_pair it is considered to be a Hash
- def message(data)
+ #
+ # If automated duck typing is not desired, the type of the message can be specified
+ # by setting the parameter 'type' to any one of:
+ # :text => Creates a Text Message
+ # :map => Creates a Map Message
+ # :bytes => Creates a Bytes Message
+ def message(data, type=nil)
jms_message = nil
- if data.respond_to?(:to_str, false)
+ type ||= if data.respond_to?(:to_str, false)
+ :text
+ elsif data.respond_to?(:each_pair, false)
+ :map
+ else
+ raise "Unknown data type #{data.class.to_s} in Message"
+ end
+
+ case type
+ when :text
jms_message = self.createTextMessage
jms_message.text = data.to_str
- elsif data.respond_to?(:each_pair, false)
+ when :map
jms_message = self.createMapMessage
jms_message.data = data
+ when :bytes
+ jms_message = self.createBytesMessage
+ jms_message.write_bytes(data.to_java_bytes)
else
- raise "Unknown data type #{data.class.to_s} in Message"
+ raise "Invalid type #{type} requested"
end
jms_message
end
@@ -147,12 +165,18 @@ def message(data)
# To create a temporary queue:
# session.create_destination(:queue_name => :temporary)
#
+ # To create a queue:
+ # session.create_destination('queue://queue_name')
+ #
# To create a topic:
# session.create_destination(:topic_name => 'name of queue')
#
# To create a temporary topic:
# session.create_destination(:topic_name => :temporary)
#
+ # To create a topic:
+ # session.create_destination('topic://topic_name')
+ #
# Create the destination based on the parameter supplied
#
# Parameters:
@@ -171,9 +195,18 @@ def create_destination(params)
# Allow a Java JMS destination object to be passed in
return params[:destination] if params[:destination] && params[:destination].java_kind_of?(JMS::Destination)
- # :q_name is deprecated
- queue_name = params[:queue_name] || params[:q_name]
- topic_name = params[:topic_name]
+ queue_name = nil
+ topic_name = nil
+
+ if params.is_a? String
+ queue_name = params['queue://'.length..-1] if params.start_with?('queue://')
+ topic_name = params['topic://'.length..-1] if params.start_with?('topic://')
+ else
+ # :q_name is deprecated
+ queue_name = params[:queue_name] || params[:q_name]
+ topic_name = params[:topic_name]
+ end
+
raise "Missing mandatory parameter :queue_name or :topic_name to Session::producer, Session::consumer, or Session::browser" unless queue_name || topic_name
if queue_name
View
29 lib/jms/session_pool.rb
@@ -24,7 +24,7 @@ module JMS
# Default: 5.0
# :pool_logger Supply a logger that responds to #debug, #info, #warn and #debug?
# For example: Rails.logger
- # Default: None
+ # Default: JMS.logger
# Example:
# session_pool = connection.create_session_pool(config)
# session_pool.session do |session|
@@ -35,7 +35,7 @@ def initialize(connection, params={})
# Save Session params since it will be used every time a new session is
# created in the pool
session_params = params.nil? ? {} : params.dup
- logger = session_params[:pool_logger]
+ logger = session_params[:pool_logger] || JMS.logger
# Define how GenePool can create new sessions
@pool = GenePool.new(
:name => session_params[:pool_name] || self.class.name,
@@ -54,10 +54,22 @@ def initialize(connection, params={})
# Obtain a session from the pool and pass it to the supplied block
# The session is automatically returned to the pool once the block completes
+ #
+ # In the event a JMS Exception is thrown the session will be closed and removed
+ # from the pool to prevent re-using sessions that are no longer valid
def session(&block)
- #TODO Check if session is open?
- @pool.with_connection &block
- #TODO Catch connection failures and release from pool?
+ s = nil
+ begin
+ s = @pool.checkout
+ block.call(s)
+ rescue javax.jms.JMSException => e
+ s.close rescue nil
+ @pool.remove(s)
+ s = nil # Do not check back in since we have removed it
+ raise e
+ ensure
+ @pool.checkin(s) if s
+ end
end
# Obtain a session from the pool and create a MessageConsumer.
@@ -133,13 +145,16 @@ def producer(params, &block)
# Immediately Close all sessions in the pool and release from the pool
#
+ # Note: This is an immediate close, active sessions will be aborted
+ #
+ # Note: Once closed a session pool cannot be re-used. A new instance must
+ # be created
+ #
# TODO: Allow an option to wait for active sessions to be returned before
# closing
def close
@pool.each do |s|
- #@pool.remove(s)
s.close
- #@pool.remove(s)
end
end
View
3  test/connection_test.rb
@@ -7,6 +7,9 @@
require 'jms'
require 'yaml'
+# Set Log4J properties file so that it does not need to be in the CLASSPATH
+java.lang.System.properties['log4j.configuration'] = "test/log4j.properties"
+
class JMSTest < Test::Unit::TestCase
context 'JMS Connection' do
# Load configuration from jms.yml
View
4 test/jms.yml
@@ -13,7 +13,9 @@ activemq:
:factory: org.apache.activemq.ActiveMQConnectionFactory
:broker_url: tcp://localhost:61616
:require_jars:
- - ~/Applications/apache-activemq-5.4.2-fuse-03-09/activemq-all-5.4.2-fuse-03-09.jar
+ - ~/Applications/apache-activemq-5.5.0/activemq-all-5.5.0.jar
+ - ~/Applications/apache-activemq-5.5.0/lib/optional/slf4j-log4j12-1.5.11.jar
+ - ~/Applications/apache-activemq-5.5.0/lib/optional/log4j-1.2.14.jar
:queue_name: TestQueue
:topic_name: TestTopic
View
32 test/log4j.properties
@@ -0,0 +1,32 @@
+#
+# This file controls most of the logging in ActiveMQ which is mainly based around
+# the commons logging API.
+#
+log4j.rootLogger=INFO, console
+log4j.logger.org.apache.activemq.spring=WARN
+log4j.logger.org.apache.activemq.web.handler=WARN
+log4j.logger.org.springframework=WARN
+log4j.logger.org.apache.xbean=WARN
+log4j.logger.org.apache.camel=INFO
+
+# When debugging or reporting problems to the ActiveMQ team,
+# comment out the above lines and uncomment the next.
+
+#log4j.rootLogger=DEBUG, logfile, console
+
+# Or for more fine grained debug logging uncomment one of these
+#log4j.logger.org.apache.activemq=DEBUG
+#log4j.logger.org.apache.camel=DEBUG
+
+# Console appender
+log4j.appender.console=org.apache.log4j.ConsoleAppender
+log4j.appender.console.layout=org.apache.log4j.PatternLayout
+log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %-5p: %m [%c %t]%n
+log4j.appender.console.threshold=INFO
+
+log4j.additivity.org.apache.activemq.audit=false
+log4j.logger.org.apache.activemq.audit=INFO, audit
+
+log4j.appender.audit=org.apache.log4j.ConsoleAppender
+log4j.appender.audit.layout=org.apache.log4j.PatternLayout
+log4j.appender.audit.layout.ConversionPattern=%d{ISO8601} %-5p: %m [%c %t]%n
View
10 test/message_test.rb
@@ -31,7 +31,7 @@ class JMSTest < Test::Unit::TestCase
producer.send(session.message('Hello World'))
# Consume Message
- session.consume(:destination => producer.destination) do |message|
+ session.consume(:destination => producer.destination, :timeout=>1000) do |message|
assert_equal message.java_kind_of?(JMS::TextMessage), true
data = message.data
end
@@ -44,7 +44,7 @@ class JMSTest < Test::Unit::TestCase
JMS::Connection.session(@config) do |session|
assert_not_nil session
data = :timed_out
- browse_data = nil
+ browse_data = :timed_out
session.producer(:queue_name => @queue_name) do |producer|
# Send Message
producer.send(session.message('Hello World'))
@@ -56,7 +56,7 @@ class JMSTest < Test::Unit::TestCase
end
# Consume Message
- session.consume(:queue_name => @queue_name) do |message|
+ session.consume(:queue_name => @queue_name, :timeout=>1000) do |message|
assert_equal message.java_kind_of?(JMS::TextMessage), true
data = message.data
end
@@ -91,7 +91,7 @@ class JMSTest < Test::Unit::TestCase
producer.send(message)
# Consume Message
- session.consume(:destination => producer.destination) do |message|
+ session.consume(:destination => producer.destination, :timeout=>1000) do |message|
assert_equal message.java_kind_of?(JMS::TextMessage), true
data = message.data
#assert_equal :non_persistent, message.jms_delivery_mode
@@ -116,7 +116,7 @@ class JMSTest < Test::Unit::TestCase
producer.send(message)
# Consume Message
- session.consume(:destination => producer.destination) do |message|
+ session.consume(:destination => producer.destination, :timeout=>1000) do |message|
assert_equal message.java_kind_of?(JMS::TextMessage), true
data = message.data
assert_equal :persistent, message.jms_delivery_mode_sym
View
86 test/session_pool_test.rb
@@ -0,0 +1,86 @@
+# Allow examples to be run in-place without requiring a gem install
+$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
+
+require 'rubygems'
+require 'test/unit'
+require 'shoulda'
+require 'jms'
+require 'yaml'
+
+class JMSTest < Test::Unit::TestCase
+ context 'JMS Session' do
+ # Load configuration from jms.yml
+ setup do
+ # To change the JMS provider, edit jms.yml and change :default
+
+ # Load Connection parameters from configuration file
+ cfg = YAML.load_file(File.join(File.dirname(__FILE__), 'jms.yml'))
+ jms_provider = cfg['default']
+ @config = cfg[jms_provider]
+ raise "JMS Provider option:#{jms_provider} not found in jms.yml file" unless @config
+ @queue_name = @config.delete(:queue_name) || raise("Mandatory :queue_name missing from jms.yml")
+ @topic_name = @config.delete(:topic_name) || raise("Mandatory :topic_name missing from jms.yml")
+ @pool_params = {
+ :pool_name => 'Test::JMS::SessionPool',
+ :pool_size => 10,
+ :pool_warn_timeout => 5.0,
+ #:pool_logger =>
+ }
+ end
+
+ should 'create a session pool' do
+ JMS::Connection.start(@config) do |connection|
+ pool = connection.create_session_pool(@pool_params)
+ pool.session do |session|
+ assert_not_nil session
+ assert session.is_a?(javax.jms.Session)
+ end
+ pool.close
+ end
+ end
+
+ should 'remove bad session from pool' do
+ JMS::Connection.start(@config) do |connection|
+ pool = connection.create_session_pool(@pool_params.merge(:pool_size=>1))
+ s = nil
+ r = begin
+ pool.session do |session|
+ assert_not_nil session
+ assert session.is_a?(javax.jms.Session)
+ s = session
+ s.close
+ s.create_map_message
+ false
+ end
+ rescue javax.jms.IllegalStateException
+ true
+ end
+ assert r == true
+
+ # Now verify that the previous closed session was removed from the pool
+ pool.session do |session|
+ assert_not_nil session
+ assert session.is_a?(javax.jms.Session)
+ assert s != session
+ session.create_map_message
+ end
+ end
+ end
+
+ should 'allow multiple sessions to be used concurrently' do
+ JMS::Connection.start(@config) do |connection|
+ pool = connection.create_session_pool(@pool_params)
+ pool.session do |session|
+ assert_not_nil session
+ assert session.is_a?(javax.jms.Session)
+ pool.session do |session2|
+ assert_not_nil session2
+ assert session2.is_a?(javax.jms.Session)
+ assert session != session2
+ end
+ end
+ end
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.