use of org.reactivestreams.Subscriber in project reactive-streams-jvm by reactive-streams.
the class PublisherVerificationTest method multiSubscribersPublisherVerification.
/**
* Verification using a Publisher that supports multiple subscribers
* @param shouldBlowUp if true {@link RuntimeException} will be thrown during second subscription.
*/
final PublisherVerification<Integer> multiSubscribersPublisherVerification(final boolean shouldBlowUp) {
return new PublisherVerification<Integer>(newTestEnvironment()) {
@Override
public Publisher<Integer> createPublisher(final long elements) {
return new Publisher<Integer>() {
private final Collection<CancelableSubscription> subscriptions = new CopyOnWriteArrayList<CancelableSubscription>();
private final AtomicLong source = new AtomicLong(elements);
@Override
public void subscribe(Subscriber<? super Integer> s) {
// onSubscribe first
CancelableSubscription subscription = new CancelableSubscription(s);
s.onSubscribe(subscription);
if (shouldBlowUp && !subscriptions.isEmpty()) {
s.onError(new RuntimeException("Unexpected additional subscriber"));
} else {
subscriptions.add(subscription);
}
}
class CancelableSubscription implements Subscription {
final AtomicBoolean canceled = new AtomicBoolean();
Subscriber<? super Integer> subscriber;
CancelableSubscription(Subscriber<? super Integer> subscriber) {
this.subscriber = subscriber;
}
@Override
public void request(long n) {
if (!canceled.get()) {
for (long i = 0; i < n; i++) {
if (source.getAndDecrement() < 0) {
canceled.set(true);
subscriber.onComplete();
} else {
subscriber.onNext((int) i);
}
}
}
}
@Override
public void cancel() {
canceled.set(true);
subscriber = null;
subscriptions.remove(this);
}
}
};
}
@Override
public Publisher<Integer> createFailedPublisher() {
return SKIP;
}
};
}
use of org.reactivestreams.Subscriber in project jOOQ by jOOQ.
the class R2DBC method block.
@SuppressWarnings("unchecked")
static final <T> T block(Publisher<? extends T> publisher) {
Object complete = new Object();
LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<>();
publisher.subscribe(subscriber(s -> s.request(1), queue::add, queue::add, () -> queue.add(complete)));
try {
Object result = queue.take();
if (result instanceof Throwable)
throw new DataAccessException("Exception when blocking on publisher", (Throwable) result);
else if (result == complete)
return null;
else
return (T) result;
} catch (InterruptedException e) {
throw new DataAccessException("Exception when blocking on publisher", e);
}
}
use of org.reactivestreams.Subscriber in project aws-doc-sdk-examples by awsdocs.
the class KinesisStreamEx method main.
public static void main(String[] args) {
// snippet-start:[kinesis.java2.stream_example.setup]
Region region = Region.US_EAST_1;
KinesisAsyncClient client = KinesisAsyncClient.builder().region(region).build();
SubscribeToShardRequest request = SubscribeToShardRequest.builder().consumerARN(CONSUMER_ARN).shardId("arn:aws:kinesis:us-east-1:111122223333:stream/StockTradeStream").startingPosition(s -> s.type(ShardIteratorType.LATEST)).build();
// snippet-end:[kinesis.java2.stream_example.setup]
SubscribeToShardResponseHandler responseHandler = SubscribeToShardResponseHandler.builder().onError(t -> System.err.println("Error during stream - " + t.getMessage())).subscriber(MySubscriber::new).build();
client.subscribeToShard(request, responseHandler);
client.close();
}
use of org.reactivestreams.Subscriber in project aws-doc-sdk-examples by awsdocs.
the class KinesisStreamEx method responseHandlerBuilderVisitorBuilder.
// snippet-end:[kinesis.java2.stream_example.publish_transformer]
/**
* Creates a SubscribeToShardResponseHandler.Visitor using the builder which lets you register an event handler for
* all events you're interested in rather than implementing the interface.
*/
// snippet-start:[kinesis.java2.stream_example.visitor]
private static CompletableFuture<Void> responseHandlerBuilderVisitorBuilder(KinesisAsyncClient client, SubscribeToShardRequest request) {
SubscribeToShardResponseHandler.Visitor visitor = SubscribeToShardResponseHandler.Visitor.builder().onSubscribeToShardEvent(e -> System.out.println("Received subscribe to shard event " + e)).build();
SubscribeToShardResponseHandler responseHandler = SubscribeToShardResponseHandler.builder().onError(t -> System.err.println("Error during stream - " + t.getMessage())).subscriber(visitor).build();
return client.subscribeToShard(request, responseHandler);
}
use of org.reactivestreams.Subscriber in project quasar by puniverse.
the class TestHelper method createDummyFailedPublisher.
public static <T> Publisher<T> createDummyFailedPublisher() {
return new Publisher<T>() {
@Override
public void subscribe(Subscriber<? super T> s) {
s.onSubscribe(new Subscription() {
@Override
public void request(long n) {
}
@Override
public void cancel() {
}
});
s.onError(new RuntimeException("Can't subscribe subscriber: " + s + ", because of reasons."));
}
};
}
Aggregations