onstomp 1.0.0pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +2 -0
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +4 -0
- data/DeveloperNarrative.md +15 -0
- data/Gemfile +4 -0
- data/LICENSE.md +221 -0
- data/README.md +73 -0
- data/Rakefile +6 -0
- data/UserNarrative.md +8 -0
- data/examples/basic.rb +40 -0
- data/examples/events.rb +72 -0
- data/lib/onstomp/client.rb +152 -0
- data/lib/onstomp/components/frame.rb +108 -0
- data/lib/onstomp/components/frame_headers.rb +212 -0
- data/lib/onstomp/components/nil_processor.rb +20 -0
- data/lib/onstomp/components/scopes/header_scope.rb +25 -0
- data/lib/onstomp/components/scopes/receipt_scope.rb +25 -0
- data/lib/onstomp/components/scopes/transaction_scope.rb +191 -0
- data/lib/onstomp/components/scopes.rb +45 -0
- data/lib/onstomp/components/subscription.rb +30 -0
- data/lib/onstomp/components/threaded_processor.rb +62 -0
- data/lib/onstomp/components/uri.rb +30 -0
- data/lib/onstomp/components.rb +13 -0
- data/lib/onstomp/connections/base.rb +208 -0
- data/lib/onstomp/connections/heartbeating.rb +82 -0
- data/lib/onstomp/connections/serializers/stomp_1.rb +166 -0
- data/lib/onstomp/connections/serializers/stomp_1_0.rb +41 -0
- data/lib/onstomp/connections/serializers/stomp_1_1.rb +134 -0
- data/lib/onstomp/connections/serializers.rb +9 -0
- data/lib/onstomp/connections/stomp_1.rb +69 -0
- data/lib/onstomp/connections/stomp_1_0.rb +28 -0
- data/lib/onstomp/connections/stomp_1_1.rb +65 -0
- data/lib/onstomp/connections.rb +119 -0
- data/lib/onstomp/interfaces/client_configurable.rb +55 -0
- data/lib/onstomp/interfaces/client_events.rb +168 -0
- data/lib/onstomp/interfaces/connection_events.rb +62 -0
- data/lib/onstomp/interfaces/event_manager.rb +69 -0
- data/lib/onstomp/interfaces/frame_methods.rb +190 -0
- data/lib/onstomp/interfaces/receipt_manager.rb +33 -0
- data/lib/onstomp/interfaces/subscription_manager.rb +48 -0
- data/lib/onstomp/interfaces/uri_configurable.rb +106 -0
- data/lib/onstomp/interfaces.rb +14 -0
- data/lib/onstomp/version.rb +13 -0
- data/lib/onstomp.rb +147 -0
- data/onstomp.gemspec +29 -0
- data/spec/onstomp/client_spec.rb +265 -0
- data/spec/onstomp/components/frame_headers_spec.rb +163 -0
- data/spec/onstomp/components/frame_spec.rb +144 -0
- data/spec/onstomp/components/nil_processor_spec.rb +32 -0
- data/spec/onstomp/components/scopes/header_scope_spec.rb +27 -0
- data/spec/onstomp/components/scopes/receipt_scope_spec.rb +33 -0
- data/spec/onstomp/components/scopes/transaction_scope_spec.rb +227 -0
- data/spec/onstomp/components/scopes_spec.rb +63 -0
- data/spec/onstomp/components/subscription_spec.rb +58 -0
- data/spec/onstomp/components/threaded_processor_spec.rb +92 -0
- data/spec/onstomp/components/uri_spec.rb +33 -0
- data/spec/onstomp/connections/base_spec.rb +349 -0
- data/spec/onstomp/connections/heartbeating_spec.rb +132 -0
- data/spec/onstomp/connections/serializers/stomp_1_0_spec.rb +50 -0
- data/spec/onstomp/connections/serializers/stomp_1_1_spec.rb +99 -0
- data/spec/onstomp/connections/serializers/stomp_1_spec.rb +104 -0
- data/spec/onstomp/connections/stomp_1_0_spec.rb +54 -0
- data/spec/onstomp/connections/stomp_1_1_spec.rb +137 -0
- data/spec/onstomp/connections/stomp_1_spec.rb +113 -0
- data/spec/onstomp/connections_spec.rb +135 -0
- data/spec/onstomp/interfaces/client_events_spec.rb +108 -0
- data/spec/onstomp/interfaces/connection_events_spec.rb +55 -0
- data/spec/onstomp/interfaces/event_manager_spec.rb +72 -0
- data/spec/onstomp/interfaces/frame_methods_spec.rb +109 -0
- data/spec/onstomp/interfaces/receipt_manager_spec.rb +53 -0
- data/spec/onstomp/interfaces/subscription_manager_spec.rb +64 -0
- data/spec/onstomp_spec.rb +15 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/custom_argument_matchers.rb +51 -0
- data/spec/support/frame_matchers.rb +88 -0
- data/spec/support/shared_frame_method_examples.rb +116 -0
- data/yard_extensions.rb +32 -0
- metadata +219 -0
data/.autotest
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# A Narrative for Developers
|
2
|
+
|
3
|
+
This document explores the `OnStomp` library through a narrative aimed at end
|
4
|
+
developers who wish to extend or modify the library. It will start with the
|
5
|
+
basics and work through the important code through exposition and examples.
|
6
|
+
|
7
|
+
## Clients
|
8
|
+
|
9
|
+
## Event Management
|
10
|
+
|
11
|
+
## URI Based Configuration
|
12
|
+
|
13
|
+
## Connections
|
14
|
+
|
15
|
+
## Processors
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,221 @@
|
|
1
|
+
# Apache License
|
2
|
+
|
3
|
+
Version 2.0, January 2004
|
4
|
+
|
5
|
+
[http://www.apache.org/licenses/](http://www.apache.org/licenses/)
|
6
|
+
|
7
|
+
## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
8
|
+
|
9
|
+
### 1. Definitions
|
10
|
+
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
12
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
13
|
+
|
14
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
15
|
+
the copyright owner that is granting the License.
|
16
|
+
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
18
|
+
other entities that control, are controlled by, or are under common
|
19
|
+
control with that entity. For the purposes of this definition,
|
20
|
+
"control" means (i) the power, direct or indirect, to cause the
|
21
|
+
direction or management of such entity, whether by contract or
|
22
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
23
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
24
|
+
|
25
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
26
|
+
exercising permissions granted by this License.
|
27
|
+
|
28
|
+
"Source" form shall mean the preferred form for making modifications,
|
29
|
+
including but not limited to software source code, documentation
|
30
|
+
source, and configuration files.
|
31
|
+
|
32
|
+
"Object" form shall mean any form resulting from mechanical
|
33
|
+
transformation or translation of a Source form, including but
|
34
|
+
not limited to compiled object code, generated documentation,
|
35
|
+
and conversions to other media types.
|
36
|
+
|
37
|
+
"Work" shall mean the work of authorship, whether in Source or
|
38
|
+
Object form, made available under the License, as indicated by a
|
39
|
+
copyright notice that is included in or attached to the work
|
40
|
+
(an example is provided in the Appendix below).
|
41
|
+
|
42
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
43
|
+
form, that is based on (or derived from) the Work and for which the
|
44
|
+
editorial revisions, annotations, elaborations, or other modifications
|
45
|
+
represent, as a whole, an original work of authorship. For the purposes
|
46
|
+
of this License, Derivative Works shall not include works that remain
|
47
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
48
|
+
the Work and Derivative Works thereof.
|
49
|
+
|
50
|
+
"Contribution" shall mean any work of authorship, including
|
51
|
+
the original version of the Work and any modifications or additions
|
52
|
+
to that Work or Derivative Works thereof, that is intentionally
|
53
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
54
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
55
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
56
|
+
means any form of electronic, verbal, or written communication sent
|
57
|
+
to the Licensor or its representatives, including but not limited to
|
58
|
+
communication on electronic mailing lists, source code control systems,
|
59
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
60
|
+
Licensor for the purpose of discussing and improving the Work, but
|
61
|
+
excluding communication that is conspicuously marked or otherwise
|
62
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
63
|
+
|
64
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
65
|
+
on behalf of whom a Contribution has been received by Licensor and
|
66
|
+
subsequently incorporated within the Work.
|
67
|
+
|
68
|
+
### 2. Grant of Copyright License
|
69
|
+
|
70
|
+
Subject to the terms and conditions of
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
73
|
+
copyright license to reproduce, prepare Derivative Works of,
|
74
|
+
publicly display, publicly perform, sublicense, and distribute the
|
75
|
+
Work and such Derivative Works in Source or Object form.
|
76
|
+
|
77
|
+
### 3. Grant of Patent License
|
78
|
+
Subject to the terms and conditions of
|
79
|
+
this License, each Contributor hereby grants to You a perpetual,
|
80
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
81
|
+
(except as stated in this section) patent license to make, have made,
|
82
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
83
|
+
where such license applies only to those patent claims licensable
|
84
|
+
by such Contributor that are necessarily infringed by their
|
85
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
86
|
+
with the Work to which such Contribution(s) was submitted. If You
|
87
|
+
institute patent litigation against any entity (including a
|
88
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
89
|
+
or a Contribution incorporated within the Work constitutes direct
|
90
|
+
or contributory patent infringement, then any patent licenses
|
91
|
+
granted to You under this License for that Work shall terminate
|
92
|
+
as of the date such litigation is filed.
|
93
|
+
|
94
|
+
### 4. Redistribution
|
95
|
+
|
96
|
+
You may reproduce and distribute copies of the
|
97
|
+
Work or Derivative Works thereof in any medium, with or without
|
98
|
+
modifications, and in Source or Object form, provided that You
|
99
|
+
meet the following conditions:
|
100
|
+
|
101
|
+
<ol style="list-style-type: lower-alpha;">
|
102
|
+
<li>
|
103
|
+
You must give any other recipients of the Work or
|
104
|
+
Derivative Works a copy of this License; and
|
105
|
+
</li>
|
106
|
+
<li>
|
107
|
+
You must cause any modified files to carry prominent notices
|
108
|
+
stating that You changed the files; and
|
109
|
+
</li>
|
110
|
+
<li>
|
111
|
+
You must retain, in the Source form of any Derivative Works
|
112
|
+
that You distribute, all copyright, patent, trademark, and
|
113
|
+
attribution notices from the Source form of the Work,
|
114
|
+
excluding those notices that do not pertain to any part of
|
115
|
+
the Derivative Works; and
|
116
|
+
</li>
|
117
|
+
<li>
|
118
|
+
If the Work includes a "NOTICE" text file as part of its
|
119
|
+
distribution, then any Derivative Works that You distribute must
|
120
|
+
include a readable copy of the attribution notices contained
|
121
|
+
within such NOTICE file, excluding those notices that do not
|
122
|
+
pertain to any part of the Derivative Works, in at least one
|
123
|
+
of the following places: within a NOTICE text file distributed
|
124
|
+
as part of the Derivative Works; within the Source form or
|
125
|
+
documentation, if provided along with the Derivative Works; or,
|
126
|
+
within a display generated by the Derivative Works, if and
|
127
|
+
wherever such third-party notices normally appear. The contents
|
128
|
+
of the NOTICE file are for informational purposes only and
|
129
|
+
do not modify the License. You may add Your own attribution
|
130
|
+
notices within Derivative Works that You distribute, alongside
|
131
|
+
or as an addendum to the NOTICE text from the Work, provided
|
132
|
+
that such additional attribution notices cannot be construed
|
133
|
+
as modifying the License.
|
134
|
+
</li>
|
135
|
+
</ol>
|
136
|
+
|
137
|
+
You may add Your own copyright statement to Your modifications and
|
138
|
+
may provide additional or different license terms and conditions
|
139
|
+
for use, reproduction, or distribution of Your modifications, or
|
140
|
+
for any such Derivative Works as a whole, provided Your use,
|
141
|
+
reproduction, and distribution of the Work otherwise complies with
|
142
|
+
the conditions stated in this License.
|
143
|
+
|
144
|
+
### 5. Submission of Contributions
|
145
|
+
Unless You explicitly state otherwise,
|
146
|
+
any Contribution intentionally submitted for inclusion in the Work
|
147
|
+
by You to the Licensor shall be under the terms and conditions of
|
148
|
+
this License, without any additional terms or conditions.
|
149
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
150
|
+
the terms of any separate license agreement you may have executed
|
151
|
+
with Licensor regarding such Contributions.
|
152
|
+
|
153
|
+
### 6. Trademarks
|
154
|
+
This License does not grant permission to use the trade
|
155
|
+
names, trademarks, service marks, or product names of the Licensor,
|
156
|
+
except as required for reasonable and customary use in describing the
|
157
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
158
|
+
|
159
|
+
### 7. Disclaimer of Warranty
|
160
|
+
Unless required by applicable law or
|
161
|
+
agreed to in writing, Licensor provides the Work (and each
|
162
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
163
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
164
|
+
implied, including, without limitation, any warranties or conditions
|
165
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
166
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
167
|
+
appropriateness of using or redistributing the Work and assume any
|
168
|
+
risks associated with Your exercise of permissions under this License.
|
169
|
+
|
170
|
+
### 8. Limitation of Liability
|
171
|
+
In no event and under no legal theory,
|
172
|
+
whether in tort (including negligence), contract, or otherwise,
|
173
|
+
unless required by applicable law (such as deliberate and grossly
|
174
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
175
|
+
liable to You for damages, including any direct, indirect, special,
|
176
|
+
incidental, or consequential damages of any character arising as a
|
177
|
+
result of this License or out of the use or inability to use the
|
178
|
+
Work (including but not limited to damages for loss of goodwill,
|
179
|
+
work stoppage, computer failure or malfunction, or any and all
|
180
|
+
other commercial damages or losses), even if such Contributor
|
181
|
+
has been advised of the possibility of such damages.
|
182
|
+
|
183
|
+
### 9. Accepting Warranty or Additional Liability
|
184
|
+
While redistributing
|
185
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
186
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
187
|
+
or other liability obligations and/or rights consistent with this
|
188
|
+
License. However, in accepting such obligations, You may act only
|
189
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
190
|
+
of any other Contributor, and only if You agree to indemnify,
|
191
|
+
defend, and hold each Contributor harmless for any liability
|
192
|
+
incurred by, or claims asserted against, such Contributor by reason
|
193
|
+
of your accepting any such warranty or additional liability.
|
194
|
+
|
195
|
+
## END OF TERMS AND CONDITIONS
|
196
|
+
|
197
|
+
## APPENDIX: How to apply the Apache License to your work.
|
198
|
+
|
199
|
+
To apply the Apache License to your work, attach the following
|
200
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
201
|
+
replaced with your own identifying information. (Don't include
|
202
|
+
the brackets!) The text should be enclosed in the appropriate
|
203
|
+
comment syntax for the file format. We also recommend that a
|
204
|
+
file or class name and description of purpose be included on the
|
205
|
+
same "printed page" as the copyright notice for easier
|
206
|
+
identification within third-party archives.
|
207
|
+
|
208
|
+
Copyright [yyyy] [name of copyright owner]
|
209
|
+
|
210
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
211
|
+
you may not use this file except in compliance with the License.
|
212
|
+
You may obtain a copy of the License at
|
213
|
+
|
214
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
215
|
+
|
216
|
+
Unless required by applicable law or agreed to in writing, software
|
217
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
218
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
219
|
+
See the License for the specific language governing permissions and
|
220
|
+
limitations under the License.
|
221
|
+
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# OnStomp
|
2
|
+
|
3
|
+
A client-side ruby gem for communicating with message brokers that support
|
4
|
+
the STOMP 1.0 and 1.1 protocols. This gem was formerly known as `stomper`,
|
5
|
+
but that name has been dropped because a
|
6
|
+
[python stomp library](http://code.google.com/p/stomper/) by the same name
|
7
|
+
already existed. Also, I think "OnStomp" better expresses the event-driven
|
8
|
+
nature of this gem.
|
9
|
+
|
10
|
+
## Installing
|
11
|
+
|
12
|
+
The OnStomp gem can be installed as a standard ruby gem:
|
13
|
+
|
14
|
+
gem install onstomp
|
15
|
+
|
16
|
+
Alternatively, you can clone the
|
17
|
+
[source](https://github.com/meadvillerb/onstomp) through github.
|
18
|
+
|
19
|
+
## Example Usage
|
20
|
+
|
21
|
+
# A simple message producer
|
22
|
+
client = OnStomp.connect('stomp://user:passw0rd@broker.example.org')
|
23
|
+
client.send('/queue/onstomp-test', 'hello world')
|
24
|
+
client.disconnect
|
25
|
+
|
26
|
+
# A simple message consumer
|
27
|
+
client = OnStomp::Client.new('stomp+ssl://broker.example.org:10101')
|
28
|
+
client.connect
|
29
|
+
client.subscribe('/queue/onstomp-test', :ack => 'client') do |m|
|
30
|
+
client.ack m
|
31
|
+
puts "Got and ACK'd a message: #{m.body}"
|
32
|
+
end
|
33
|
+
|
34
|
+
while true
|
35
|
+
# Keep the subscription running until the sun burns out
|
36
|
+
end
|
37
|
+
|
38
|
+
## Motivation
|
39
|
+
|
40
|
+
There is a STOMP client gem named [stomp](http://gitorious.org/stomp), so why
|
41
|
+
create another gem? OnStomp was designed around giving users more control
|
42
|
+
over how STOMP frames are handled through an event-driven interface. All
|
43
|
+
IO reading and writing is performed through the use of non-blocking methods
|
44
|
+
in the hopes of increasing performance.
|
45
|
+
|
46
|
+
The `stomp` gem is a good gem that works well, I just desired a different
|
47
|
+
style API for working with message brokers.
|
48
|
+
|
49
|
+
## Further Reading
|
50
|
+
|
51
|
+
* A {file:docs/UserNarrative.md User's Narrative}
|
52
|
+
* A {file:docs/DeveloperNarrative.md Developers's Narrative}
|
53
|
+
* A {file:docs/CHANGELOG.md History of Changes}
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
OnStomp is covered by the Apache License 2.0.
|
58
|
+
See the full {file:docs/LICENSE.md LICENSE} for details.
|
59
|
+
|
60
|
+
## Thanks
|
61
|
+
|
62
|
+
There are a few people/groups I'd like to thank for helping me with the
|
63
|
+
creation of this gem.
|
64
|
+
|
65
|
+
* Lionel Cons the good suggestions while I was implementing support for the
|
66
|
+
STOMP 1.1 spec. Check out his Perl client [Net::STOMP::Client](http://search.cpan.org/~lcons/Net-STOMP-Client-0.9.5/lib/Net/STOMP/Client.pm)
|
67
|
+
* Brian McCallister, Johan Sørensen, Guy M. Allard and Thiago Morello for
|
68
|
+
their work on the `stomp` gem which introduced me to the STOMP protocol.
|
69
|
+
* Hiram Chino and everyone on the stomp-spec mailing list for keeping the
|
70
|
+
STOMP 1.1 spec moving
|
71
|
+
* Aman Gupta and contributors to
|
72
|
+
[eventmachine](https://github.com/eventmachine/eventmachine) for the insights
|
73
|
+
into working with non-blocking IO in Ruby
|
data/Rakefile
ADDED
data/UserNarrative.md
ADDED
data/examples/basic.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.unshift(File.expand_path('../../lib', __FILE__))
|
3
|
+
require 'onstomp'
|
4
|
+
|
5
|
+
puts "Starting demo"
|
6
|
+
puts "----------------------------"
|
7
|
+
|
8
|
+
running = true
|
9
|
+
client = OnStomp::Client.new("stomp://localhost")
|
10
|
+
client.connect
|
11
|
+
|
12
|
+
puts "Connected to broker using protocol #{client.connection.version}"
|
13
|
+
|
14
|
+
client.subscribe("/queue/onstomp/test") do |message|
|
15
|
+
puts "Received: '#{message.body}'"
|
16
|
+
if message.body == 'finished'
|
17
|
+
running = false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
client.send("/queue/onstomp/test", "hello world")
|
22
|
+
client.send("/queue/onstomp/test", "this is a simple demo of onstomp")
|
23
|
+
client.send("/queue/onstomp/test", "finished")
|
24
|
+
|
25
|
+
Thread.pass while running
|
26
|
+
client.disconnect
|
27
|
+
puts "----------------------------"
|
28
|
+
puts "End of demo"
|
29
|
+
|
30
|
+
# Example output:
|
31
|
+
#
|
32
|
+
#
|
33
|
+
# Starting demo
|
34
|
+
# ----------------------------
|
35
|
+
# Connected to broker using protocol 1.0
|
36
|
+
# Received: hello world
|
37
|
+
# Received: this is a simple demo of onstomp
|
38
|
+
# Received: finished
|
39
|
+
# ----------------------------
|
40
|
+
# End of demo
|
data/examples/events.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.unshift(File.expand_path('../../lib', __FILE__))
|
3
|
+
require 'onstomp'
|
4
|
+
|
5
|
+
puts "Starting demo"
|
6
|
+
puts "----------------------------"
|
7
|
+
|
8
|
+
client = OnStomp::Client.new("stomp://localhost")
|
9
|
+
|
10
|
+
client.before_transmitting do |frame, _|
|
11
|
+
puts "Frame headers [#{frame.command}] before modification: #{frame.headers.to_a.inspect}"
|
12
|
+
frame[:'x-alt-header'] = 'another value'
|
13
|
+
end
|
14
|
+
|
15
|
+
client.before_send do |frame, _|
|
16
|
+
puts "SEND headers before modification: #{frame.headers.to_a.inspect}"
|
17
|
+
frame[:'x-misc-header'] = 'this is a test'
|
18
|
+
end
|
19
|
+
|
20
|
+
client.after_transmitting do |frame, _|
|
21
|
+
puts "Final frame headers [#{frame.command}]: #{frame.headers.to_a.inspect}"
|
22
|
+
end
|
23
|
+
|
24
|
+
client.before_disconnect do |frame, _|
|
25
|
+
puts "Disconnecting from broker"
|
26
|
+
end
|
27
|
+
|
28
|
+
client.on_connection_established do |client, con|
|
29
|
+
puts "=== Connected to broker using protocol #{con.version} ==="
|
30
|
+
end
|
31
|
+
|
32
|
+
client.on_connection_closed do |client, con|
|
33
|
+
puts "=== Connection has been closed ==="
|
34
|
+
end
|
35
|
+
|
36
|
+
client.on_connection_terminated do |client, con|
|
37
|
+
puts "=== Connection closed unexpectedly ==="
|
38
|
+
end
|
39
|
+
|
40
|
+
receipt_count = 0
|
41
|
+
client.connect
|
42
|
+
client.send("/queue/onstomp/test", "hello world") do |r|
|
43
|
+
puts "---- Got receipt #{r[:'receipt-id']} ----"
|
44
|
+
raise ArgumentError, 'blam!'
|
45
|
+
receipt_count += 1
|
46
|
+
end
|
47
|
+
|
48
|
+
while receipt_count < 1 && client.connected?
|
49
|
+
end
|
50
|
+
|
51
|
+
client.disconnect rescue nil
|
52
|
+
|
53
|
+
puts "----------------------------"
|
54
|
+
puts "End of demo"
|
55
|
+
|
56
|
+
# Example output:
|
57
|
+
#
|
58
|
+
#
|
59
|
+
# Starting demo
|
60
|
+
# ----------------------------
|
61
|
+
# Final frame headers [CONNECT]: [["accept-version", "1.0,1.1"], ["host", "localhost"], ["heart-beat", "0,0"], ["login", ""], ["passcode", ""]]
|
62
|
+
# === Connected to broker using protocol 1.0 ===
|
63
|
+
# Frame headers [SEND] before modification: [["destination", "/queue/onstomp/test"], ["receipt", "1"]]
|
64
|
+
# SEND headers before modification: [["destination", "/queue/onstomp/test"], ["receipt", "1"], ["x-alt-header", "another value"]]
|
65
|
+
# Final frame headers [SEND]: [["destination", "/queue/onstomp/test"], ["receipt", "1"], ["x-alt-header", "another value"], ["x-misc-header", "this is a test"], ["content-length", "11"]]
|
66
|
+
# ---- Got receipt 1 ----
|
67
|
+
# === Connection closed unexpectedly ===
|
68
|
+
# Frame headers [DISCONNECT] before modification: []
|
69
|
+
# Disconnecting from broker
|
70
|
+
# === Connection has been closed ===
|
71
|
+
# ----------------------------
|
72
|
+
# End of demo
|
@@ -0,0 +1,152 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
# This class encapsulates a client connection to a message broker through the
|
4
|
+
# Stomp protocol.
|
5
|
+
class OnStomp::Client
|
6
|
+
include OnStomp::Interfaces::ClientConfigurable
|
7
|
+
include OnStomp::Interfaces::FrameMethods
|
8
|
+
include OnStomp::Interfaces::ClientEvents
|
9
|
+
include OnStomp::Interfaces::ReceiptManager
|
10
|
+
include OnStomp::Interfaces::SubscriptionManager
|
11
|
+
include OnStomp::Components::Scopes
|
12
|
+
|
13
|
+
# The +URI+ reference to the STOMP broker
|
14
|
+
# @return [String]
|
15
|
+
attr_reader :uri
|
16
|
+
# SSL options for the connection
|
17
|
+
# @return {Symbol => Object}
|
18
|
+
attr_reader :ssl
|
19
|
+
# Connection object specific to the established STOMP protocol version
|
20
|
+
# @return [OnStomp::Connections::Base]
|
21
|
+
attr_reader :connection
|
22
|
+
|
23
|
+
# The protocol versions to allow for this connection
|
24
|
+
# @return [Array<String>]
|
25
|
+
attr_configurable_protocols :versions
|
26
|
+
|
27
|
+
# The client-side heartbeat settings to allow for this connection
|
28
|
+
# @return [Array<Fixnum>]
|
29
|
+
attr_configurable_client_beats :heartbeats
|
30
|
+
|
31
|
+
# The host header value to send to the broker when connecting. This allows
|
32
|
+
# the client to inform the server which host it wishes to connect with
|
33
|
+
# when multiple brokers may share an IP address through virtual hosting.
|
34
|
+
# @return [String]
|
35
|
+
attr_configurable_str :host, :default => 'localhost', :uri_attr => :host
|
36
|
+
|
37
|
+
# The login header value to send to the broker when connecting.
|
38
|
+
# @return [String]
|
39
|
+
attr_configurable_str :login, :default => '', :uri_attr => :user
|
40
|
+
|
41
|
+
# The passcode header value to send to the broker when connecting.
|
42
|
+
# @return [String]
|
43
|
+
attr_configurable_str :passcode, :default => '', :uri_attr => :password
|
44
|
+
|
45
|
+
# The class to use when instantiating a new IO processor for the connection.
|
46
|
+
# Defaults to {OnStomp::Components::ThreadedProcessor}
|
47
|
+
# @return [Class]
|
48
|
+
attr_configurable_processor :processor
|
49
|
+
|
50
|
+
# Creates a new client for the specified uri and optional hash of options.
|
51
|
+
# @param [String,URI] uri
|
52
|
+
# @param [{Symbol => Object}] options
|
53
|
+
def initialize(uri, options={})
|
54
|
+
@uri = uri.is_a?(::URI) ? uri : ::URI.parse(uri)
|
55
|
+
@ssl = options.delete(:ssl)
|
56
|
+
configure_configurable options
|
57
|
+
configure_subscription_management
|
58
|
+
configure_receipt_management
|
59
|
+
on_disconnect do |f, con|
|
60
|
+
close unless f[:receipt]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Connects to the STOMP broker referenced by {#uri}. Includes optional
|
65
|
+
# headers in the CONNECT frame, if specified.
|
66
|
+
# @param [{#to_sym => #to_s}] headers
|
67
|
+
# @return [self]
|
68
|
+
def connect(headers={})
|
69
|
+
@connection = OnStomp::Connections.connect self, headers,
|
70
|
+
{ :'accept-version' => @versions.join(','), :host => @host,
|
71
|
+
:'heart-beat' => @heartbeats.join(','), :login => @login,
|
72
|
+
:passcode => @passcode }, pending_connection_events
|
73
|
+
processor_inst.start
|
74
|
+
self
|
75
|
+
end
|
76
|
+
alias :open :connect
|
77
|
+
|
78
|
+
# Sends a DISCONNECT frame to the broker and blocks until the connection
|
79
|
+
# has been closed. This method ensures that all frames not yet sent to
|
80
|
+
# the broker will get processed barring any IO exceptions.
|
81
|
+
# @param [{#to_sym => #to_s}] headers
|
82
|
+
# @return [OnStomp::Components::Frame] transmitted DISCONNECT frame
|
83
|
+
def disconnect_with_flush(headers={})
|
84
|
+
disconnect_without_flush(headers).tap do
|
85
|
+
processor_inst.join
|
86
|
+
end
|
87
|
+
end
|
88
|
+
alias :disconnect_without_flush :disconnect
|
89
|
+
alias :disconnect :disconnect_with_flush
|
90
|
+
|
91
|
+
# Returns true if a connection to the broker exists and itself is connected.
|
92
|
+
# @return [true,false]
|
93
|
+
def connected?
|
94
|
+
connection && connection.connected?
|
95
|
+
end
|
96
|
+
|
97
|
+
# Forces the connection between broker and client closed.
|
98
|
+
# @note Use of this method may result in frames never being sent to the
|
99
|
+
# broker. This method should only be used if {#disconnect} is not an
|
100
|
+
# option and the connection needs to be terminated immediately.
|
101
|
+
# @return [self]
|
102
|
+
def close!
|
103
|
+
close
|
104
|
+
processor_inst.stop
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
# @group Methods you ought not use directly.
|
109
|
+
|
110
|
+
# Ultimately sends a {OnStomp::Components::Frame frame} to the STOMP broker.
|
111
|
+
# This method should not be invoked directly. Use the frame methods provided
|
112
|
+
# by the {OnStomp::Interfaces:FrameMethod} interface.
|
113
|
+
# @return [OnStomp::Components::Frame]
|
114
|
+
def transmit(frame, cbs={})
|
115
|
+
frame.tap do
|
116
|
+
register_callbacks frame, cbs
|
117
|
+
trigger_before_transmitting frame
|
118
|
+
connection.write_frame_nonblock frame
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Called by {#connection} when a frame has been read from the socket
|
123
|
+
# connection to the STOMP broker.
|
124
|
+
def dispatch_received frame
|
125
|
+
trigger_before_receiving frame
|
126
|
+
trigger_after_receiving frame
|
127
|
+
end
|
128
|
+
|
129
|
+
# Called by {#connection} when a frame has been written to the socket
|
130
|
+
# connection to the STOMP broker.
|
131
|
+
def dispatch_transmitted frame
|
132
|
+
trigger_after_transmitting frame
|
133
|
+
end
|
134
|
+
|
135
|
+
# @endgroup
|
136
|
+
|
137
|
+
private
|
138
|
+
def register_callbacks f, cbs
|
139
|
+
cbs[:subscribe] && add_subscription(f, cbs[:subscribe])
|
140
|
+
cbs[:receipt] && add_receipt(f, cbs[:receipt])
|
141
|
+
end
|
142
|
+
|
143
|
+
def processor_inst
|
144
|
+
@processor_inst ||= processor.new(self)
|
145
|
+
end
|
146
|
+
|
147
|
+
def close
|
148
|
+
connection && connection.close
|
149
|
+
clear_subscriptions
|
150
|
+
clear_receipts
|
151
|
+
end
|
152
|
+
end
|