4 # stompclient: Generic client to consume and produce STOMP queues and topics, tested against
8 # A simple client that can connect to an STOMP server, subscribe to topics and queues and also
9 # send to topics and queues.
12 # stompclient [OPTIONS]
18 # The server to connect to, can also be set in STOMP_SERVER environment variable
21 # The port to connect to, default to 6163
24 # The user to connect as, can also be set in STOMP_USER environment variable
27 # The password to use, can also be set in STOMP_PASSWORD environment variable
29 # When connected to a server, use the 'help' command to see further information about
30 # using the client, common commands that can be issued are:
32 # - subscribe /topic/foo: Subscribes to topic 'foo'
33 # - /topic/foo bar: Sends 'bar' to the topic 'foo'
34 # - details: Toggle the display or timestamp and topic or queue information for each message
38 # - 20 December 2009 Include into MCollective
39 # - 17 March 2009 Initial release
41 # R.I.Pienaar <rip@devco.net> more information at www.devco.net
43 # Licensed under the Apache License, Version 2.0
51 opts = GetoptLong.new(
52 [ '--server', '-s', GetoptLong::REQUIRED_ARGUMENT],
53 [ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT],
54 [ '--user', '-u', GetoptLong::REQUIRED_ARGUMENT],
55 [ '--password', '-P', GetoptLong::REQUIRED_ARGUMENT],
56 [ '--help', '-h', GetoptLong::NO_ARGUMENT]
59 @user = ENV["STOMP_USER"];
60 @password = ENV["STOMP_PASSWORD"]
61 @server = ENV["STOMP_SERVER"]
62 @port = ENV["STOMP_PORT"] || 6163
64 opts.each { |opt, arg|
68 require 'rdoc/ri/ri_paths'
73 puts("Install RDoc::usage or view the comments in the top of the script to get detailed help") if e.to_str != "exit"
88 @conn = Stomp::Connection.open(@user, @password, @server, @port, true)
93 puts("List of commands:")
94 puts("\n\t- subscribe /(topic|queue)/foo subscribes to topic of queue 'foo'")
95 puts("\t- /(topic|queue|/foo bar sends msg 'bar' to topic of queue 'foo'")
96 puts("\t- quit|exit|q|^d exit")
97 puts("\t- detail show/dont show time and topic a msg was received on")
98 puts("\t- help show this help")
103 Thread.new(@conn) do |amq|
106 dest = msg.headers["destination"]
107 time = Time.now.strftime('%H:%M:%S')
110 msg = "\r#{time}:#{dest} > #{msg.body.chomp}\n"
112 msg = "\r#{msg.body.chomp}\n"
120 line = Readline::readline('AMQ> ')
122 Readline::HISTORY.push(line) if line != ""
127 if (line =~ /^(\/(topic|queue)\/\S+)\s+(.+)$/)
128 puts("Sending '#{$3}' to #{$1}")
130 if @conn.respond_to?("publish")
131 @conn.publish($1, $3)
136 elsif (line =~ /^sub\S* (\/(topic|queue)\/\S+)$/)
137 puts("Subscribing to #{$1}")
140 elsif (line =~ /^det(ail)*$/)
143 puts("No longer showing details")
146 puts("Showing time and topic for each msg")
148 elsif (line =~ /^(quit|exit|q)$/)
150 elsif (line =~ /^(help|h|\?)$/)
154 puts("ERROR: unrecognised input: #{line}")