vertx-pg-client-js.pg_subscriber.js Maven / Gradle / Ivy
/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/** @module vertx-pg-client-js/pg_subscriber */
var utils = require('vertx-js/util/utils');
var Vertx = require('vertx-js/vertx');
var PgConnection = require('vertx-pg-client-js/pg_connection');
var PgChannel = require('vertx-pg-client-js/pg_channel');
var io = Packages.io;
var JsonObject = io.vertx.core.json.JsonObject;
var JPgSubscriber = Java.type('com.julienviet.pgclient.pubsub.PgSubscriber');
var PgConnectOptions = Java.type('com.julienviet.pgclient.PgConnectOptions');
/**
@class
*/
var PgSubscriber = function(j_val) {
var j_pgSubscriber = j_val;
var that = this;
/**
Return a channel for the given name
.
@public
@param name {string} the channel name
@return {PgChannel} the channel
*/
this.channel = function(name) {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] === 'string') {
return utils.convReturnVertxGen(PgChannel, j_pgSubscriber["channel(java.lang.String)"](name));
} else throw new TypeError('function invoked with invalid arguments');
};
/**
Connect the subscriber to Postgres.
@public
@param handler {function} the handler notified of the connection success or failure
@return {PgSubscriber} a reference to this, so the API can be used fluently
*/
this.connect = function(handler) {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] === 'function') {
j_pgSubscriber["connect(io.vertx.core.Handler)"](function(ar) {
if (ar.succeeded()) {
handler(null, null);
} else {
handler(null, ar.cause());
}
});
return that;
} else throw new TypeError('function invoked with invalid arguments');
};
/**
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.
@public
@param policy {todo} the policy to set
@return {PgSubscriber} a reference to this, so the API can be used fluently
*/
this.reconnectPolicy = function(policy) {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] === 'function') {
j_pgSubscriber["reconnectPolicy(java.util.function.Function)"](function(jVal) {
var jRet = policy(jVal);
return utils.convParamLong(jRet);
});
return that;
} else throw new TypeError('function invoked with invalid arguments');
};
/**
Set an handler called when the subscriber is closed.
@public
@param handler {function} the handler
@return {PgSubscriber} a reference to this, so the API can be used fluently
*/
this.closeHandler = function(handler) {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] === 'function') {
j_pgSubscriber["closeHandler(io.vertx.core.Handler)"](handler);
return that;
} else throw new TypeError('function invoked with invalid arguments');
};
/**
@public
@return {PgConnection} the actual connection to Postgres, it might be null
*/
this.actualConnection = function() {
var __args = arguments;
if (__args.length === 0) {
return utils.convReturnVertxGen(PgConnection, j_pgSubscriber["actualConnection()"]());
} else throw new TypeError('function invoked with invalid arguments');
};
/**
@public
@return {boolean} whether the subscriber is closed
*/
this.closed = function() {
var __args = arguments;
if (__args.length === 0) {
return j_pgSubscriber["closed()"]();
} else throw new TypeError('function invoked with invalid arguments');
};
/**
Close the subscriber, the retry policy will not be invoked.
@public
*/
this.close = function() {
var __args = arguments;
if (__args.length === 0) {
j_pgSubscriber["close()"]();
} else throw new TypeError('function invoked with invalid arguments');
};
// A reference to the underlying Java delegate
// NOTE! This is an internal API and must not be used in user code.
// If you rely on this property your code is likely to break if we change it / remove it without warning.
this._jdel = j_pgSubscriber;
};
PgSubscriber._jclass = utils.getJavaClass("com.julienviet.pgclient.pubsub.PgSubscriber");
PgSubscriber._jtype = {
accept: function(obj) {
return PgSubscriber._jclass.isInstance(obj._jdel);
},
wrap: function(jdel) {
var obj = Object.create(PgSubscriber.prototype, {});
PgSubscriber.apply(obj, arguments);
return obj;
},
unwrap: function(obj) {
return obj._jdel;
}
};
PgSubscriber._create = function(jdel) {
var obj = Object.create(PgSubscriber.prototype, {});
PgSubscriber.apply(obj, arguments);
return obj;
}
/**
Create a subscriber.
@memberof module:vertx-pg-client-js/pg_subscriber
@param vertx {Vertx} the vertx instance
@param options {Object} the connect options
@return {PgSubscriber} the subscriber
*/
PgSubscriber.subscriber = function(vertx, options) {
var __args = arguments;
if (__args.length === 2 && typeof __args[0] === 'object' && __args[0]._jdel && (typeof __args[1] === 'object' && __args[1] != null)) {
return utils.convReturnVertxGen(PgSubscriber, JPgSubscriber["subscriber(io.vertx.core.Vertx,com.julienviet.pgclient.PgConnectOptions)"](vertx._jdel, options != null ? new PgConnectOptions(new JsonObject(Java.asJSONCompatible(options))) : null));
} else throw new TypeError('function invoked with invalid arguments');
};
module.exports = PgSubscriber;