Java_Pub Sub_Api - kuyeol/Document GitHub Wiki
μλ‘ μ°κ΄λ μΈν°νμ΄μ€λ€κ³Ό μ μ λ©μλλ€μ Publisher
κ° μμ±ν νλͺ©μ νλ μ΄μμ Subscriber
κ° μλΉνλλ‘ μ€κ³λ νλ¦ μ μ΄ν κ΅¬μ± μμ(flow-controlled components) λ₯Ό μ μν©λλ€.
κ° Subscriber
λ Subscription
μ ν΅ν΄ κ΄λ¦¬λ©λλ€.
μ΄ μΈν°νμ΄μ€λ€μ Reactive Streams λͺ
μΈμ ν΄λΉνλ©°, λμμ± λ° λΆμ° λΉλκΈ° νκ²½ λͺ¨λμμ μ¬μ©λ μ μμ΅λλ€.
λͺ¨λ 7κ°μ λ©μλλ void
λ°ννμ κ°μ§λ©°, μΌλ°©ν₯ λ©μμ§(one-way message) μ€νμΌλ‘ λμν©λλ€.
ν΅μ μ κ°λ¨ν ννμ νλ¦ μ μ΄(flow control)μ μμ‘΄νλλ°, μ΄λ Subscription#request
λ©μλλ₯Ό ν΅ν΄ μ΄λ£¨μ΄μ§λ©°,
"νΈμ κΈ°λ° μμ€ν
(push-based systems)"μμ λ°μν μ μλ 리μμ€ κ΄λ¦¬ λ¬Έμ λ₯Ό λ°©μ§ν μ μμ΅λλ€.
μΌλ°μ μΌλ‘ Publisher
λ μμ λ§μ Subscription
ꡬν체λ₯Ό μ μν©λλ€.
μ΄ κ΅¬ν체λ subscribe
λ©μλμμ μμ±λμ΄, νΈμΆν Subscriber
μκ² μ λ¬λ©λλ€.
νλͺ©μ μΌλ°μ μΌλ‘ λΉλκΈ°μ μΌλ‘ λ°νλλ©°, μ΄λ₯Ό μν΄ Executor
λ₯Ό μ¬μ©νλ κ²½μ°κ° λ§μ΅λλ€.
λ€μμ λ§€μ° λ¨μν Publisher
μ μμμ
λλ€:
- μμ²μ΄ μμ λμλ§
- λ¨ νλμ
TRUE
νλͺ©μ - λ¨ νλμ ꡬλ μμκ² λ°ν(publish)ν©λλ€.
μ΄ μμ μμλ ꡬλ
μκ° μ€μ§ νλμ νλͺ©λ§ λ°κΈ° λλ¬Έμ,
λ²νΌλ§μ΄λ μμ μ μ΄κ° νμ μμ΅λλ€.
(볡μ‘ν ꡬνμμλ SubmissionPublisher
μ κ°μ ν΄λμ€κ° μ΄λ¬ν κΈ°λ₯μ μ 곡ν©λλ€.)
class OneShotPublisher implements Publisher<Boolean> {
private final ExecutorService executor = ForkJoinPool.commonPool(); // daemon-based
private boolean subscribed; // true after first subscribe
public synchronized void subscribe(Subscriber<? super Boolean> subscriber) {
if (subscribed)
subscriber.onError(new IllegalStateException()); // only one allowed
else {
subscribed = true;
subscriber.onSubscribe(new OneShotSubscription(subscriber, executor));
}
}
static class OneShotSubscription implements Subscription {
private final Subscriber<? super Boolean> subscriber;
private final ExecutorService executor;
private Future<?> future; // to allow cancellation
private boolean completed;
OneShotSubscription(Subscriber<? super Boolean> subscriber,
ExecutorService executor) {
this.subscriber = subscriber;
this.executor = executor;
}
public synchronized void request(long n) {
if (!completed) {
completed = true;
if (n <= 0) {
IllegalArgumentException ex = new IllegalArgumentException();
executor.execute(() -> subscriber.onError(ex));
} else {
future = executor.submit(() -> {
subscriber.onNext(Boolean.TRUE);
subscriber.onComplete();
});
}
}
}
public synchronized void cancel() {
completed = true;
if (future != null) future.cancel(false);
}
}
}}
Subscriber
λ νλͺ©(item)μ μμ²νκ³ μ²λ¦¬νλ μν μ λ΄λΉν©λλ€.
Subscriber#onNext
λ©μλλ₯Ό ν΅ν΄ νλͺ©μ΄ μ λ¬λλ©°, μμ²νμ§ μμΌλ©΄ νλͺ©μ μ λ¬λμ§ μμ΅λλ€.
νμ§λ§ ν λ²μ μ¬λ¬ νλͺ©μ μμ²ν μ μμ΅λλ€.
λ§μ Subscriber
ꡬν체λ λ€μ μμ μ€νμΌμ²λΌ λμν μ μμ΅λλ€:
- λ²νΌ ν¬κΈ°(buffer size)λ₯Ό 1λ‘ μ€μ νλ©΄ νλͺ©μ νλμ© λ¨κ³μ μΌλ‘ μ²λ¦¬(single-step)νκ² λ©λλ€.
- λ ν° λ²νΌ ν¬κΈ°λ μ²λ¦¬μ ν΅μ μ λ³λ ¬μ μΌλ‘ ν¨μ¨μ μΌλ‘ μνν μ μκ² λμμ€λλ€.
μλ₯Ό λ€μ΄ λ²νΌ ν¬κΈ°λ₯Ό 64λ‘ μ€μ νλ©΄, μ΄ μμ² μ€ μ²λ¦¬ λκΈ° μ€μΈ νλͺ© μλ 32~64κ° μ¬μ΄λ₯Ό μ μ§νκ² λ©λλ€.
νΉμ Subscription
μ λν Subscriber
μ λ©μλ νΈμΆ μμλ μ격νκ² λ³΄μ₯λλ―λ‘,
λκΈ°ν(synchronized), λ½(lock), volatile λ³μ λ±μ μ¬μ©ν νμκ° μμ΅λλ€.
λ¨, Subscriber
κ° μ¬λ¬ κ°μ Subscription
μ λμμ μ μ§νλ€λ©΄,
μ΄λ³΄λ€λ κ°κ°μ Subscription
μ λν΄ λ³λμ Subscriber
λ₯Ό μ μνλ κ²μ΄ λ μ’μ΅λλ€.
class SampleSubscriber<T> implements Subscriber<T> {
final Consumer<? super T> consumer;
Subscription subscription;
final long bufferSize;
long count;
SampleSubscriber(long bufferSize, Consumer<? super T> consumer) {
this.bufferSize = bufferSize;
this.consumer = consumer;
}
public void onSubscribe(Subscription subscription) {
long initialRequestSize = bufferSize;
count = bufferSize - bufferSize / 2; // re-request when half consumed
(this.subscription = subscription).request(initialRequestSize);
}
public void onNext(T item) {
if (--count <= 0)
subscription.request(count = bufferSize - bufferSize / 2);
consumer.accept(item);
}
public void onError(Throwable ex) { ex.printStackTrace(); }
public void onComplete() {}
}
}
defaultBufferSize
μ κΈ°λ³Έκ°μ λ€μκ³Ό κ°μ κ²½μ°μ μ μ©ν μμμ μ΄ λ μ μμ΅λλ€:
- μμ μ²λ¦¬ μλ(rate)
- μ¬μ© κ°λ₯ν 리μμ€(resources)
- μ¬μ© νν(usages)
μ΄ κ°μ Flow κ΅¬μ± μμμμ μμ² ν¬κΈ°(request size) λ° λ²νΌ μ©λ(capacity) μ μ€μ ν λ κΈ°μ€μ΄ λ©λλ€.
νλ¦ μ μ΄(flow control)κ° μ ν νμνμ§ μμ κ²½μ°,
Subscriber
λ μ²μλΆν° μ¬μ€μ 무μ ν κ°μμ νλͺ©μ μμ²ν μλ μμ΅λλ€.
μλ₯Ό λ€μ΄ λ€μκ³Ό κ°μ΄ μ¬μ©ν μ μμ΅λλ€:
class UnboundedSubscriber<T> implements Subscriber<T> {
public void onSubscribe(Subscription subscription) {
subscription.request(Long.MAX_VALUE); // effectively unbounded
}
public void onNext(T item) { use(item); }
public void onError(Throwable ex) { ex.printStackTrace(); }
public void onComplete() {}
void use(T item) { ... }
public final class Flow {
private Flow() {} // uninstantiable
}
/**
* A producer of items (and related control messages) received by
* Subscribers. Each current {@link Subscriber} receives the same
* items (via method {@code onNext}) in the same order, unless
* drops or errors are encountered. If a Publisher encounters an
* error that does not allow items to be issued to a Subscriber,
* that Subscriber receives {@code onError}, and then receives no
* further messages. Otherwise, when it is known that no further
* messages will be issued to it, a subscriber receives {@code
* onComplete}. Publishers ensure that Subscriber method
* invocations for each subscription are strictly ordered in <a
* href="package-summary.html#MemoryVisibility"><i>happens-before</i></a>
* order.
*
* <p>Publishers may vary in policy about whether drops (failures
* to issue an item because of resource limitations) are treated
* as unrecoverable errors. Publishers may also vary about
* whether Subscribers receive items that were produced or
* available before they subscribed.
*
* @param <T> the published item type
*/
@FunctionalInterface
public static interface Publisher<T> {
/**
* Adds the given Subscriber if possible. If already
* subscribed, or the attempt to subscribe fails due to policy
* violations or errors, the Subscriber's {@code onError}
* method is invoked with an {@link IllegalStateException}.
* Otherwise, the Subscriber's {@code onSubscribe} method is
* invoked with a new {@link Subscription}. Subscribers may
* enable receiving items by invoking the {@code request}
* method of this Subscription, and may unsubscribe by
* invoking its {@code cancel} method.
*
* @param subscriber the subscriber
* @throws NullPointerException if subscriber is null
*/
public void subscribe(Subscriber<? super T> subscriber);
}
/**
* A receiver of messages. The methods in this interface are
* invoked in strict sequential order for each {@link
* Subscription}.
*
* @param <T> the subscribed item type
*/
public static interface Subscriber<T> {
/**
* Method invoked prior to invoking any other Subscriber
* methods for the given Subscription. If this method throws
* an exception, resulting behavior is not guaranteed, but may
* cause the Subscription not to be established or to be cancelled.
*
* <p>Typically, implementations of this method invoke {@code
* subscription.request} to enable receiving items.
*
* @param subscription a new subscription
*/
public void onSubscribe(Subscription subscription);
/**
* Method invoked with a Subscription's next item. If this
* method throws an exception, resulting behavior is not
* guaranteed, but may cause the Subscription to be cancelled.
*
* @param item the item
*/
public void onNext(T item);
/**
* Method invoked upon an unrecoverable error encountered by a
* Publisher or Subscription, after which no other Subscriber
* methods are invoked by the Subscription. If this method
* itself throws an exception, resulting behavior is
* undefined.
*
* @param throwable the exception
*/
public void onError(Throwable throwable);
/**
* Method invoked when it is known that no additional
* Subscriber method invocations will occur for a Subscription
* that is not already terminated by error, after which no
* other Subscriber methods are invoked by the Subscription.
* If this method throws an exception, resulting behavior is
* undefined.
*/
public void onComplete();
}
/**
* Message control linking a {@link Publisher} and {@link
* Subscriber}. Subscribers receive items only when requested,
* and may cancel at any time. The methods in this interface are
* intended to be invoked only by their Subscribers; usages in
* other contexts have undefined effects.
*/
public static interface Subscription {
/**
* Adds the given number {@code n} of items to the current
* unfulfilled demand for this subscription. If {@code n} is
* less than or equal to zero, the Subscriber will receive an
* {@code onError} signal with an {@link
* IllegalArgumentException} argument. Otherwise, the
* Subscriber will receive up to {@code n} additional {@code
* onNext} invocations (or fewer if terminated).
*
* @param n the increment of demand; a value of {@code
* Long.MAX_VALUE} may be considered as effectively unbounded
*/
public void request(long n);
/**
* Causes the Subscriber to (eventually) stop receiving
* messages. Implementation is best-effort -- additional
* messages may be received after invoking this method.
* A cancelled subscription need not ever receive an
* {@code onComplete} or {@code onError} signal.
*/
public void cancel();
}
/**
* A component that acts as both a Subscriber and Publisher.
*
* @param <T> the subscribed item type
* @param <R> the published item type
*/
public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> {
}
static final int DEFAULT_BUFFER_SIZE = 256;
/**
* Returns a default value for Publisher or Subscriber buffering,
* that may be used in the absence of other constraints.
*
* @implNote
* The current value returned is 256.
*
* @return the buffer size value
*/
public static int defaultBufferSize() {
return DEFAULT_BUFFER_SIZE;
}
}