rocketmq-client-ruby 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/lib/rocketmq-client-ruby/client/message.rb +48 -0
- data/lib/rocketmq-client-ruby/client/producer.rb +85 -0
- data/lib/rocketmq-client-ruby/client/push_consumer.rb +107 -0
- data/lib/rocketmq-client-ruby/client/received_message.rb +84 -0
- data/lib/rocketmq-client-ruby/client/response.rb +14 -0
- data/lib/rocketmq-client-ruby/client/transaction_mq_producer.rb +75 -0
- data/lib/rocketmq-client-ruby/rocketmq.rb +180 -0
- data/lib/rocketmq-client-ruby/version.rb +3 -0
- data/lib/rocketmq-client-ruby.rb +62 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bc8d8fefaefbcde565865564358bb0b23c5e5f49f94eca64e790aa6a83cd3eb4
|
4
|
+
data.tar.gz: 316bf2c053e255dea897923ca049c5841ad35a6516fe241b7cb0a15de0c0b252
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: abf37dbca078dee1e44953e81bcaebc0f0785bab264759a609c9683702607917ec2f161bbb6424397eec89f2570432dc3a9308f29f977700415af4a5cca64b4f
|
7
|
+
data.tar.gz: 8cd7030d81156a78733fb1ee085e4b5d0a682f330ae1fdeec59756bbec0bb586f65aea70d8433bf54fd11b1bd3b28fe1ab641a4700e48b0bc055f31503701903
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Client
|
4
|
+
# Message module
|
5
|
+
class Message
|
6
|
+
include Rocketmq::C
|
7
|
+
|
8
|
+
def initialize(topic)
|
9
|
+
@message = CreateMessage(topic)
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_keys(keys)
|
13
|
+
SetMessageKeys(@message, keys)
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_tags(tags)
|
17
|
+
SetMessageTags(@message, tags)
|
18
|
+
end
|
19
|
+
|
20
|
+
def set_body(body)
|
21
|
+
SetMessageBody(@message, body)
|
22
|
+
end
|
23
|
+
|
24
|
+
def set_property(key, value)
|
25
|
+
SetMessageProperty(@message, key, value)
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_property(key)
|
29
|
+
GetMessageProperty(@message, key)
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_delay_time_level(delay_time_level)
|
33
|
+
SetDelayTimeLevel(@message, delay_time_level)
|
34
|
+
end
|
35
|
+
|
36
|
+
def [](key)
|
37
|
+
get_property(key)
|
38
|
+
end
|
39
|
+
|
40
|
+
def []=(key, value)
|
41
|
+
set_property(key, value)
|
42
|
+
end
|
43
|
+
|
44
|
+
def raw
|
45
|
+
@message
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Client
|
4
|
+
# Producer module
|
5
|
+
class Producer
|
6
|
+
include Rocketmq::C
|
7
|
+
|
8
|
+
def initialize(group_id, orderly: false, timeout: nil, compress_level: nil, max_message_size: nil)
|
9
|
+
producer_factory =
|
10
|
+
if orderly
|
11
|
+
:CreateOrderlyProducer
|
12
|
+
else
|
13
|
+
:CreateProducer
|
14
|
+
end
|
15
|
+
|
16
|
+
@producer = send(producer_factory, group_id)
|
17
|
+
@callback_refs = []
|
18
|
+
raise StandardError.new('Returned null pointer when create Producer') unless @producer
|
19
|
+
|
20
|
+
set_timeout(timeout) if timeout.to_i.positive?
|
21
|
+
set_compress_level(compress_level) if compress_level
|
22
|
+
set_max_message_size(max_message_size) if max_message_size.to_i.positive?
|
23
|
+
end
|
24
|
+
|
25
|
+
def set_compress_level(compress_level)
|
26
|
+
SetProducerCompressLevel(@producer, compress_level)
|
27
|
+
end
|
28
|
+
|
29
|
+
def set_max_message_size(max_message_size)
|
30
|
+
SetProducerMaxMessageSize(@producer, max_message_size)
|
31
|
+
end
|
32
|
+
|
33
|
+
def set_timeout(timeout)
|
34
|
+
SetProducerSendMsgTimeout(@producer, timeout)
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_name_server_domain(domain)
|
38
|
+
SetProducerNameServerDomain(@producer, domain)
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_name_server_address(addr)
|
42
|
+
SetProducerNameServerAddress(@producer, addr)
|
43
|
+
end
|
44
|
+
|
45
|
+
def set_session_credentials(access_key, access_secret, channel)
|
46
|
+
SetProducerSessionCredentials(@producer, access_key, access_secret, channel)
|
47
|
+
end
|
48
|
+
|
49
|
+
def send_sync(msg)
|
50
|
+
c_result = SendResult.new
|
51
|
+
SendMessageSync(@producer, msg.raw, c_result.to_ptr)
|
52
|
+
Response.new(c_result)
|
53
|
+
end
|
54
|
+
|
55
|
+
def send_oneway(msg)
|
56
|
+
SendMessageOneway(msg)
|
57
|
+
end
|
58
|
+
|
59
|
+
def send_orderly_with_sharding_key(msg, sharding_key)
|
60
|
+
c_result = SendResult.new
|
61
|
+
SendMessageOrderlyByShardingKey(@producer, msg.raw, sharding_key, c_result.to_ptr)
|
62
|
+
Response.new(c_result)
|
63
|
+
end
|
64
|
+
|
65
|
+
def set_group(group_name)
|
66
|
+
SetProducerGroupName(@producer, group_name)
|
67
|
+
end
|
68
|
+
|
69
|
+
def set_instance_name(instance_name)
|
70
|
+
SetProducerInstanceName(@producer, instance_name)
|
71
|
+
end
|
72
|
+
|
73
|
+
def start
|
74
|
+
StartProducer(@producer)
|
75
|
+
end
|
76
|
+
|
77
|
+
def shutdown
|
78
|
+
ShutdownProducer(@producer)
|
79
|
+
end
|
80
|
+
|
81
|
+
def destroy
|
82
|
+
DestroyPushConsumer(@producer)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Client
|
4
|
+
# PushConsumer
|
5
|
+
class PushConsumer
|
6
|
+
include Rocketmq::C
|
7
|
+
|
8
|
+
def initialize(group_id, orderly: false, message_model: MessageModel[:clustering])
|
9
|
+
@orderly = orderly
|
10
|
+
@push_consumer = CreatePushConsumer(group_id)
|
11
|
+
@callback_refs = []
|
12
|
+
raise StandardError.new('Returned null pointer when create Producer') unless @push_consumer
|
13
|
+
set_message_model(message_model)
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_message_model(model)
|
17
|
+
SetPushConsumerMessageModel(@push_consumer, model)
|
18
|
+
end
|
19
|
+
|
20
|
+
def set_name_server_address(addr)
|
21
|
+
SetPushConsumerNameServerAddress(@push_consumer, addr)
|
22
|
+
end
|
23
|
+
|
24
|
+
def set_name_server_domain(domain)
|
25
|
+
SetPushConsumerNameServerDomain(@push_consumer, domain)
|
26
|
+
end
|
27
|
+
|
28
|
+
def set_session_credentials(access_key, access_secret, channel)
|
29
|
+
SetPushConsumerSessionCredentials(@push_consumer, access_key, access_secret, channel)
|
30
|
+
end
|
31
|
+
|
32
|
+
def subscribe(topic, callback, expression: '*')
|
33
|
+
on_message =
|
34
|
+
FFI::Function.new(:int, %i[pointer pointer]) do |_, msg|
|
35
|
+
exc = nil
|
36
|
+
begin
|
37
|
+
consume_result = callback.call(ReceivedMessage.new(msg))
|
38
|
+
if consume_result != ConsumeStatus[:consume_success] &&
|
39
|
+
consume_result != ConsumeStatus[:reconsume_later]
|
40
|
+
raise StandardError.new('Consume status error, please use enum \'ConsumeStatus\' as response')
|
41
|
+
end
|
42
|
+
return consume_result
|
43
|
+
rescue => ex
|
44
|
+
exc = ex
|
45
|
+
return ConsumeStatus[:reconsume_later]
|
46
|
+
ensure
|
47
|
+
raise exc if exc
|
48
|
+
end
|
49
|
+
end
|
50
|
+
Subscribe(@push_consumer, topic, expression)
|
51
|
+
register_callback(on_message)
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_group
|
55
|
+
GetPushConsumerGroupID(@push_consumer)
|
56
|
+
end
|
57
|
+
|
58
|
+
def set_group(group_id)
|
59
|
+
SetPushConsumerGroupID(@push_consumer, group_id)
|
60
|
+
end
|
61
|
+
|
62
|
+
def set_thread_count(thread_count)
|
63
|
+
SetPushConsumerThreadCount(@push_consumer, thread_count)
|
64
|
+
end
|
65
|
+
|
66
|
+
def set_message_batch_max_size(max_size)
|
67
|
+
SetPushConsumerMessageBatchMaxSize(@push_consumer, max_size)
|
68
|
+
end
|
69
|
+
|
70
|
+
def set_instance_name(name)
|
71
|
+
SetPushConsumerInstanceName(@push_consumer, name)
|
72
|
+
end
|
73
|
+
|
74
|
+
def start
|
75
|
+
StartPushConsumer(@push_consumer)
|
76
|
+
end
|
77
|
+
|
78
|
+
def shutdown
|
79
|
+
unregister_callback(@push_consumer) if @callback_refs.length.positive?
|
80
|
+
ShutdownPushConsumer(@push_consumer)
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def register_callback(callback)
|
86
|
+
register_func =
|
87
|
+
if @orderly
|
88
|
+
:RegisterMessageCallbackOrderly
|
89
|
+
else
|
90
|
+
:RegisterMessageCallback
|
91
|
+
end
|
92
|
+
@callback_refs << callback
|
93
|
+
send(register_func, @push_consumer, callback)
|
94
|
+
end
|
95
|
+
|
96
|
+
def unregister_callback
|
97
|
+
unregister_func =
|
98
|
+
if @orderly
|
99
|
+
:UnregisterMessageCallbackOrderly
|
100
|
+
else
|
101
|
+
:UnregisterMessageCallback
|
102
|
+
end
|
103
|
+
send(unregister_func, @push_consumer)
|
104
|
+
@callback_refs = []
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Client
|
4
|
+
# ReceivedMessage Module
|
5
|
+
class ReceivedMessage
|
6
|
+
include Rocketmq::C
|
7
|
+
|
8
|
+
def initialize(handle)
|
9
|
+
@handle = handle
|
10
|
+
end
|
11
|
+
|
12
|
+
def topic
|
13
|
+
GetMessageTopic(@handle)
|
14
|
+
end
|
15
|
+
|
16
|
+
def tags
|
17
|
+
GetMessageTags(@handle)
|
18
|
+
end
|
19
|
+
|
20
|
+
def keys
|
21
|
+
GetMessageKeys(@handle)
|
22
|
+
end
|
23
|
+
|
24
|
+
def body
|
25
|
+
GetMessageBody(@handle)
|
26
|
+
end
|
27
|
+
|
28
|
+
def id
|
29
|
+
GetMessageId(@handle)
|
30
|
+
end
|
31
|
+
|
32
|
+
def delay_time_level
|
33
|
+
GetMessageDelayTimeLevel(@handle)
|
34
|
+
end
|
35
|
+
|
36
|
+
def queue_id
|
37
|
+
GetMessageQueueId(@handle)
|
38
|
+
end
|
39
|
+
|
40
|
+
def reconsume_times
|
41
|
+
GetMessageReconsumeTimes(@handle)
|
42
|
+
end
|
43
|
+
|
44
|
+
def store_size
|
45
|
+
GetMessageStoreSize(@handle)
|
46
|
+
end
|
47
|
+
|
48
|
+
def born_timestamp
|
49
|
+
GetMessageBornTimestamp(@handle)
|
50
|
+
end
|
51
|
+
|
52
|
+
def store_timestamp
|
53
|
+
GetMessageStoreTimestamp(@handle)
|
54
|
+
end
|
55
|
+
|
56
|
+
def queue_offset
|
57
|
+
GetMessageQueueOffset(@handle)
|
58
|
+
end
|
59
|
+
|
60
|
+
def commit_log_offset
|
61
|
+
GetMessageCommitLogOffset(@handle)
|
62
|
+
end
|
63
|
+
|
64
|
+
def prepared_transaction_offset
|
65
|
+
GetMessagePreparedTransactionOffset(@handle)
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_property(prop)
|
69
|
+
GetMessageProperty(@handle, prop)
|
70
|
+
end
|
71
|
+
|
72
|
+
def [](key)
|
73
|
+
get_property(key)
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_s
|
77
|
+
body
|
78
|
+
end
|
79
|
+
|
80
|
+
def inspect
|
81
|
+
"<ReceivedMessage topic=#{topic} id=#{id} body=#{body}>"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Client
|
4
|
+
# Response module
|
5
|
+
class Response
|
6
|
+
attr_reader :status, :msg_id, :offset_msg_id
|
7
|
+
def initialize(send_result)
|
8
|
+
@status = send_result[:send_status]
|
9
|
+
@msg_id = send_result[:msg_id]
|
10
|
+
@offset_msg_id = send_result[:offset_msg_id]
|
11
|
+
@raw_result = send_result
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Client
|
4
|
+
# TransactionMQProducer module
|
5
|
+
class TransactionMQProducer < Producer
|
6
|
+
include Rocketmq::C
|
7
|
+
def initialize(group_id, checker_callback, user_args: nil, timeout: nil, compress_level: nil, max_message_size: nil)
|
8
|
+
# super(group_id, timeout, compress_level, max_message_size)
|
9
|
+
@callback_refs = []
|
10
|
+
|
11
|
+
on_check =
|
12
|
+
FFI::Function.new(:int, %i[pointer pointer pointer]) do |_, msg_ptr, _|
|
13
|
+
exc = nil
|
14
|
+
begin
|
15
|
+
msg = ReceivedMessage.new(msg_ptr)
|
16
|
+
check_result = checker_callback.call(msg)
|
17
|
+
if check_result != TransactionStatus[:unknown] &&
|
18
|
+
check_result != TransactionStatus[:commit] &&
|
19
|
+
check_result != TransactionStatus[:rollback]
|
20
|
+
raise StandardError.new('Check transaction status error, please use enum \'TransactionStatus\' as response')
|
21
|
+
end
|
22
|
+
return check_result
|
23
|
+
rescue => ex
|
24
|
+
exc = ex
|
25
|
+
return TransactionStatus[:unknown]
|
26
|
+
ensure
|
27
|
+
raise exc if exc
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
@callback_refs << on_check
|
32
|
+
@producer = CreateTransactionProducer(group_id, on_check, user_args)
|
33
|
+
raise StandardError.new('Returned null pointer when create transaction producer') unless @producer
|
34
|
+
|
35
|
+
set_timeout(timeout) if timeout.to_i.positive?
|
36
|
+
set_compress_level(compress_level) if compress_level
|
37
|
+
set_max_message_size(max_message_size) if max_message_size.to_i.positive?
|
38
|
+
end
|
39
|
+
|
40
|
+
def set_name_server_address(addr)
|
41
|
+
SetProducerNameServerAddress(@producer, addr)
|
42
|
+
end
|
43
|
+
|
44
|
+
def send_message_in_transaction(message, local_execute, user_args: nil)
|
45
|
+
|
46
|
+
on_local_exec =
|
47
|
+
FFI::Function.new(:int, %i[pointer pointer pointer]) do |_, msg_ptr, user_args|
|
48
|
+
exc = nil
|
49
|
+
begin
|
50
|
+
msg = ReceivedMessage.new(msg_ptr)
|
51
|
+
execute_result = local_execute.call(msg)
|
52
|
+
if execute_result != TransactionStatus[:unknown] &&
|
53
|
+
execute_result != TransactionStatus[:commit] &&
|
54
|
+
execute_result != TransactionStatus[:rollback]
|
55
|
+
raise StandardError.new('Local transaction status error, please use enum \'TransactionStatus\' as response')
|
56
|
+
end
|
57
|
+
return execute_result
|
58
|
+
rescue => ex
|
59
|
+
exc = ex
|
60
|
+
return TransactionStatus[:unknown]
|
61
|
+
ensure
|
62
|
+
raise exc if exc
|
63
|
+
end
|
64
|
+
end
|
65
|
+
@callback_refs << on_local_exec
|
66
|
+
c_result = SendResult.new
|
67
|
+
begin
|
68
|
+
SendMessageTransaction(@producer, message.raw, on_local_exec, user_args, c_result.to_ptr)
|
69
|
+
ensure
|
70
|
+
@callback_refs.delete(on_local_exec)
|
71
|
+
end
|
72
|
+
Reponse.new(c_result)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ffi' unless defined?(FFI)
|
4
|
+
|
5
|
+
module Rocketmq
|
6
|
+
# This module mainly defines the export method of librockmqclient
|
7
|
+
module C
|
8
|
+
def self.attach_function_maybe(*args)
|
9
|
+
attach_function(*args)
|
10
|
+
rescue FFI::NotFoundError
|
11
|
+
puts("Missing function: #{args[0]} detail: #{args}")
|
12
|
+
end
|
13
|
+
|
14
|
+
extend FFI::Library
|
15
|
+
ffi_lib %w[librocketmq]
|
16
|
+
|
17
|
+
Status = enum(
|
18
|
+
# Success
|
19
|
+
:ok, 0,
|
20
|
+
# Failed
|
21
|
+
:null_pointer, 1,
|
22
|
+
:malloc_failed, 2,
|
23
|
+
# producer
|
24
|
+
:producer_start_failed, 10,
|
25
|
+
:producer_send_sync_failed, 11,
|
26
|
+
:producer_send_oneway_failed, 12,
|
27
|
+
:producer_send_orderly_failed, 13,
|
28
|
+
:producer_send_async_failed, 14,
|
29
|
+
:producer_send_orderlyasync_failed, 15,
|
30
|
+
:producer_send_transaction_failed, 16,
|
31
|
+
# push consumer
|
32
|
+
:push_consumer_start_failed, 20,
|
33
|
+
# pull consumer
|
34
|
+
:pullconsumer_start_failed, 30,
|
35
|
+
:pullconsumer_fetch_mq_failed, 31,
|
36
|
+
:pullconsumer_fetch_message_failed, 32,
|
37
|
+
|
38
|
+
:not_suport, 500,
|
39
|
+
:not_support_now, -1
|
40
|
+
)
|
41
|
+
|
42
|
+
MessageModel = enum(
|
43
|
+
:broadcasting, 0,
|
44
|
+
:clustering, 1
|
45
|
+
)
|
46
|
+
|
47
|
+
TransactionStatus = enum(
|
48
|
+
:commit, 0,
|
49
|
+
:rollback, 1,
|
50
|
+
:unknown, 2
|
51
|
+
)
|
52
|
+
|
53
|
+
SendStatus = enum(
|
54
|
+
:send_ok, 0,
|
55
|
+
:send_flush_disk_timeout, 1,
|
56
|
+
:send_flush_slave_timeout, 2,
|
57
|
+
:send_slave_not_available, 3
|
58
|
+
)
|
59
|
+
|
60
|
+
ConsumeStatus = enum(
|
61
|
+
:consume_success, 0,
|
62
|
+
:reconsume_later, 1
|
63
|
+
)
|
64
|
+
|
65
|
+
MessageProperty = enum(
|
66
|
+
:trace_switch, 'TRACE_ON',
|
67
|
+
:msg_region, 'MSG_REGION',
|
68
|
+
:keys, 'KEYS',
|
69
|
+
:tags, 'TAGS',
|
70
|
+
:wait_store_msg_ok, 'WAIT',
|
71
|
+
:delay_time_level, 'DELAY',
|
72
|
+
:retry_topic, 'RETRY_TOPIC',
|
73
|
+
:real_topic, 'REAL_TOPIC',
|
74
|
+
:real_queue_id, 'REAL_QID',
|
75
|
+
:transaction_prepared, 'TRAN_MSG',
|
76
|
+
:producer_group, 'PGROUP',
|
77
|
+
:min_offset, 'MIN_OFFSET',
|
78
|
+
:max_offset, 'MAX_OFFSET',
|
79
|
+
:buyer_id, 'BUYER_ID',
|
80
|
+
:origin_message_id, 'ORIGIN_MESSAGE_ID',
|
81
|
+
:transfer_flag, 'TRANSFER_FLAG',
|
82
|
+
:correction_flag, 'CORRECTION_FLAG',
|
83
|
+
:mq2_flag, 'MQ2_FLAG',
|
84
|
+
:reconsume_tiem, 'RECONSUME_TIME',
|
85
|
+
:uniq_client_message_id_keyidx, 'UNIQ_KEY',
|
86
|
+
:max_reconsume_times, 'MAX_RECONSUME_TIMES',
|
87
|
+
:consume_start_timestamp, 'CONSUME_START_TIME'
|
88
|
+
)
|
89
|
+
|
90
|
+
class SendResult < FFI::Struct
|
91
|
+
layout :send_status, SendStatus,
|
92
|
+
:msg_id, :char, 256,
|
93
|
+
:offset_msg_id, :long_long
|
94
|
+
end
|
95
|
+
|
96
|
+
callback :msg_consume_callback, %i[pointer pointer], :int
|
97
|
+
callback :transaction_check_callback, %i[pointer pointer pointer], :int
|
98
|
+
callback :local_transaction_execute_callback, %i[pointer pointer pointer], :int
|
99
|
+
|
100
|
+
# Message
|
101
|
+
attach_function :CreateMessage, [:string], :pointer
|
102
|
+
attach_function :DestroyMessage, [:pointer], Status
|
103
|
+
attach_function :SetMessageKeys, %i[pointer string], Status
|
104
|
+
attach_function :SetMessageTags, %i[pointer string], Status
|
105
|
+
attach_function :SetMessageBody, %i[pointer string], Status
|
106
|
+
attach_function :SetByteMessageBody, %i[pointer string int], Status
|
107
|
+
attach_function :GetMessageProperty, %i[pointer string], :string
|
108
|
+
attach_function :SetMessageProperty, %i[pointer string string], Status
|
109
|
+
attach_function :SetDelayTimeLevel, %i[pointer int], Status
|
110
|
+
|
111
|
+
# TransactionMQProducer
|
112
|
+
attach_function :CreateTransactionProducer, %i[string transaction_check_callback pointer], :pointer
|
113
|
+
attach_function :SendMessageTransaction, %i[pointer pointer local_transaction_execute_callback pointer pointer], :int
|
114
|
+
# Producer
|
115
|
+
attach_function :CreateProducer, [:string], :pointer
|
116
|
+
attach_function :CreateOrderlyProducer, [:string], :pointer
|
117
|
+
attach_function :SetProducerNameServerDomain, %i[pointer string], Status
|
118
|
+
attach_function :SetProducerNameServerAddress, %i[pointer string], Status
|
119
|
+
attach_function :SetProducerSendMsgTimeout, %i[pointer int], Status
|
120
|
+
attach_function :SetProducerCompressLevel, %i[pointer int], Status
|
121
|
+
attach_function :SetProducerMaxMessageSize, %i[pointer int], Status
|
122
|
+
attach_function :SetProducerGroupName, %i[pointer string], Status
|
123
|
+
attach_function :SetProducerInstanceName, %i[pointer string], Status
|
124
|
+
attach_function :SetProducerSessionCredentials, %i[pointer string string string], Status
|
125
|
+
attach_function :SendMessageSync, %i[pointer pointer pointer], Status
|
126
|
+
attach_function :SendMessageOneway, %i[pointer pointer pointer], Status
|
127
|
+
attach_function :SendMessageOrderlyByShardingKey, %i[pointer pointer string pointer], Status
|
128
|
+
attach_function :StartProducer, [:pointer], Status
|
129
|
+
attach_function :ShutdownProducer, [:pointer], Status
|
130
|
+
|
131
|
+
# PushConsumer
|
132
|
+
attach_function :CreatePushConsumer, [:string], :pointer
|
133
|
+
attach_function :DestroyPushConsumer, [:pointer], Status
|
134
|
+
attach_function :SetPushConsumerMessageModel, [:pointer, MessageModel], Status
|
135
|
+
attach_function :StartPushConsumer, [:pointer], Status
|
136
|
+
attach_function :ShutdownPushConsumer, [:pointer], Status
|
137
|
+
attach_function :SetPushConsumerThreadCount, %i[pointer int], Status
|
138
|
+
attach_function :SetPushConsumerMessageBatchMaxSize, %i[pointer int], Status
|
139
|
+
attach_function :SetPushConsumerInstanceName, %i[pointer string], Status
|
140
|
+
attach_function :SetPushConsumerNameServerAddress, %i[pointer string], Status
|
141
|
+
attach_function :SetPushConsumerNameServerDomain, %i[pointer string], Status
|
142
|
+
attach_function :GetPushConsumerGroupID, [:pointer], :string
|
143
|
+
attach_function :SetPushConsumerGroupID, %i[pointer string], Status
|
144
|
+
attach_function :SetPushConsumerSessionCredentials, %i[pointer string string string], Status
|
145
|
+
attach_function :RegisterMessageCallback, %i[pointer msg_consume_callback], Status
|
146
|
+
attach_function :RegisterMessageCallbackOrderly, %i[pointer msg_consume_callback], Status
|
147
|
+
attach_function :UnregisterMessageCallback, [:pointer], Status
|
148
|
+
attach_function :UnregisterMessageCallbackOrderly, [:pointer], Status
|
149
|
+
attach_function :Subscribe, %i[pointer string string], Status
|
150
|
+
|
151
|
+
# PullConsumer
|
152
|
+
attach_function :CreatePullConsumer, [:string], :pointer
|
153
|
+
attach_function :DestroyPullConsumer, [:pointer], Status
|
154
|
+
attach_function :StartPullConsumer, [:pointer], Status
|
155
|
+
attach_function :ShutdownPullConsumer, [:pointer], Status
|
156
|
+
attach_function :GetPullConsumerGroupID, [:pointer], :string
|
157
|
+
attach_function :SetPullConsumerGroupID, %i[pointer string], Status
|
158
|
+
attach_function :SetPullConsumerNameServerAddress, %i[pointer string], Status
|
159
|
+
attach_function :SetPullConsumerNameServerDomain, %i[pointer string], Status
|
160
|
+
attach_function :SetPullConsumerSessionCredentials, %i[pointer string string string], Status
|
161
|
+
attach_function :FetchSubscriptionMessageQueues, %i[pointer string pointer int], :int
|
162
|
+
|
163
|
+
# ReceivedMessage
|
164
|
+
attach_function :GetMessageTopic, [:pointer], :string
|
165
|
+
attach_function :GetMessageTags, [:pointer], :string
|
166
|
+
attach_function :GetMessageKeys, [:pointer], :string
|
167
|
+
attach_function :GetMessageBody, [:pointer], :string
|
168
|
+
attach_function :GetMessageId, [:pointer], :string
|
169
|
+
attach_function :GetMessageDelayTimeLevel, [:pointer], :int
|
170
|
+
attach_function :GetMessageQueueId, [:pointer], :int
|
171
|
+
attach_function :GetMessageReconsumeTimes, [:pointer], :int
|
172
|
+
attach_function :GetMessageStoreSize, [:pointer], :int
|
173
|
+
attach_function :GetMessageBornTimestamp, [:pointer], :long_long
|
174
|
+
attach_function :GetMessageStoreTimestamp, [:pointer], :long_long
|
175
|
+
attach_function :GetMessageQueueOffset, [:pointer], :long_long
|
176
|
+
attach_function :GetMessageCommitLogOffset, [:pointer], :long_long
|
177
|
+
attach_function :GetMessagePreparedTransactionOffset, [:pointer], :long_long
|
178
|
+
attach_function :GetMessageProperty, %i[pointer string], :string
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Rocketmq
|
2
|
+
# :stopdoc:
|
3
|
+
LIBPATH ||= __dir__ + ::File::SEPARATOR
|
4
|
+
PATH ||= ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
5
|
+
# :startdoc:
|
6
|
+
|
7
|
+
# Returns the library path for the module. If any arguments are given,
|
8
|
+
# they will be joined to the end of the libray path using
|
9
|
+
# <tt>File.join</tt>.
|
10
|
+
#
|
11
|
+
def self.libpath(*args)
|
12
|
+
rv = args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
13
|
+
if block_given?
|
14
|
+
begin
|
15
|
+
$LOAD_PATH.unshift LIBPATH
|
16
|
+
rv = yield
|
17
|
+
ensure
|
18
|
+
$LOAD_PATH.shift
|
19
|
+
end
|
20
|
+
end
|
21
|
+
rv
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the lpath for the module. If any arguments are given,
|
25
|
+
# they will be joined to the end of the path using
|
26
|
+
# <tt>File.join</tt>.
|
27
|
+
#
|
28
|
+
def self.path(*args)
|
29
|
+
rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
30
|
+
if block_given?
|
31
|
+
begin
|
32
|
+
$LOAD_PATH.unshift PATH
|
33
|
+
rv = yield
|
34
|
+
ensure
|
35
|
+
$LOAD_PATH.shift
|
36
|
+
end
|
37
|
+
end
|
38
|
+
rv
|
39
|
+
end
|
40
|
+
|
41
|
+
# Utility method used to require all files ending in .rb that lie in the
|
42
|
+
# directory below this file that has the same name as the filename passed
|
43
|
+
# in. Optionally, a specific _directory_ name can be passed in such that
|
44
|
+
# the _filename_ does not have to be equivalent to the directory.
|
45
|
+
#
|
46
|
+
def self.require_all_libs_relative_to(fname, dir = nil)
|
47
|
+
dir ||= ::File.basename(fname, ".*")
|
48
|
+
search_me = ::File.expand_path(
|
49
|
+
::File.join(::File.dirname(fname), dir, "**", "*.rb")
|
50
|
+
)
|
51
|
+
|
52
|
+
Dir.glob(search_me).sort.each { |rb| require rb }
|
53
|
+
end
|
54
|
+
end # module Rocketmq
|
55
|
+
|
56
|
+
require File.join(Rocketmq::LIBPATH, "rocketmq-client-ruby", "rocketmq")
|
57
|
+
require File.join(Rocketmq::LIBPATH, "rocketmq-client-ruby", "client/message")
|
58
|
+
require File.join(Rocketmq::LIBPATH, "rocketmq-client-ruby", "client/producer")
|
59
|
+
require File.join(Rocketmq::LIBPATH, "rocketmq-client-ruby", "client/push_consumer")
|
60
|
+
require File.join(Rocketmq::LIBPATH, "rocketmq-client-ruby", "client/transaction_mq_producer")
|
61
|
+
require File.join(Rocketmq::LIBPATH, "rocketmq-client-ruby", "client/received_message")
|
62
|
+
require File.join(Rocketmq::LIBPATH, "rocketmq-client-ruby", "client/response")
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rocketmq-client-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- EdmondFrank
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
description: A Ruby FFI binding to librocketmq-client-cpp.
|
28
|
+
email:
|
29
|
+
- edmomdfrank@yahoo.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE
|
35
|
+
- lib/rocketmq-client-ruby.rb
|
36
|
+
- lib/rocketmq-client-ruby/client/message.rb
|
37
|
+
- lib/rocketmq-client-ruby/client/producer.rb
|
38
|
+
- lib/rocketmq-client-ruby/client/push_consumer.rb
|
39
|
+
- lib/rocketmq-client-ruby/client/received_message.rb
|
40
|
+
- lib/rocketmq-client-ruby/client/response.rb
|
41
|
+
- lib/rocketmq-client-ruby/client/transaction_mq_producer.rb
|
42
|
+
- lib/rocketmq-client-ruby/rocketmq.rb
|
43
|
+
- lib/rocketmq-client-ruby/version.rb
|
44
|
+
homepage: https://github.com/edmondfrank/rocketmq-client-ruby
|
45
|
+
licenses:
|
46
|
+
- MIT-2.0
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '2.5'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.1.6
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: A Ruby FFI binding to librocketmq-client-cpp.
|
67
|
+
test_files: []
|