From d9c73363a9fabbb91ac897e22e050a49fc3b9431 Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Tue, 25 Sep 2012 11:00:29 +0000 Subject: [PATCH] AMQ-4070: Improved ruby sample to shutdown nicely on cltr+c, and use publish. Thanks to Francesco for the patch. git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1389793 13f79535-47bb-0310-9956-ffa450edef68 --- assembly/src/release/example/ruby/catstomp.rb | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/assembly/src/release/example/ruby/catstomp.rb b/assembly/src/release/example/ruby/catstomp.rb index 96c6c495f5..ed8eff0f44 100755 --- a/assembly/src/release/example/ruby/catstomp.rb +++ b/assembly/src/release/example/ruby/catstomp.rb @@ -1,4 +1,4 @@ -#!/usr/bin/ruby +#!/usr/bin/env ruby # ------------------------------------------------------------------------ # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with @@ -19,31 +19,38 @@ require 'rubygems' require 'stomp' -begin +continue = true + +trap("INT") { + puts "CTRL+C" + puts "shutting down ..." + @conn.disconnect + sleep 1 + STDIN.close +} + - @port = 61613 - @host = "localhost" - @user = ENV["STOMP_USER"]; - @password = ENV["STOMP_PASSWORD"] +@port = 61613 +@host = "localhost" +@user = ENV["STOMP_USER"]; +@password = ENV["STOMP_PASSWORD"] - @host = ENV["STOMP_HOST"] if ENV["STOMP_HOST"] != NIL - @port = ENV["STOMP_PORT"] if ENV["STOMP_PORT"] != NIL +@host = ENV["STOMP_HOST"] if ENV["STOMP_HOST"] != NIL +@port = ENV["STOMP_PORT"] if ENV["STOMP_PORT"] != NIL - @destination = "/topic/stompcat" - @destination = $*[0] if $*[0] != NIL +@destination = "/topic/stompcat" +@destination = $*[0] if $*[0] != NIL - $stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n" - @conn = Stomp::Connection.open @user, @password, @host, @port, true - $stderr.print "Sending input to #{@destination}\n" +$stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n" +@conn = Stomp::Connection.open @user, @password, @host, @port, true +$stderr.print "Sending input to #{@destination}\n" - @headers = {'persistent'=>'false'} - @headers['reply-to'] = $*[1] if $*[1] != NIL +@headers = {'persistent'=>'false'} +@headers['reply-to'] = $*[1] if $*[1] != NIL - STDIN.each_line { |line| - @conn.send @destination, line, @headers - } - @conn.disconnect - -rescue +begin + STDIN.each_line { |line| + @conn.publish @destination, line, @headers + } +rescue IOError end -