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

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

Introduction

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

Prototype

PgChannel channel(String name);

Source Link

Document

Return a channel for the given name .

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);
    });//from   w  ww  . ja  v a2 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 -> {/*from  ww  w. j av a 2 s . c om*/
        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);
                    });
        }
    });
}