pulsar-client 2.4.1.pre.beta.1 → 2.4.1.pre.beta.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e63f527c666d454aa9d86692fdf7ce62c5f0d8b9
4
- data.tar.gz: f486f65c4a9b00e7dd7de8e5603a04976e93ef70
3
+ metadata.gz: bdac30fc88a91584ef35377492dbbb8dbde43cb1
4
+ data.tar.gz: f258ede389a3034b8f783f586489db1e15e8d781
5
5
  SHA512:
6
- metadata.gz: bc954d9604c218777a5f7f3e22a7c23fd4457fafe9c5d6daeb33980d8cd45e549c6f6f32abf0e465ecedf07996a47bb34245c9b003133b27bfef00ceaec62227
7
- data.tar.gz: e2d4d814048247c1d79061950767cafc9bc15793908a58c099d1ba4242cc889e38083fa4857f80cbe2a51cfa4155545d00d3e9587a7c78987cf662e547500a68
6
+ metadata.gz: 3dedd175bbbbe43ec40925cbbd55e738157272a89a4a0b27635b51adad5fef51e5faf284c0e683365c9d83a6b4904407673ccfe1670424a89b924fe74d97ff5c
7
+ data.tar.gz: 30cb9298ceca90c1178759d9b08f64951947e0c4c9464af6f1386dccb1402ca77aae900ba01a00e6f615b1bda8a2b20bdb6f8bb09f88a7e36e837600599a27bb
data/README.md CHANGED
@@ -27,7 +27,7 @@ the `.pre` suffix in the Gemfile to install it via Bundler.
27
27
 
28
28
  ## Usage
29
29
 
30
- Example:
30
+ Setup and basic `consumer.receive` example:
31
31
 
32
32
  ```ruby
33
33
  # have these in your shell with appropriate values
@@ -48,10 +48,35 @@ producer.send("Hello, world!")
48
48
  # named "hello-consumer"
49
49
  subscription = "hello-consumer"
50
50
  consumer = client.subscribe(topic, subscription)
51
- consumer.listen do |message|
52
- puts message
53
- break
51
+
52
+ msg = consumer.receive
53
+ message = msg.data
54
+ puts "got #{message}"
55
+ consumer.acknolwedge(msg)
56
+ ```
57
+
58
+ Convenience method for listening to messages in a loop:
59
+
60
+ ```ruby
61
+ consumer.listen do |message, _, done|
62
+ # process message here; call done to stop the loop.
63
+ # messages are auto-acknowledged.
64
+ puts "got #{message}"
65
+ done.call()
66
+ end
67
+ ```
68
+
69
+ Convenience method for listening on a separate thread:
70
+
71
+ ```ruby
72
+ listenerThread = consumer.listen_in_thread do |message, _, done|
73
+ # process message here; call done to stop the loop.
74
+ # messages are auto-acknowledged.
75
+ puts "got #{message}"
76
+ done.call()
54
77
  end
78
+ # ...
79
+ listenerThread.join # wait for the thread to finish
55
80
  ```
56
81
 
57
82
  (more documentation coming; see TODO.md)
@@ -4,6 +4,7 @@
4
4
  #include "producer.hpp"
5
5
  #include "consumer.hpp"
6
6
  #include "client.hpp"
7
+ #include "util.hpp"
7
8
 
8
9
  using namespace Rice;
9
10
 
@@ -11,6 +12,7 @@ extern "C"
11
12
  void Init_bindings()
