Example usage for io.vertx.pgclient.pubsub PgSubscriber subscriber

List of usage examples for io.vertx.pgclient.pubsub PgSubscriber subscriber

Introduction

In this page you can find the example usage for io.vertx.pgclient.pubsub PgSubscriber subscriber.

Prototype

static PgSubscriber subscriber(Vertx vertx, PgConnectOptions options) 

Source Link

Document

Create a subscriber.

Usage

From source file:examples.PgClientExamples.java

License:Apache License

public void pubsub02(Vertx vertx) {

    PgSubscriber subscriber = PgSubscriber.subscriber(vertx, new PgConnectOptions().setPort(5432)
            .setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"));

    // You can set the channel before connect
    subscriber.channel("channel1").handler(payload -> {
        System.out.println("Received " + payload);
    });//  w w  w . j a  va 2 s  . c o  m

    subscriber.connect(ar -> {
        if (ar.succeeded()) {

            // Or you can set the channel after connect
            subscriber.channel("channel2").handler(payload -> {
                System.out.println("Received " + payload);
            });
        }
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void pubsub03(Vertx vertx) {

    PgSubscriber subscriber = PgSubscriber.subscriber(vertx, new PgConnectOptions().setPort(5432)
            .setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"));

    subscriber.connect(ar -> {// ww w  .  ja v a  2 s.  com
        if (ar.succeeded()) {
            // Complex channel name - name in PostgreSQL requires a quoted ID
            subscriber.channel("Complex.Channel.Name").handler(payload -> {
                System.out.println("Received " + payload);
            });
            subscriber.channel("Complex.Channel.Name").subscribeHandler(subscribed -> {
                subscriber.actualConnection().query("NOTIFY \"Complex.Channel.Name\", 'msg'", notified -> {
                    System.out.println("Notified \"Complex.Channel.Name\"");
                });
            });

            // PostgreSQL simple ID's are forced lower-case
            subscriber.channel("simple_channel").handler(payload -> {
                System.out.println("Received " + payload);
            });
            subscriber.channel("simple_channel").subscribeHandler(subscribed -> {
                // The following simple channel identifier is forced to lower case
                subscriber.actualConnection().query("NOTIFY Simple_CHANNEL, 'msg'", notified -> {
                    System.out.println("Notified simple_channel");
                });
            });

            // The following channel name is longer than the current
            // (NAMEDATALEN = 64) - 1 == 63 character limit and will be truncated
            subscriber.channel("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb")
                    .handler(payload -> {
                        System.out.println("Received " + payload);
                    });
        }
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void pubsub04(Vertx vertx) {

    PgSubscriber subscriber = PgSubscriber.subscriber(vertx, new PgConnectOptions().setPort(5432)
            .setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"));

    // Reconnect at most 10 times after 100 ms each
    subscriber.reconnectPolicy(retries -> {
        if (retries < 10) {
            return 100L;
        } else {//from w  w w  .  ja  v a  2  s  .c  o m
            return -1L;
        }
    });
}