Skip to content

Instantly share code, notes, and snippets.

@alanrrb
Created February 28, 2010 18:37
Show Gist options
  • Save alanrrb/317717 to your computer and use it in GitHub Desktop.
Save alanrrb/317717 to your computer and use it in GitHub Desktop.
# adicionando a gem
require 'rubygems'
require 'jsparrow'
# capturando os parametros de linha
$NOME = ARGV[0] if ARGV[0]
# parametrizando a conexao com o provider
JSparrow::Connection.configure do |connection|
connection.use_jms_client_jar '/Users/alan/Oracle/Middleware/wlserver_10.3/server/lib/weblogic.jar'
connection.use_jndi_properties :initial_context_factory => 'weblogic.jndi.WLInitialContextFactory',
:provider_url => 't3://localhost:7001',
:security_principal => 'weblogic',
:security_credentials => 'weblogic123'
connection.enable_connection_factories :topic_connection_factory => 'ConnectionFactory'
connection.enable_topics :chat_topic => 'MyTopic'
end
# iniciando a conexao
jms_client = JSparrow::Connection.new_client
jms_client.start
# criando o sender
topic_sender = jms_client.topic_sender(:chat_topic)
# implementando o Listener
class ChatTopicListener < JSparrow::Connection::Listener
listen_to :topic => :chat_topic
receive_only_in_criteria :selector => "recipient = 'jsparrow-example' and to_listener = 'ChatTopicListener'"
def on_receive_message(received_message)
puts "#{received_message.text}"
end
end
# registrando o listener e iniciando.
listener = JSparrow::Connection.new_listener :as => ChatTopicListener
listener.start_listening
# enviando as mensagens
texto = ""
while texto != "exit"
print "Escreva seu texto: "
texto = STDIN.gets.strip
mensagem = texto
mensagem = "#{$NOME} escreveu: #{mensagem}" if $NOME
topic_sender.send_text_message(mensagem) do |msg|
msg.set_string_property('recipient', 'jsparrow-example')
msg.set_string_property('to_listener', 'ChatTopicListener')
end
end
# finalizando o listener e parando o client
listener.stop_listening
jms_client.stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment