diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/pubsub/PgSubscriberImpl.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/pubsub/PgSubscriberImpl.java index f1dde7640..08091773d 100644 --- a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/pubsub/PgSubscriberImpl.java +++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/pubsub/PgSubscriberImpl.java @@ -99,6 +99,7 @@ public synchronized PgSubscriber reconnectPolicy(Function policy) private synchronized void handleClose(Void v) { conn = null; + channels.values().forEach(channel -> channel.listening = false); checkReconnect(0); } @@ -190,6 +191,9 @@ private synchronized void handleConnectResult(Completable completionHandle log.error("Cannot LISTEN to channels", ar2.cause()); conn.close(); } else { + synchronized (PgSubscriberImpl.this) { + channels.values().forEach(channel -> channel.listening = true); + } handlers.forEach(vertx::runOnContext); } completionHandler.complete(null, ar2.cause()); @@ -206,6 +210,7 @@ private class ChannelList { final String quotedName; final ArrayList subs = new ArrayList<>(); boolean subscribed; + boolean listening; ChannelList(String name) { this.name = name; @@ -220,15 +225,23 @@ void add(ChannelImpl sub) { String sql = "LISTEN " + quotedName; conn.query(sql).execute().onComplete(ar -> { if (ar.succeeded()) { - Handler handler = sub.subscribeHandler; - if (handler != null) { - handler.handle(null); + // Notify every channel that joined while the LISTEN was in flight + List> handlers; + synchronized (PgSubscriberImpl.this) { + listening = true; + handlers = subs.stream() + .map(s -> s.subscribeHandler) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } + handlers.forEach(handler -> handler.handle(null)); } else { log.error("Cannot LISTEN to channel " + name, ar.cause()); } }); } + } else if (listening && sub.subscribeHandler != null) { + vertx.runOnContext(sub.subscribeHandler); } } diff --git a/vertx-pg-client/src/test/java/io/vertx/tests/pgclient/PubSubTest.java b/vertx-pg-client/src/test/java/io/vertx/tests/pgclient/PubSubTest.java index af53a6bb5..abc9b503e 100644 --- a/vertx-pg-client/src/test/java/io/vertx/tests/pgclient/PubSubTest.java +++ b/vertx-pg-client/src/test/java/io/vertx/tests/pgclient/PubSubTest.java @@ -132,6 +132,19 @@ private void testConnect(TestContext ctx, String channel1Name, String channel2Na notifiedLatch.awaitSuccess(10000); } + @Test + public void testSecondChannelWithSameNameIsNotifiedOfSubscription(TestContext ctx) { + subscriber = PgSubscriber.subscriber(vertx, options); + Async connected = ctx.async(); + subscriber.connect().onComplete(ctx.asyncAssertSuccess(v -> connected.complete())); + connected.awaitSuccess(10000); + Async first = ctx.async(); + subscriber.channel("the_channel").subscribeHandler(v -> first.complete()).handler(notification -> { }); + first.awaitSuccess(10000); + Async second = ctx.async(); + subscriber.channel("the_channel").subscribeHandler(v -> second.complete()).handler(notification -> { }); + } + @Test public void testSubscribe(TestContext ctx) { testSubscribe(ctx, "the_channel");