12
13
  {
13
14
  Module rb_mPulsar = define_module("Pulsar");
15
+ bind_errors(rb_mPulsar);
14
16
  bind_message(rb_mPulsar);
15
17
  bind_producer(rb_mPulsar);
16
18
  bind_consumer(rb_mPulsar);
@@ -11,25 +11,30 @@ namespace pulsar_rb {
11
11
 
12
12
  typedef struct {
13
13
  pulsar::Consumer& consumer;
14
+ unsigned int timeout_ms;
14
15
  pulsar::Message message;
15
16
  pulsar::Result result;
16
17
  } consumer_receive_job;
17
18
 
18
19
  void* consumer_receive_nogvl(void* jobPtr) {
19
20
  consumer_receive_job& job = *(consumer_receive_job*)jobPtr;
20
- job.result = job.consumer.receive(job.message);
21
+ if (job.timeout_ms > 0) {
22
+ job.result = job.consumer.receive(job.message, job.timeout_ms);
23
+ } else {
24
+ job.result = job.consumer.receive(job.message);
25
+ }
21
26
  return nullptr;
22
27
  }
23
28
 
24
- pulsar::Message consumer_receive(pulsar::Consumer& consumer) {
25
- consumer_receive_job job = { consumer };
29
+ pulsar::Message consumer_receive(pulsar::Consumer& consumer, unsigned int timeout_ms) {
30
+ consumer_receive_job job = { consumer, timeout_ms };
26
31
  rb_thread_call_without_gvl(&consumer_receive_nogvl, &job, RUBY_UBF_IO, nullptr);
27
32
  CheckResult(job.result);
28
33
  return job.message;
29
34
  }
30
35
 
31
- Message::ptr Consumer::receive() {
32
- pulsar::Message message = consumer_receive(_consumer);
36
+ Message::ptr Consumer::receive(unsigned int timeout_ms) {
37
+ pulsar::Message message = consumer_receive(_consumer, timeout_ms);
33
38
  return Message::ptr(new Message(message));
34
39
  }
35
40
 
@@ -48,7 +53,7 @@ using namespace Rice;
48
53
  void bind_consumer(Module &module) {
49
54
  define_class_under<pulsar_rb::Consumer>(module, "Consumer")
50
55
  .define_constructor(Constructor<pulsar_rb::Consumer>())
51
- .define_method("receive", &pulsar_rb::Consumer::receive)
56
+ .define_method("receive", &pulsar_rb::Consumer::receive, (Arg("timeout_ms") = 0))
52
57
  .define_method("acknowledge", &pulsar_rb::Consumer::acknowledge)
53
58
  .define_method("negative_acknowledge", &pulsar_rb::Consumer::negative_acknowledge)
54
59
  ;
@@ -14,7 +14,7 @@ namespace pulsar_rb {
14
14
  Consumer() {};
15
15
  Consumer(const pulsar::Consumer& consumer) : _consumer(consumer) {}
16
16
 
17
- Message::ptr receive();
17
+ Message::ptr receive(unsigned int timeout_ms=0);
18
18
  void acknowledge(const Message& message);
19
19
  void negative_acknowledge(const Message& message);
20
20
 
@@ -1,11 +1,23 @@
1
1
  #include <pulsar/Client.h>
2
2
 
3
3
  #include "util.hpp"
4
+ #include "rice/Exception.hpp"
4
5
 
5
- using namespace pulsar;
6
+ using namespace Rice;
6
7
 
7
- void CheckResult(Result res) {
8
- if (res != ResultOk) {
9
- throw PulsarException(res);
8
+ VALUE rb_ePulsarError = Qnil;
9
+ VALUE rb_ePulsarError_Timeout = Qnil;
10
+
11
+ void bind_errors(Module &module) {
12
+ rb_ePulsarError = rb_define_class_under(module.value(), "Error", rb_eStandardError);
13
+ rb_ePulsarError_Timeout = rb_define_class_under(rb_ePulsarError, "Timeout", rb_ePulsarError);
14
+ }
15
+
16
+ void CheckResult(pulsar::Result res) {
17
+ if (res == pulsar::ResultTimeout) {
18
+ throw Exception(rb_ePulsarError_Timeout, "pulsar timeout");
19
+ }
20
+ else if (res != ResultOk) {
21
+ throw Exception(rb_ePulsarError, "unexpected pulsar exception: %d", res);
10
22
  }
11
23
  }
@@ -1,16 +1,13 @@
1
1
  #ifndef __PULSAR_RUBY_CLIENT_UTIL_HPP
2
2
  #define __PULSAR_RUBY_CLIENT_UTIL_HPP
3
3
 
4
+ #include "rice/Module.hpp"
4
5
  #include <pulsar/Client.h>
5
6
 
6
7
  using namespace pulsar;
7
8
 
8
- struct PulsarException {
9
- Result _result;
10
- PulsarException(Result res) :
11
- _result(res) {}
12
- };
13
-
14
9
  void CheckResult(Result res);
15
10
 
11
+ void bind_errors(Rice::Module& module);
12
+
16
13
  #endif
@@ -19,6 +19,6 @@
19
19
 
20
20
  module Pulsar
21
21
  class Client
22
- VERSION = "2.4.1-beta.1"
22
+ VERSION = "2.4.1-beta.2"
23
23
  end
24
24
  end
data/lib/pulsar/client.rb CHANGED
@@ -37,8 +37,10 @@ module Pulsar
37
37
  super(topic, config)
38
38
  end
39
39
 
40
- def subscribe(topic, subscription_name, config=nil)
41
- config ||= Pulsar::ConsumerConfiguration.new
40
+ def subscribe(topic, subscription_name, config={})
41
+ unless config.is_a?(Pulsar::ConsumerConfiguration)
42
+ config = Pulsar::ConsumerConfiguration.new(config)
43
+ end
42
44
  super(topic, subscription_name, config)
43
45
  end
44
46
  end
@@ -45,7 +45,7 @@ module Pulsar
45
45
  end
46
46
 
47
47
  def listen_in_thread
48
- Thread.new { listen }
48
+ Thread.new { listen {|*args| yield *args }}
49
49
  end
50
50
  end
51
51
  end
@@ -34,6 +34,11 @@ module Pulsar
34
34
  }
35
35
 
36
36
  module RubySideTweaks
37
+ def initialize(config={})
38
+ super()
39
+ self.consumer_type = config.consumer_type if config.has?(:consumer_type)
40
+ end
41
+
37
42
  def consumer_type
38
43
  enum_value = super
39
44
  CONSUMER_TYPES.invert[enum_value]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pulsar-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.1.pre.beta.1
4
+ version: 2.4.1.pre.beta.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Fugal