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

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

Introduction

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

Prototype

@Fluent
PgSubscriber connect(Handler<AsyncResult<Void>> handler);

Source Link

Document

Connect the subscriber to Postgres.

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  2s  . com*/

    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 -> {
        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);
            });/*  w  w w.j  a va 2s.com*/
            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);
                    });
        }
    });
}