pulsar-client 2.4.1.pre.beta.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
4
+
5
+ require "pulsar/client"
6
+ require "irb"
7
+
8
+ IRB.start(__FILE__)
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #
4
+ # Licensed to the Apache Software Foundation (ASF) under one
5
+ # or more contributor license agreements. See the NOTICE file
6
+ # distributed with this work for additional information
7
+ # regarding copyright ownership. The ASF licenses this file
8
+ # to you under the Apache License, Version 2.0 (the
9
+ # "License"); you may not use this file except in compliance
10
+ # with the License. You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing,
15
+ # software distributed under the License is distributed on an
16
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
+ # KIND, either express or implied. See the License for the
18
+ # specific language governing permissions and limitations
19
+ # under the License.
20
+ #
21
+
22
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
23
+
24
+ require "pulsar/client"
25
+
26
+ unless ARGV.size >= 1 && ARGV.size <= 2 && !Pulsar::Client.sufficient_environment?
27
+ puts "Usage: #{__FILE__} <topic> [consumer-name]"
28
+ puts
29
+ puts "If not specified, the consumer name defaults to 'example-consumer'."
30
+ puts
31
+ puts "Additionally, the PULSAR_BROKER_URI environment variable must be set. Optional"
32
+ puts "PULSAR_CERT_PATH and PULSAR_AUTH_TOKEN environment variables are also"
33
+ puts "recognized."
34
+ exit 1
35
+ end
36
+
37
+ topic = ARGV[0]
38
+ consumer_name = ARGV[1] || 'example-consumer'
39
+ client = Pulsar::Client.from_environment
40
+ consumer = client.subscribe(topic, consumer_name)
41
+ consumer.listen do |message, id, finish|
42
+ puts "Received message '#{message}' [id: #{id}]"
43
+ finish.call if message == "exit"
44
+ end
45
+ client.close
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #
4
+ # Licensed to the Apache Software Foundation (ASF) under one
5
+ # or more contributor license agreements. See the NOTICE file
6
+ # distributed with this work for additional information
7
+ # regarding copyright ownership. The ASF licenses this file
8
+ # to you under the Apache License, Version 2.0 (the
9
+ # "License"); you may not use this file except in compliance
10
+ # with the License. You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing,
15
+ # software distributed under the License is distributed on an
16
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
+ # KIND, either express or implied. See the License for the
18
+ # specific language governing permissions and limitations
19
+ # under the License.
20
+ #
21
+
22
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
23
+
24
+ require "pulsar/client"
25
+
26
+ unless ARGV.size == 1 && !Pulsar::Client.sufficient_environment?
27
+ puts "Usage: #{__FILE__} <topic>"
28
+ puts
29
+ puts "Additionally, the PULSAR_BROKER_URI environment variable must be set. Optional"
30
+ puts "PULSAR_CERT_PATH and PULSAR_AUTH_TOKEN environment variables are also"
31
+ puts "recognized."
32
+ exit 1
33
+ end
34
+
35
+ topic = ARGV[0]
36
+ client = Pulsar::Client.from_environment
37
+ producer = client.create_producer(topic)
38
+ while data = gets
39
+ data.chomp!
40
+ producer.send(data)
41
+ break if data == "exit"
42
+ end
43
+ client.close
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,18 @@
1
+ #include "rice/Module.hpp"
2
+
3
+ #include "message.hpp"
4
+ #include "producer.hpp"
5
+ #include "consumer.hpp"
6
+ #include "client.hpp"
7
+
8
+ using namespace Rice;
9
+
10
+ extern "C"
11
+ void Init_bindings()
12
+ {
13
+ Module rb_mPulsar = define_module("Pulsar");
14
+ bind_message(rb_mPulsar);
15
+ bind_producer(rb_mPulsar);
16
+ bind_consumer(rb_mPulsar);
17
+ bind_client(rb_mPulsar);
18
+ }
@@ -0,0 +1,187 @@
1
+ #include "rice/Data_Type.hpp"
2
+ #include "rice/Constructor.hpp"
3
+ #include <pulsar/Client.h>
4
+ #include <ruby/thread.h>
5
+
6
+ #include "client.hpp"
7
+ #include "util.hpp"
8
+
9
+ namespace pulsar_rb {
10
+
11
+ ClientConfiguration::ClientConfiguration() : _config() {
12
+ }
13
+
14
+ void ClientConfiguration::setAuthFromToken(const std::string &token) {
15
+ _config.setAuth(pulsar::AuthToken::createWithToken(token));
16
+ }
17
+
18
+ int ClientConfiguration::getOperationTimeoutSeconds() {
19
+ return _config.getOperationTimeoutSeconds();
20
+ }
21
+
22
+ void ClientConfiguration::setOperationTimeoutSeconds(int timeout) {
23
+ _config.setOperationTimeoutSeconds(timeout);
24
+ }
25
+
26
+ int ClientConfiguration::getIOThreads() {
27
+ return _config.getIOThreads();
28
+ }
29
+
30
+ void ClientConfiguration::setIOThreads(int threads) {
31
+ _config.setIOThreads(threads);
32
+ }
33
+
34
+ int ClientConfiguration::getMessageListenerThreads() {
35
+ return _config.getMessageListenerThreads();
36
+ }
37
+
38
+ void ClientConfiguration::setMessageListenerThreads(int threads) {
39
+ _config.setMessageListenerThreads(threads);
40
+ }
41
+
42
+ int ClientConfiguration::getConcurrentLookupRequest() {
43
+ return _config.getConcurrentLookupRequest();
44
+ }
45
+
46
+ void ClientConfiguration::setConcurrentLookupRequest(int n) {
47
+ _config.setConcurrentLookupRequest(n);
48
+ }
49
+
50
+ std::string ClientConfiguration::getLogConfFilePath() {
51
+ return _config.getLogConfFilePath();
52
+ }
53
+
54
+ void ClientConfiguration::setLogConfFilePath(const std::string& path) {
55
+ _config.setLogConfFilePath(path);
56
+ }
57
+
58
+ bool ClientConfiguration::isUseTls() {
59
+ return _config.isUseTls();
60
+ }
61
+
62
+ void ClientConfiguration::setUseTls(bool enable) {
63
+ _config.setUseTls(enable);
64
+ }
65
+
66
+ std::string ClientConfiguration::getTlsTrustCertsFilePath() {
67
+ return _config.getTlsTrustCertsFilePath();
68
+ }
69
+
70
+ void ClientConfiguration::setTlsTrustCertsFilePath(const std::string& path) {
71
+ _config.setTlsTrustCertsFilePath(path);
72
+ }
73
+
74
+ bool ClientConfiguration::isTlsAllowInsecureConnection() {
75
+ return _config.isTlsAllowInsecureConnection();
76
+ }
77
+
78
+ void ClientConfiguration::setTlsAllowInsecureConnection(bool enable) {
79
+ _config.setTlsAllowInsecureConnection(enable);
80
+ }
81
+
82
+ bool ClientConfiguration::isValidateHostName() {
83
+ return _config.isValidateHostName();
84
+ }
85
+
86
+ void ClientConfiguration::setValidateHostName(bool enable) {
87
+ _config.setValidateHostName(enable);
88
+ }
89
+
90
+ Client::Client(Rice::String service_url, const ClientConfiguration& config) : _client(service_url.str(), config._config) {
91
+ }
92
+
93
+ typedef struct {
94
+ pulsar::Client& client;
95
+ const Rice::String& topic;
96
+ const pulsar::ProducerConfiguration& config;
97
+ pulsar::Producer producer;
98
+ pulsar::Result result;
99
+ } client_create_producer_task;
100
+
101
+ void* client_create_producer_worker(void* taskPtr) {
102
+ client_create_producer_task& task = *(client_create_producer_task*)taskPtr;
103
+ task.result = task.client.createProducer(task.topic.str(), task.config, task.producer);
104
+ return nullptr;
105
+ }
106
+
107
+ Producer::ptr Client::create_producer(Rice::String topic, const ProducerConfiguration& config) {
108
+ client_create_producer_task task = { _client, topic, config };
109
+ rb_thread_call_without_gvl(&client_create_producer_worker, &task, RUBY_UBF_IO, nullptr);
110
+ CheckResult(task.result);
111
+ return Producer::ptr(new Producer(task.producer));
112
+ }
113
+
114
+ typedef struct {
115
+ pulsar::Client& client;
116
+ const Rice::String& topic;
117
+ const Rice::String& subscriptionName;
118
+ const pulsar::ConsumerConfiguration& config;
119
+ pulsar::Consumer consumer;
120
+ pulsar::Result result;
121
+ } client_subscribe_task;
122
+
123
+ void* client_subscribe_worker(void* taskPtr) {
124
+ client_subscribe_task& task = *(client_subscribe_task*)taskPtr;
125
+ task.result = task.client.subscribe(task.topic.str(), task.subscriptionName.str(), task.config, task.consumer);
126
+ return nullptr;
127
+ }
128
+
129
+ Consumer::ptr Client::subscribe(Rice::String topic, Rice::String subscriptionName, const ConsumerConfiguration& config) {
130
+ client_subscribe_task task = { _client, topic, subscriptionName, config };
131
+ rb_thread_call_without_gvl(&client_subscribe_worker, &task, RUBY_UBF_IO, nullptr);
132
+ CheckResult(task.result);
133
+ return Consumer::ptr(new Consumer(task.consumer));
134
+ }
135
+
136
+ typedef struct {
137
+ pulsar::Client& client;
138
+ pulsar::Result result;
139
+ } client_close_task;
140
+
141
+ void* client_close_worker(void* taskPtr) {
142
+ client_close_task& task = *(client_close_task*)taskPtr;
143
+ task.result = task.client.close();
144
+ return nullptr;
145
+ }
146
+
147
+ void Client::close() {
148
+ client_close_task task = { _client };
149
+ rb_thread_call_without_gvl(&client_close_worker, &task, RUBY_UBF_IO, nullptr);
150
+ CheckResult(task.result);
151
+ }
152
+
153
+ }
154
+
155
+ using namespace Rice;
156
+
157
+ void bind_client(Module& module) {
158
+ define_class_under<pulsar_rb::Client>(module, "Client")
159
+ .define_constructor(Constructor<pulsar_rb::Client, const std::string&, const pulsar_rb::ClientConfiguration&>())
160
+ .define_method("create_producer", &pulsar_rb::Client::create_producer)
161
+ .define_method("subscribe", &pulsar_rb::Client::subscribe)
162
+ .define_method("close", &pulsar_rb::Client::close)
163
+ ;
164
+
165
+ define_class_under<pulsar_rb::ClientConfiguration>(module, "ClientConfiguration")
166
+ .define_constructor(Constructor<pulsar_rb::ClientConfiguration>())
167
+ .define_method("authentication_token=", &pulsar_rb::ClientConfiguration::setAuthFromToken)
168
+ .define_method("operation_timeout_seconds", &pulsar_rb::ClientConfiguration::getOperationTimeoutSeconds)
169
+ .define_method("operation_timeout_seconds=", &pulsar_rb::ClientConfiguration::setOperationTimeoutSeconds)
170
+ .define_method("io_threads", &pulsar_rb::ClientConfiguration::getIOThreads)
171
+ .define_method("io_threads=", &pulsar_rb::ClientConfiguration::setIOThreads)
172
+ .define_method("message_listener_threads", &pulsar_rb::ClientConfiguration::getMessageListenerThreads)
173
+ .define_method("message_listener_threads=", &pulsar_rb::ClientConfiguration::setMessageListenerThreads)
174
+ .define_method("concurrent_lookup_requests", &pulsar_rb::ClientConfiguration::getConcurrentLookupRequest)
175
+ .define_method("concurrent_lookup_requests=", &pulsar_rb::ClientConfiguration::setConcurrentLookupRequest)
176
+ .define_method("log_conf_file_path", &pulsar_rb::ClientConfiguration::getLogConfFilePath)
177
+ .define_method("log_conf_file_path=", &pulsar_rb::ClientConfiguration::setLogConfFilePath)
178
+ .define_method("use_tls?", &pulsar_rb::ClientConfiguration::isUseTls)
179
+ .define_method("use_tls=", &pulsar_rb::ClientConfiguration::setUseTls)
180
+ .define_method("tls_trust_certs_file_path", &pulsar_rb::ClientConfiguration::getTlsTrustCertsFilePath)
181
+ .define_method("tls_trust_certs_file_path=", &pulsar_rb::ClientConfiguration::setTlsTrustCertsFilePath)
182
+ .define_method("tls_allow_insecure_connection?", &pulsar_rb::ClientConfiguration::isTlsAllowInsecureConnection)
183
+ .define_method("tls_allow_insecure_connection=", &pulsar_rb::ClientConfiguration::setTlsAllowInsecureConnection)
184
+ .define_method("tls_validate_hostname?", &pulsar_rb::ClientConfiguration::isValidateHostName)
185
+ .define_method("tls_validate_hostname=", &pulsar_rb::ClientConfiguration::setValidateHostName)
186
+ ;
187
+ }
@@ -0,0 +1,56 @@
1
+ #ifndef __PULSAR_RUBY_CLIENT_CLIENT_HPP
2
+ #define __PULSAR_RUBY_CLIENT_CLIENT_HPP
3
+
4
+ #include "rice/Module.hpp"
5
+ #include "rice/String.hpp"
6
+ #include "rice/Data_Object.hpp"
7
+ #include <pulsar/Client.h>
8
+
9
+ #include "producer.hpp"
10
+ #include "consumer.hpp"
11
+
12
+ namespace pulsar_rb {
13
+ class ClientConfiguration {
14
+ public:
15
+ pulsar::ClientConfiguration _config;
16
+ ClientConfiguration();
17
+
18
+ void setAuthFromToken(const std::string &token);
19
+ int getOperationTimeoutSeconds();
20
+ void setOperationTimeoutSeconds(int timeout);
21
+ int getIOThreads();
22
+ void setIOThreads(int threads);
23
+ int getMessageListenerThreads();
24
+ void setMessageListenerThreads(int threads);
25
+ int getConcurrentLookupRequest();
26
+ void setConcurrentLookupRequest(int n);
27
+ std::string getLogConfFilePath();
28
+ void setLogConfFilePath(const std::string& path);
29
+ bool isUseTls();
30
+ void setUseTls(bool enable);
31
+ std::string getTlsTrustCertsFilePath();
32
+ void setTlsTrustCertsFilePath(const std::string& path);
33
+ bool isTlsAllowInsecureConnection();
34
+ void setTlsAllowInsecureConnection(bool enable);
35
+ bool isValidateHostName();
36
+ void setValidateHostName(bool enable);
37
+
38
+ typedef Rice::Data_Object<ClientConfiguration> ptr;
39
+ };
40
+
41
+ class Client {
42
+ public:
43
+ pulsar::Client _client;
44
+ Client(Rice::String service_url, const ClientConfiguration& config);
45
+
46
+ Producer::ptr create_producer(Rice::String topic, const ProducerConfiguration& config);
47
+ Consumer::ptr subscribe(Rice::String topic, Rice::String subscriptionName, const ConsumerConfiguration& config);
48
+ void close();
49
+
50
+ typedef Rice::Data_Object<Client> ptr;
51
+ };
52
+ };
53
+
54
+ void bind_client(Rice::Module &module);
55
+
56
+ #endif
@@ -0,0 +1,90 @@
1
+ #include "rice/Data_Type.hpp"
2
+ #include "rice/Enum.hpp"
3
+ #include "rice/Constructor.hpp"
4
+ #include <pulsar/Client.h>
5
+ #include <ruby/thread.h>
6
+
7
+ #include "consumer.hpp"
8
+ #include "util.hpp"
9
+
10
+ namespace pulsar_rb {
11
+
12
+ typedef struct {
13
+ pulsar::Consumer& consumer;
14
+ pulsar::Message message;
15
+ pulsar::Result result;
16
+ } consumer_receive_job;
17
+
18
+ void* consumer_receive_nogvl(void* jobPtr) {
19
+ consumer_receive_job& job = *(consumer_receive_job*)jobPtr;
20
+ job.result = job.consumer.receive(job.message);
21
+ return nullptr;
22
+ }
23
+
24
+ pulsar::Message consumer_receive(pulsar::Consumer& consumer) {
25
+ consumer_receive_job job = { consumer };
26
+ rb_thread_call_without_gvl(&consumer_receive_nogvl, &job, RUBY_UBF_IO, nullptr);
27
+ CheckResult(job.result);
28
+ return job.message;
29
+ }
30
+
31
+ Message::ptr Consumer::receive() {
32
+ pulsar::Message message = consumer_receive(_consumer);
33
+ return Message::ptr(new Message(message));
34
+ }
35
+
36
+ void Consumer::acknowledge(const Message& message) {
37
+ _consumer.acknowledgeAsync(message._msg, nullptr);
38
+ }
39
+
40
+ void Consumer::negative_acknowledge(const Message& message) {
41
+ _consumer.negativeAcknowledge(message._msg);
42
+ }
43
+
44
+ }
45
+
46
+ using namespace Rice;
47
+
48
+ void bind_consumer(Module &module) {
49
+ define_class_under<pulsar_rb::Consumer>(module, "Consumer")
50
+ .define_constructor(Constructor<pulsar_rb::Consumer>())
51
+ .define_method("receive", &pulsar_rb::Consumer::receive)
52
+ .define_method("acknowledge", &pulsar_rb::Consumer::acknowledge)
53
+ .define_method("negative_acknowledge", &pulsar_rb::Consumer::negative_acknowledge)
54
+ ;
55
+
56
+ define_enum<pulsar::ConsumerType>("ConsumerType", module)
57
+ .define_value("Exclusive", ConsumerExclusive)
58
+ .define_value("Shared", ConsumerShared)
59
+ .define_value("Failover", ConsumerFailover)
60
+ .define_value("KeyShared", ConsumerKeyShared);
61
+
62
+ define_class_under<pulsar_rb::ConsumerConfiguration>(module, "ConsumerConfiguration")
63
+ .define_constructor(Constructor<pulsar_rb::ConsumerConfiguration>())
64
+ .define_method("consumer_type", &ConsumerConfiguration::getConsumerType)
65
+ .define_method("consumer_type=", &ConsumerConfiguration::setConsumerType)
66
+ // TODO .define_method("schema", &ConsumerConfiguration::getSchema)
67
+ // TODO .define_method("schema=", &ConsumerConfiguration::setSchema)
68
+ // TODO .define_method("message_listener", &ConsumerConfiguration_setMessageListener)
69
+ .define_method("receiver_queue_size", &ConsumerConfiguration::getReceiverQueueSize)
70
+ .define_method("receiver_queue_size=", &ConsumerConfiguration::setReceiverQueueSize)
71
+ .define_method("max_total_receiver_queue_size_across_partitions", &ConsumerConfiguration::getMaxTotalReceiverQueueSizeAcrossPartitions)
72
+ .define_method("max_total_receiver_queue_size_across_partitions=", &ConsumerConfiguration::setMaxTotalReceiverQueueSizeAcrossPartitions)
73
+ .define_method("consumer_name", &ConsumerConfiguration::getConsumerName)
74
+ .define_method("consumer_name=", &ConsumerConfiguration::setConsumerName)
75
+ .define_method("unacked_messages_timeout_ms", &ConsumerConfiguration::getUnAckedMessagesTimeoutMs)
76
+ .define_method("unacked_messages_timeout_ms=", &ConsumerConfiguration::setUnAckedMessagesTimeoutMs)
77
+ .define_method("negative_ack_redelivery_delay_ms", &ConsumerConfiguration::getNegativeAckRedeliveryDelayMs)
78
+ .define_method("negative_ack_redelivery_delay_ms=", &ConsumerConfiguration::setNegativeAckRedeliveryDelayMs)
79
+ .define_method("broker_consumer_stats_cache_time_ms", &ConsumerConfiguration::getBrokerConsumerStatsCacheTimeInMs)
80
+ .define_method("broker_consumer_stats_cache_time_ms=", &ConsumerConfiguration::setBrokerConsumerStatsCacheTimeInMs)
81
+ .define_method("pattern_auto_discovery_period", &ConsumerConfiguration::getPatternAutoDiscoveryPeriod)
82
+ .define_method("pattern_auto_discovery_period=", &ConsumerConfiguration::setPatternAutoDiscoveryPeriod)
83
+ .define_method("read_compacted?", &ConsumerConfiguration::isReadCompacted)
84
+ .define_method("read_compacted=", &ConsumerConfiguration::setReadCompacted)
85
+ .define_method("subscription_initial_position", &ConsumerConfiguration::getSubscriptionInitialPosition)
86
+ .define_method("subscription_initial_position=", &ConsumerConfiguration::setSubscriptionInitialPosition)
87
+ .define_method("[]", &ConsumerConfiguration::getProperty)
88
+ .define_method("[]=", &ConsumerConfiguration::setProperty)
89
+ ;
90
+ }