Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for listening to notifications #52

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions src/main/java/io/r2dbc/postgresql/PostgresqlConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
import io.r2dbc.postgresql.client.SimpleQueryMessageFlow;
import io.r2dbc.postgresql.client.TransactionStatus;
import io.r2dbc.postgresql.codec.Codecs;
import io.r2dbc.postgresql.message.backend.NotificationResponse;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.spi.Connection;
import io.r2dbc.spi.IsolationLevel;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;
import java.util.function.Function;

import static io.r2dbc.postgresql.client.TransactionStatus.IDLE;
Expand Down Expand Up @@ -165,6 +168,12 @@ public Mono<Void> rollbackTransactionToSavepoint(String name) {
});
}

public Disposable addNotificationListener(Consumer<NotificationResponse> consumer) {
Assert.requireNonNull(consumer, "consumer must not be null");

return this.client.addNotificationListener(consumer);
}

@Override
public Mono<Void> setTransactionIsolationLevel(IsolationLevel isolationLevel) {
Assert.requireNonNull(isolationLevel, "isolationLevel must not be null");
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/r2dbc/postgresql/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

import io.netty.buffer.ByteBufAllocator;
import io.r2dbc.postgresql.message.backend.BackendMessage;
import io.r2dbc.postgresql.message.backend.NotificationResponse;
import io.r2dbc.postgresql.message.backend.ReadyForQuery;
import io.r2dbc.postgresql.message.frontend.FrontendMessage;
import org.reactivestreams.Publisher;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Optional;
import java.util.function.Consumer;

/**
* An abstraction that wraps the networking part of exchanging methods.
Expand Down Expand Up @@ -75,4 +78,12 @@ public interface Client {
*/
TransactionStatus getTransactionStatus();

/**
* Add a consumer of notification messages.
*
* @param consumer the consumer of notification messages
* @return a new {@link Disposable} that can be used to cancel the underlying subscription.
* @throws IllegalArgumentException if {@code consumer} is {@code null}
*/
Disposable addNotificationListener(Consumer<NotificationResponse> consumer);
}
14 changes: 14 additions & 0 deletions src/main/java/io/r2dbc/postgresql/client/ReactorNettyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import io.r2dbc.postgresql.message.backend.ErrorResponse;
import io.r2dbc.postgresql.message.backend.Field;
import io.r2dbc.postgresql.message.backend.NoticeResponse;
import io.r2dbc.postgresql.message.backend.NotificationResponse;
import io.r2dbc.postgresql.message.backend.ReadyForQuery;
import io.r2dbc.postgresql.message.frontend.FrontendMessage;
import io.r2dbc.postgresql.message.frontend.Terminate;
import io.r2dbc.postgresql.util.Assert;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.Disposable;
import reactor.core.publisher.EmitterProcessor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
Expand All @@ -50,6 +52,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -104,6 +107,11 @@ public final class ReactorNettyClient implements Client {
sink.next(message);
});

private final EmitterProcessor<NotificationResponse> notificationProcessor = EmitterProcessor.create(false);

private final BiConsumer<BackendMessage, SynchronousSink<BackendMessage>> handleNotificationResponse = handleBackendMessage(NotificationResponse.class,
(message, sink) -> this.notificationProcessor.onNext(message));

