本文整理汇总了Java中org.reactivestreams.Publisher.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Java Publisher.subscribe方法的具体用法?Java Publisher.subscribe怎么用?Java Publisher.subscribe使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.reactivestreams.Publisher
的用法示例。
在下文中一共展示了Publisher.subscribe方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: reducePub
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
private static <T,R> Publisher<R> reducePub(Publisher<T> pub, R init, BiFunction<R, T, R> bf) {
return new Publisher<R>() {
@Override
public void subscribe(Subscriber<? super R> sub) {
pub.subscribe(new DelegateSub<T, R>(sub) {
R result = init;
@Override
public void onNext(T t) {
result = bf.apply(result, t);
}
@Override
public void onComplete() {
sub.onNext(result);
sub.onComplete();
}
});
}
};
}
示例2: sumPub
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
private static Publisher<Integer> sumPub(Publisher<Integer> pub) {
return new Publisher<Integer>() {
@Override
public void subscribe(Subscriber sub) {
pub.subscribe(new DelegateSub<Integer, Integer>(sub) {
int sum = 0;
@Override
public void onNext(Integer t) {
sum += t;
}
@Override
public void onComplete() {
sub.onNext(sum);
sub.onComplete();
}
});
}
};
}
示例3: subscribeActual
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
@Override
protected void subscribeActual(Subscriber<? super R> s) {
K key;
Publisher<? extends R> source;
try {
key = caseSelector.call();
source = mapOfCases.get(key);
} catch (Throwable ex) {
EmptySubscription.error(ex, s);
return;
}
if (source == null) {
source = defaultCase;
}
source.subscribe(s);
}
示例4: onHeaders
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
@Override
public void onHeaders(Response response) {
if (logger.isDebugEnabled()) {
logger.debug("received response headers {}", response);
}
Publisher<T> publisher = contentFn.apply(request.getReactiveResponse(), content);
publisher.subscribe(this);
}
示例5: executeAsync
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
public RFuture<BatchResult<?>> executeAsync(int syncSlaves, long syncTimeout, boolean skipResult, long responseTimeout, int retryAttempts, long retryInterval) {
for (Publisher<?> publisher : publishers) {
publisher.subscribe(new DefaultSubscriber<Object>() {
@Override
public void onSubscribe(Subscription s) {
s.request(1);
}
});
}
return batchService.executeAsync(syncSlaves, syncTimeout, skipResult, responseTimeout, retryAttempts, retryInterval);
}
示例6: record
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
public void record(Publisher<?> publisher, String filter) {
ChronicleQueue queue = reactiveJournal.createQueue();
ExcerptAppender appender = queue.acquireAppender();
TriConsumer<ExcerptAppender, String, Object> onNextConsumer = getOnNextConsumerRecorder();
BiConsumer<ExcerptAppender, String> onCompleteConsumer = getOnCompleteRecorder();
TriConsumer<ExcerptAppender, String, Throwable> onErrorConsumer = getOnErrorRecorder();
publisher.subscribe(new Subscriber<Object>() {
@Override
public void onSubscribe(Subscription subscription) {
subscription.request(Long.MAX_VALUE);
}
@Override
public void onNext(Object o) {
onNextConsumer.accept(appender, filter, o);
}
@Override
public void onError(Throwable throwable) {
onErrorConsumer.accept(appender, filter, throwable);
}
@Override
public void onComplete() {
onCompleteConsumer.accept(appender, filter);
}
});
}
示例7: main
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
public static void main(String[] args) {
//Operators
// Publisher -> Data1 -> Operator1 -> Data2 -> Operator2 -> Data3 -> Subscriber
// map (d1 -> d2)
Publisher<Integer> pub = iterPub(Stream.iterate(1, a -> a + 1).limit(10).collect(Collectors.toList()));
// Publisher<String> mapPub = mapPub(pub, s -> "[" + s + "]");
/// Publisher<Integer> sumPub = sumPub(pub);
Publisher<StringBuilder> reducePub = reducePub(pub, new StringBuilder(), (a, b)->a.append(b+","));
reducePub.subscribe(logSub());
abc:{
System.out.println("1");
if(2==1)
break abc;
System.out.println("2");
}
if (2 ==1){
}else for (int i=0 ; i<10 ; i++){
System.out.println(i);
}
}
示例8: mapPub
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
private static <T, R> Publisher<R> mapPub(Publisher<T> pub, Function<T, R> f) {
return new Publisher<R>() {
@Override
public void subscribe(Subscriber<? super R> sub) {
pub.subscribe(new DelegateSub<T, R>(sub) {
@Override
public void onNext(T t) {
sub.onNext(f.apply(t));
}
});
}
};
}
示例9: assertResult
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
public static <T> void assertResult(Publisher<T> source, T... array) {
TestSubscriber<T> ts = new TestSubscriber<T>();
source.subscribe(ts);
ts.assertValues(array)
.assertNoErrors()
.assertComplete();
}
示例10: onStream
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
@Override
public void onStream(Publisher<ByteBuffer> publisher) {
publisher.subscribe(new SimpleSubscriber(byteBuffer -> {
byte[] b = new byte[byteBuffer.remaining()];
byteBuffer.duplicate().get(b);
bodyParts.append(new String(b, StandardCharsets.UTF_8));
}));
}
示例11: onStream
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
@Override
public void onStream(Publisher<ByteBuffer> publisher) {
baos = new ByteArrayOutputStream();
publisher.subscribe(new SimpleSubscriber(b -> {
try {
baos.write(BinaryUtils.copyBytesFrom(b));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}));
}
示例12: PublisherContent
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
public PublisherContent(Publisher<ContentChunk> publisher, String contentType) {
this.contentType = Objects.requireNonNull(contentType);
publisher.subscribe(this);
}
示例13: main
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
//1. Create the reactiveRecorder and delete any previous content by clearing the cache
ReactiveJournal reactiveJournal = new ReactiveJournal(tmpDir + File.separator + "HW");
reactiveJournal.clearCache();
//2. Create a HelloWorld Publisher
Publisher<String> helloWorldFlowable = subscriber -> {
subscriber.onNext("Hello World!");
subscriber.onComplete();
};
//3. Pass the Publisher into the reactiveRecorder which will subscribe to it and record all events.
ReactiveRecorder reactiveRecorder = reactiveJournal.createReactiveRecorder();
reactiveRecorder.record(helloWorldFlowable, "");
//4. Subscribe to ReactiveJournal and print out results
Publisher recordedObservable = reactiveJournal.createReactivePlayer().play(new PlayOptions());
recordedObservable.subscribe(new Subscriber() {
@Override
public void onSubscribe(Subscription subscription) {
subscription.request(Long.MAX_VALUE);
}
@Override
public void onNext(Object o) {
System.out.println(o);
}
@Override
public void onError(Throwable throwable) {
System.err.println(throwable);
}
@Override
public void onComplete() {
System.out.println("Hello World Complete");
}
});
//5. Sometimes useful to see the recording written to a file
reactiveJournal.writeToFile(tmpDir + File.separator + "/hw.txt",true);
}
示例14: onWebSocketText
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
@Override
public void onWebSocketText(String graphqlQuery) {
log.info("Websocket said {}", graphqlQuery);
QueryParameters parameters = QueryParameters.from(graphqlQuery);
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query(parameters.getQuery())
.variables(parameters.getVariables())
.operationName(parameters.getOperationName())
.build();
Instrumentation instrumentation = new ChainedInstrumentation(
singletonList(new TracingInstrumentation())
);
//
// In order to have subscriptions in graphql-java you MUST use the
// SubscriptionExecutionStrategy strategy.
//
GraphQL graphQL = GraphQL
.newGraphQL(graphqlPublisher.getGraphQLSchema())
.instrumentation(instrumentation)
.build();
ExecutionResult executionResult = graphQL.execute(executionInput);
Publisher<ExecutionResult> stockPriceStream = executionResult.getData();
stockPriceStream.subscribe(new Subscriber<ExecutionResult>() {
@Override
public void onSubscribe(Subscription s) {
subscriptionRef.set(s);
request(1);
}
@Override
public void onNext(ExecutionResult er) {
log.debug("Sending stick price update");
try {
Object stockPriceUpdate = er.getData();
getRemote().sendString(JsonKit.toJsonString(stockPriceUpdate));
} catch (IOException e) {
e.printStackTrace();
}
request(1);
}
@Override
public void onError(Throwable t) {
log.error("Subscription threw an exception", t);
getSession().close();
}
@Override
public void onComplete() {
log.info("Subscription complete");
getSession().close();
}
});
}
示例15: subscribe
import org.reactivestreams.Publisher; //导入方法依赖的package包/类
void subscribe(Publisher<? extends T> source1, Publisher<? extends T> source2) {
source1.subscribe(first);
source2.subscribe(second);
}