File: conn_putget.rb

package info (click to toggle)
ruby-stomp 1.4.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 832 kB
  • sloc: ruby: 8,595; sh: 77; makefile: 3
file content (131 lines) | stat: -rw-r--r-- 3,241 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- encoding: utf-8 -*-

#
# The current require dance for different Ruby versions.
# Change this to suit your requirements.
#
if Kernel.respond_to?(:require_relative)
  require_relative("./stomp_common")
else
  $LOAD_PATH << File.dirname(__FILE__)
  require "stomp_common"
end
include Stomp1xCommon

#
# == Stomp 1.x Putt / Get Example
#
# Purpose: to demonstrate producing and consuming messages using a
# Stomp#Connection instance.
#
# Note: this example assumes that you have at least the 1.2.0 gem release
# installed.
#
# When you:
#
# * Use a Stomp compliant broker
# * Want a Stomp 1.1+ connection and functionality
#
# then your code *must* specifically request that environment.
#
# You need to supply all of the normal values expected of course:
#
# * login - the user name
# * passcode - the password
# * host - the host to connect to
# * port - the port to connect to
#
# Additionaly you are required to supply the 1.1+ connection data as documented
# in the Stomp 1.1+ specifications:
#
# http://stomp.github.com/stomp-specification-1.0.html
# http://stomp.github.com/stomp-specification-1.1.html
# http://stomp.github.com/stomp-specification-1.2.html
#
# You are urged to become familiar with the specs.  They are short documents.
#
# This includes:
#
# * The Stomp version(s) you wish the broker to consider
# * The broker vhost to connect to
#
# You may optionally specify other 1.1+ data:
#
# * heartbeat request
#
# Using the stomp gem, you should specify this data in the "connect_headers" Hash
# parameter. This example uses the common get_connection() method to 
# get a connection.
#
class ConnectionPutGetExample
  # Initialize
  def initialize
  end
  # Run example
  def run
    #
    # Get a connection
    # ================
    #
    conn = get_connection()
    #
    # Let's just do some sanity checks, and look around.
    #
    raise "Connection failed!!" unless conn.open?()
    #
    # The broker _could_ have returned an ERROR frame (unlikely).
    #
    raise "Connect error: #{conn.connection_frame.body}" if conn.connection_frame.command == Stomp::CMD_ERROR
    #
    puts "Connection complete."
     #
    # Get Destination
    #
    qname = dest()
    #
    # Publish/put messages
    #
    puts "\nConnection start puts"
    nm = nmsgs()
    ph = {:persistent => true}
    ph['suppress_content_length'] = 'yes' if suppresscl()
    puts "Put Headers: #{ph}"
    1.upto(nm) do |n|
      data = "message payload: #{n} #{Time.now.to_f}"
      conn.publish(qname, data, ph)
      puts "Sent: #{data}"
    end

    #conn.disconnect()
    #conn = get_connection()
    
    puts
    puts "Connection start receives"
    # Receives
    #
    # Subscribe
    #
    uuid = conn.uuid() # uuid for Stomp::Connection is a public method
    conn.subscribe(qname, {'id' => uuid}) # Subscribe
    #
    # Run gets
    #
    1.upto(nm) do
      received = conn.receive()
      puts "Received data: #{received.body}"
    end
    #
    # And be polite, unsubscribe.
    #
    conn.unsubscribe(qname, {'id' => uuid})
    #
    # Finally disconnect
    # ==================
    #
    conn.disconnect()   # Business as usual
    puts "\nConnection disconnect complete"
  end
end
#
e = ConnectionPutGetExample.new()
e.run