/**
* Creates a new frame processor connected to a given TCP connection.
*
Expand All @@ -124,6 +132,7 @@ private ReactorNettyClient(Connection connection) {
.concatMap(decoder::decode)
.doOnNext(message -> this.logger.debug("Response: {}", message))
.handle(this.handleNoticeResponse)
.handle(this.handleNotificationResponse)
.handle(this.handleErrorResponse)
.handle(this.handleBackendKeyData)
.handle(this.handleReadyForQuery)
Expand Down Expand Up @@ -258,6 +267,11 @@ public TransactionStatus getTransactionStatus() {
return this.transactionStatus.get();
}

@Override
public Disposable addNotificationListener(Consumer<NotificationResponse> consumer) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better to expose Flux<NotificationResponse> here?

return this.notificationProcessor.subscribe(consumer);
}

@SuppressWarnings("unchecked")
private static <T extends BackendMessage> BiConsumer<BackendMessage, SynchronousSink<BackendMessage>> handleBackendMessage(Class<T> type, BiConsumer<T, SynchronousSink<BackendMessage>> consumer) {
return (message, sink) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
import io.r2dbc.postgresql.authentication.PasswordAuthenticationHandler;
import io.r2dbc.postgresql.message.backend.CommandComplete;
import io.r2dbc.postgresql.message.backend.DataRow;
import io.r2dbc.postgresql.message.backend.NotificationResponse;
import io.r2dbc.postgresql.message.backend.RowDescription;
import io.r2dbc.postgresql.message.frontend.Query;
import io.r2dbc.postgresql.util.PostgresqlServerExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import reactor.core.Disposable;
import reactor.core.publisher.FluxProcessor;
import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoProcessor;
import reactor.core.publisher.UnicastProcessor;
import reactor.test.StepVerifier;

import java.util.Arrays;
Expand Down Expand Up @@ -142,4 +147,55 @@ void parallelExchange() {
.verifyComplete();
}

@Test
void handleNotify() {
UnicastProcessor<NotificationResponse> response = UnicastProcessor.create();
this.client.addNotificationListener(response::onNext);

this.client
.exchange(Mono.just(new Query("LISTEN events")))
.blockLast();

SERVER.getJdbcOperations().execute("NOTIFY events, 'test'");

StepVerifier.create(response)
.assertNext(message -> assertThat(message.getPayload()).isEqualTo("test"))
.thenCancel()
.verify();
}

@Test
void handleTrigger() {
SERVER.getJdbcOperations().execute(
"CREATE OR REPLACE FUNCTION notify_event() RETURNS TRIGGER AS $$\n" +
" DECLARE\n" +
" payload JSON;\n" +
" BEGIN\n" +
" payload = row_to_json(NEW);\n" +
" PERFORM pg_notify('events', payload::text);\n" +
" RETURN NULL;\n" +
" END;\n" +
"$$ LANGUAGE plpgsql;");

SERVER.getJdbcOperations().execute(
"CREATE TRIGGER notify_test_event\n" +
"AFTER INSERT OR UPDATE OR DELETE ON test\n" +
" FOR EACH ROW EXECUTE PROCEDURE notify_event();");

UnicastProcessor<NotificationResponse> response = UnicastProcessor.create();
this.client.addNotificationListener(response::onNext);

this.client
.exchange(Mono.just(new Query("LISTEN events")))
.blockLast();

SERVER.getJdbcOperations().execute("INSERT INTO test VALUES (100)");
SERVER.getJdbcOperations().execute("INSERT INTO test VALUES (1000)");

StepVerifier.create(response)
.assertNext(message -> assertThat(message.getPayload()).isEqualTo("{\"value\":100}"))
.assertNext(message -> assertThat(message.getPayload()).isEqualTo("{\"value\":1000}"))
.thenCancel()
.verify();
}
}
10 changes: 10 additions & 0 deletions src/test/java/io/r2dbc/postgresql/client/TestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import io.netty.buffer.ByteBufAllocator;
import io.r2dbc.postgresql.message.backend.BackendMessage;
import io.r2dbc.postgresql.message.backend.NotificationResponse;
import io.r2dbc.postgresql.message.frontend.FrontendMessage;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.postgresql.util.TestByteBufAllocator;
import org.reactivestreams.Publisher;
import reactor.core.Disposable;
import reactor.core.publisher.EmitterProcessor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
Expand All @@ -31,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

import static io.r2dbc.postgresql.client.TransactionStatus.IDLE;
Expand All @@ -53,6 +56,8 @@ public final class TestClient implements Client {

private final TransactionStatus transactionStatus;

private final EmitterProcessor<NotificationResponse> notificationProcessor = EmitterProcessor.create(false);

private TestClient(boolean expectClose, @Nullable Integer processId, @Nullable Integer secretKey, Flux<Window> windows, TransactionStatus transactionStatus) {
this.expectClose = expectClose;
this.processId = processId;
Expand Down Expand Up @@ -120,6 +125,11 @@ public TransactionStatus getTransactionStatus() {
return this.transactionStatus;
}

@Override
public Disposable addNotificationListener(Consumer<NotificationResponse> consumer) {
return this.notificationProcessor.subscribe(consumer);
}

public static final class Builder {

private final List<Window.Builder<?>> windows = new ArrayList<>();
Expand Down