Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public synchronized PgSubscriber reconnectPolicy(Function<Integer, Long> policy)

private synchronized void handleClose(Void v) {
conn = null;
channels.values().forEach(channel -> channel.listening = false);
checkReconnect(0);
}

Expand Down Expand Up @@ -190,6 +191,9 @@ private synchronized void handleConnectResult(Completable<Void> 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());
Expand All @@ -206,6 +210,7 @@ private class ChannelList {
final String quotedName;
final ArrayList<ChannelImpl> subs = new ArrayList<>();
boolean subscribed;
boolean listening;

ChannelList(String name) {
this.name = name;
Expand All @@ -220,15 +225,23 @@ void add(ChannelImpl sub) {
String sql = "LISTEN " + quotedName;
conn.query(sql).execute().onComplete(ar -> {
if (ar.succeeded()) {
Handler<Void> handler = sub.subscribeHandler;
if (handler != null) {
handler.handle(null);
// Notify every channel that joined while the LISTEN was in flight
List<Handler<Void>> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down