lib/client.rb in jsparrow-1.1.1 vs lib/client.rb in jsparrow-1.1.2
- old
+ new
@@ -7,67 +7,43 @@
#
# Cliente JMS que possibilita a conexao com o servidor de aplicacoes Java EE
# que prove o servico JMS.
#
class Client
- def initialize(connection_config, jndi_context_builder)
- @connection_config = connection_config
- @jndi_context_builder = jndi_context_builder
+ def initialize(connection)
+ @connection = connection
# Conexoes, filas, topicos, senders e receivers que serao habilitados
@connection_factories = {}
@queues = {}
@queue_senders = {}
@queue_receivers = {}
@topics = {}
@topic_senders = {}
@topic_receivers = {}
-
- # Foi iniciado?
- @started = false
end
def is_started?
- @started
+ @connection.is_opened?
end
def start
- raise InvalidClientStateError.new('started', 'start') if is_started?
+ @connection.open
- begin
- @jndi_context = @jndi_context_builder.build
- rescue => cause
- raise ClientInitializationError.new(@connection_config, cause)
- end
-
@connection_factories, @queues, @topics = lookup_resources
-
- @started = true
end
def is_stoped?
- !@started
+ @connection.is_closed?
end
def stop
- raise InvalidClientStateError.new('stoped', 'stop') if is_stoped?
-
- @jndi_context.close
-
- @started = false
+ @connection.close
end
- def queue_connection_factory_enabled?
- @connection_config.enabled_connection_factories.include?(:queue_connection_factory)
- end
-
- def queue_connection_factory
- @connection_factories[:queue_connection_factory]
- end
-
def queue_enabled?(queue_name)
- @connection_config.enabled_queues.include?(queue_name)
+ @connection.configuration.enabled_queues.include?(queue_name)
end
def queue(queue_name)
raise NameError, "Queue '#{queue_name}' does not exist." unless queue_enabled?(queue_name)
@@ -82,20 +58,12 @@
def queue_receiver(queue_name)
@queue_receivers[queue_name] ||=
Messaging::Receiver.new(queue_connection_factory, queue(queue_name))
end
- def topic_connection_factory_enabled?
- @connection_config.enabled_connection_factories.include?(:topic_connection_factory)
- end
-
- def topic_connection_factory
- @connection_factories[:topic_connection_factory]
- end
-
def topic_enabled?(topic_name)
- @connection_config.enabled_topics.include?(topic_name)
+ @connection.configuration.enabled_topics.include?(topic_name)
end
def topic(topic_name)
raise NameError, "Topic '#{topic_name}' does not exist." unless topic_enabled?(topic_name)
@@ -113,49 +81,23 @@
end
# -- Private methods -- #
private
- def lookup_resources
- @lookuped_connection_factories = lookup_resource(@connection_config.enabled_connection_factories)
- @lookuped_queues = lookup_resource(@connection_config.enabled_queues)
- @lookuped_topic = lookup_resource(@connection_config.enabled_topics)
-
- return @lookuped_connection_factories, @lookuped_queues, @lookuped_topic
+ def queue_connection_factory
+ @connection_factories[:queue_connection_factory]
end
- def lookup_resource(jndi_names = {})
- lookuped = {}
-
- return lookuped unless jndi_names
-
- jndi_names.each do |key, jndi_name|
- lookuped[key] = @jndi_context.lookup(jndi_name)
- end
-
- lookuped
+ def topic_connection_factory
+ @connection_factories[:topic_connection_factory]
end
- end
-
- class ClientInitializationError < StandardError
- attr_reader :config, :cause
- def initialize(config, cause)
- super("Could not open connection to server. Verify the config's config.")
-
- @config = config
- @cause = cause
- end
- end
-
- class InvalidClientStateError < StandardError
- attr_reader :state, :operation
-
- def initialize(state, operation)
- super("Could not did #{operation} because client is #{state}.")
-
- @state = state
- @operation = operation
- end
+ def lookup_resources
+ lookuped_connection_factories = @connection.lookup_resources(@connection.configuration.enabled_connection_factories)
+ lookuped_queues = @connection.lookup_resources(@connection.configuration.enabled_queues)
+ lookuped_topic = @connection.lookup_resources(@connection.configuration.enabled_topics)
+
+ return lookuped_connection_factories, lookuped_queues, lookuped_topic
+ end
end
end
end