Class: ReactivePgClient::PgSubscriber

Inherits:
Object
  • Object
show all
Defined in:
/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb

Overview

A class for managing subscriptions using LISTEN/UNLISTEN to Postgres channels.

The subscriber manages a single connection to Postgres.

Constant Summary

@@j_api_type =
Object.new

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Boolean) accept?(obj)

Returns:

  • (Boolean)

22
23
24
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 22

def @@j_api_type.accept?(obj)
  obj.class == PgSubscriber
end

+ (Object) j_api_type


31
32
33
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 31

def self.j_api_type
  @@j_api_type
end

+ (Object) j_class


34
35
36
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 34

def self.j_class
  Java::IoReactiversePgclientPubsub::PgSubscriber.java_class
end

+ (::ReactivePgClient::PgSubscriber) subscriber(vertx = nil, options = nil)

Create a subscriber.

Parameters:

  • vertx (::Vertx::Vertx) (defaults to: nil)
    the vertx instance
  • options (Hash) (defaults to: nil)
    the connect options

Returns:

Raises:

  • (ArgumentError)

41
42
43
44
45
46
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 41

def self.subscriber(vertx=nil,options=nil)
  if vertx.class.method_defined?(:j_del) && options.class == Hash && !block_given?
    return ::Vertx::Util::Utils.safe_create(Java::IoReactiversePgclientPubsub::PgSubscriber.java_method(:subscriber, [Java::IoVertxCore::Vertx.java_class,Java::IoReactiversePgclient::PgConnectOptions.java_class]).call(vertx.j_del,Java::IoReactiversePgclient::PgConnectOptions.new(::Vertx::Util::Utils.to_json_object(options))),::ReactivePgClient::PgSubscriber)
  end
  raise ArgumentError, "Invalid arguments when calling subscriber(#{vertx},#{options})"
end

+ (Object) unwrap(obj)


28
29
30
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 28

def @@j_api_type.unwrap(obj)
  obj.j_del
end

+ (Object) wrap(obj)


25
26
27
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 25

def @@j_api_type.wrap(obj)
  PgSubscriber.new(obj)
end

Instance Method Details

- (::ReactivePgClient::PgConnection) actual_connection

Returns the actual connection to Postgres, it might be null

Returns:

Raises:

  • (ArgumentError)

97
98
99
100
101
102
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 97

def actual_connection
  if !block_given?
    return ::Vertx::Util::Utils.safe_create(@j_del.java_method(:actualConnection, []).call(),::ReactivePgClient::PgConnection)
  end
  raise ArgumentError, "Invalid arguments when calling actual_connection()"
end

- (::ReactivePgClient::PgChannel) channel(name = nil)

Return a channel for the given name.

Parameters:

  • name (String) (defaults to: nil)
    the channel name

    This will be the name of the channel exactly as held by Postgres for sending notifications. Internally this name will be truncated to the Postgres identifier maxiumum length of (NAMEDATALEN = 64) - 1 == 63 characters, and prepared as a quoted identifier without unicode escape sequence support for use in LISTEN/UNLISTEN commands. Examples of channel names and corresponding NOTIFY commands:

    • when name == "the_channel": NOTIFY the_channel, 'msg', NOTIFY The_Channel, 'msg', or NOTIFY "the_channel", 'msg' succeed in delivering a message to the created channel
    • when name == "The_Channel": NOTIFY "The_Channel", 'msg', succeeds in delivering a message to the created channel

Returns:

Raises:

  • (ArgumentError)

50
51
52
53
54
55
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 50

def channel(name=nil)
  if name.class == String && !block_given?
    return ::Vertx::Util::Utils.safe_create(@j_del.java_method(:channel, [Java::java.lang.String.java_class]).call(name),::ReactivePgClient::PgChannel)
  end
  raise ArgumentError, "Invalid arguments when calling channel(#{name})"
end

- (void) close

This method returns an undefined value.

Close the subscriber, the retry policy will not be invoked.

Raises:

  • (ArgumentError)

112
113
114
115
116
117
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 112

def close
  if !block_given?
    return @j_del.java_method(:close, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling close()"
end

- (self) close_handler { ... }

Set an handler called when the subscriber is closed.

Yields:

  • the handler

Returns:

  • (self)

Raises:

  • (ArgumentError)

89
90
91
92
93
94
95
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 89

def close_handler
  if block_given?
    @j_del.java_method(:closeHandler, [Java::IoVertxCore::Handler.java_class]).call(Proc.new { yield })
    return self
  end
  raise ArgumentError, "Invalid arguments when calling close_handler()"
end

- (true, false) closed?

Returns whether the subscriber is closed

Returns:

  • (true, false)
    whether the subscriber is closed

Raises:

  • (ArgumentError)

104
105
106
107
108
109
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 104

def closed?
  if !block_given?
    return @j_del.java_method(:closed, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling closed?()"
end

- (self) connect { ... }

Connect the subscriber to Postgres.

Yields:

  • the handler notified of the connection success or failure

Returns:

  • (self)

Raises:

  • (ArgumentError)

59
60
61
62
63
64
65
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 59

def connect
  if block_given?
    @j_del.java_method(:connect, [Java::IoVertxCore::Handler.java_class]).call((Proc.new { |ar| yield(ar.failed ? ar.cause : nil) }))
    return self
  end
  raise ArgumentError, "Invalid arguments when calling connect()"
end

- (self) reconnect_policy(policy = nil) { ... }

Set the reconnect policy that is executed when the subscriber is disconnected.

When the subscriber is disconnected, the policy function is called with the actual number of retries and returns an amountOfTime value:

  • when amountOfTime < 0: the subscriber is closed and there is no retry
  • when amountOfTime == 0: the subscriber retries to connect immediately
  • when amountOfTime > 0: the subscriber retries after amountOfTime milliseconds

The default policy does not perform any retries.

Yields:

  • the policy to set

Returns:

  • (self)

Raises:

  • (ArgumentError)

79
80
81
82
83
84
85
# File '/Users/julien/java/reactive-pg-client/target/classes/reactive-pg-client/pg_subscriber.rb', line 79

def reconnect_policy(policy=nil)
  if block_given? && policy == nil
    @j_del.java_method(:reconnectPolicy, [Java::JavaUtilFunction::Function.java_class]).call((Proc.new { |event| yield(event) }))
    return self
  end
  raise ArgumentError, "Invalid arguments when calling reconnect_policy(#{policy})"
end