vertx-pg-client.pg_subscriber.rb Maven / Gradle / Ivy
require 'vertx/vertx'
require 'vertx-pg-client/pg_connection'
require 'vertx-pg-client/pg_channel'
require 'vertx/util/utils.rb'
# Generated from com.julienviet.pgclient.pubsub.PgSubscriber
module VertxPgClient
# A class for managing subscriptions using LISTEN/UNLISTEN
to Postgres channels.
#
# The subscriber manages a single connection to Postgres.
class PgSubscriber
# @private
# @param j_del [::VertxPgClient::PgSubscriber] the java delegate
def initialize(j_del)
@j_del = j_del
end
# @private
# @return [::VertxPgClient::PgSubscriber] the underlying java delegate
def j_del
@j_del
end
@@j_api_type = Object.new
def @@j_api_type.accept?(obj)
obj.class == PgSubscriber
end
def @@j_api_type.wrap(obj)
PgSubscriber.new(obj)
end
def @@j_api_type.unwrap(obj)
obj.j_del
end
def self.j_api_type
@@j_api_type
end
def self.j_class
Java::ComJulienvietPgclientPubsub::PgSubscriber.java_class
end
# Create a subscriber.
# @param [::Vertx::Vertx] vertx the vertx instance
# @param [Hash] options the connect options
# @return [::VertxPgClient::PgSubscriber] the subscriber
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::ComJulienvietPgclientPubsub::PgSubscriber.java_method(:subscriber, [Java::IoVertxCore::Vertx.java_class,Java::ComJulienvietPgclient::PgConnectOptions.java_class]).call(vertx.j_del,Java::ComJulienvietPgclient::PgConnectOptions.new(::Vertx::Util::Utils.to_json_object(options))),::VertxPgClient::PgSubscriber)
end
raise ArgumentError, "Invalid arguments when calling subscriber(#{vertx},#{options})"
end
# Return a channel for the given name
.
# @param [String] name the channel name
# @return [::VertxPgClient::PgChannel] the channel
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),::VertxPgClient::PgChannel)
end
raise ArgumentError, "Invalid arguments when calling channel(#{name})"
end
# Connect the subscriber to Postgres.
# @yield the handler notified of the connection success or failure
# @return [self]
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
# 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.
# @yield the policy to set
# @return [self]
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
# Set an handler called when the subscriber is closed.
# @yield the handler
# @return [self]
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
# @return [::VertxPgClient::PgConnection] the actual connection to Postgres, it might be null
def actual_connection
if !block_given?
return ::Vertx::Util::Utils.safe_create(@j_del.java_method(:actualConnection, []).call(),::VertxPgClient::PgConnection)
end
raise ArgumentError, "Invalid arguments when calling actual_connection()"
end
# @return [true,false] whether the subscriber is closed
def closed
if !block_given?
return @j_del.java_method(:closed, []).call()
end
raise ArgumentError, "Invalid arguments when calling closed()"
end
# Close the subscriber, the retry policy will not be invoked.
# @return [void]
def close
if !block_given?
return @j_del.java_method(:close, []).call()
end
raise ArgumentError, "Invalid arguments when calling close()"
end
end
end