|
| 1 | +/* |
| 2 | + * Copyright 2015-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.rsocket.core; |
| 17 | + |
| 18 | +import io.rsocket.Payload; |
| 19 | +import io.rsocket.RSocket; |
| 20 | +import org.reactivestreams.Publisher; |
| 21 | +import reactor.core.Disposable; |
| 22 | +import reactor.core.publisher.Flux; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +/** |
| 26 | + * Contract for performing RSocket requests. |
| 27 | + * |
| 28 | + * <p>{@link RSocketClient} differs from {@link RSocket} in a number of ways: |
| 29 | + * |
| 30 | + * <ul> |
| 31 | + * <li>{@code RSocket} represents a "live" connection that is transient and needs to be obtained |
| 32 | + * typically from a {@code Mono<RSocket>} source via {@code flatMap} or block. By contrast, |
| 33 | + * {@code RSocketClient} is a higher level layer that contains such a {@link #source() source} |
| 34 | + * of connections and transparently obtains and re-obtains a shared connection as needed when |
| 35 | + * requests are made concurrently. That means an {@code RSocketClient} can simply be created |
| 36 | + * once, even before a connection is established, and shared as a singleton across multiple |
| 37 | + * places as you would with any other client. |
| 38 | + * <li>For request input {@code RSocket} accepts an instance of {@code Payload} and does not allow |
| 39 | + * more than one subscription per request because there is no way to safely re-use that input. |
| 40 | + * By contrast {@code RSocketClient} accepts {@code Publisher<Payload>} and allow |
| 41 | + * re-subscribing which repeats the request. |
| 42 | + * <li>{@code RSocket} can be used for sending and it can also be implemented for receiving. By |
| 43 | + * contrast {@code RSocketClient} is used only for sending, typically from the client side |
| 44 | + * which allows obtaining and re-obtaining connections from a source as needed. However it can |
| 45 | + * also be used from the server side by {@link #from(RSocket) wrapping} the "live" {@code |
| 46 | + * RSocket} for a given connection. |
| 47 | + * </ul> |
| 48 | + * |
| 49 | + * <p>The example below shows how to create an {@code RSocketClient}: |
| 50 | + * |
| 51 | + * <pre class="code">{@code |
| 52 | + * Mono<RSocket> source = |
| 53 | + * RSocketConnector.create() |
| 54 | + * .metadataMimeType("message/x.rsocket.composite-metadata.v0") |
| 55 | + * .dataMimeType("application/cbor") |
| 56 | + * .connect(TcpClientTransport.create("localhost", 7000)); |
| 57 | + * |
| 58 | + * RSocketClient client = RSocketClient.from(source); |
| 59 | + * }</pre> |
| 60 | + * |
| 61 | + * <p>The below configures retry logic to use when a shared {@code RSocket} connection is obtained: |
| 62 | + * |
| 63 | + * <pre class="code">{@code |
| 64 | + * Mono<RSocket> source = |
| 65 | + * RSocketConnector.create() |
| 66 | + * .metadataMimeType("message/x.rsocket.composite-metadata.v0") |
| 67 | + * .dataMimeType("application/cbor") |
| 68 | + * .reconnect(Retry.fixedDelay(3, Duration.ofSeconds(1))) |
| 69 | + * .connect(TcpClientTransport.create("localhost", 7000)); |
| 70 | + * |
| 71 | + * RSocketClient client = RSocketClient.from(source); |
| 72 | + * }</pre> |
| 73 | + * |
| 74 | + * @since 1.1 |
| 75 | + * @see io.rsocket.loadbalance.LoadbalanceRSocketClient |
| 76 | + */ |
| 77 | +public interface RSocketClient extends Disposable { |
| 78 | + |
| 79 | + /** Return the underlying source used to obtain a shared {@link RSocket} connection. */ |
| 80 | + Mono<RSocket> source(); |
| 81 | + |
| 82 | + /** |
| 83 | + * Perform a Fire-and-Forget interaction via {@link RSocket#fireAndForget(Payload)}. Allows |
| 84 | + * multiple subscriptions and performs a request per subscriber. |
| 85 | + */ |
| 86 | + Mono<Void> fireAndForget(Mono<Payload> payloadMono); |
| 87 | + |
| 88 | + /** |
| 89 | + * Perform a Request-Response interaction via {@link RSocket#requestResponse(Payload)}. Allows |
| 90 | + * multiple subscriptions and performs a request per subscriber. |
| 91 | + */ |
| 92 | + Mono<Payload> requestResponse(Mono<Payload> payloadMono); |
| 93 | + |
| 94 | + /** |
| 95 | + * Perform a Request-Stream interaction via {@link RSocket#requestStream(Payload)}. Allows |
| 96 | + * multiple subscriptions and performs a request per subscriber. |
| 97 | + */ |
| 98 | + Flux<Payload> requestStream(Mono<Payload> payloadMono); |
| 99 | + |
| 100 | + /** |
| 101 | + * Perform a Request-Channel interaction via {@link RSocket#requestChannel(Publisher)}. Allows |
| 102 | + * multiple subscriptions and performs a request per subscriber. |
| 103 | + */ |
| 104 | + Flux<Payload> requestChannel(Publisher<Payload> payloads); |
| 105 | + |
| 106 | + /** |
| 107 | + * Perform a Metadata Push via {@link RSocket#metadataPush(Payload)}. Allows multiple |
| 108 | + * subscriptions and performs a request per subscriber. |
| 109 | + */ |
| 110 | + Mono<Void> metadataPush(Mono<Payload> payloadMono); |
| 111 | + |
| 112 | + /** |
| 113 | + * Create an {@link RSocketClient} that obtains shared connections as needed, when requests are |
| 114 | + * made, from the given {@code Mono<RSocket>} source. |
| 115 | + * |
| 116 | + * @param source the source for connections, typically prepared via {@link RSocketConnector}. |
| 117 | + * @return the created client instance |
| 118 | + */ |
| 119 | + static RSocketClient from(Mono<RSocket> source) { |
| 120 | + return new DefaultRSocketClient(source); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Adapt the given {@link RSocket} to use as {@link RSocketClient}. This is useful to wrap the |
| 125 | + * sending {@code RSocket} in a server. |
| 126 | + * |
| 127 | + * <p><strong>Note:</strong> unlike an {@code RSocketClient} created via {@link |
| 128 | + * RSocketClient#from(Mono)}, the instance returned from this factory method can only perform |
| 129 | + * requests for as long as the given {@code RSocket} remains "live". |
| 130 | + * |
| 131 | + * @param rsocket the {@code RSocket} to perform requests with |
| 132 | + * @return the created client instance |
| 133 | + */ |
| 134 | + static RSocketClient from(RSocket rsocket) { |
| 135 | + return new RSocketClientAdapter(rsocket); |
| 136 | + } |
| 137 | +} |
0 commit comments