當前位置: 首頁>>代碼示例>>Java>>正文


Java Publisher.subscribe方法代碼示例

本文整理匯總了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();
                }
            });
        }
    };
}
 
開發者ID:sejoung,項目名稱:ReactiveTest,代碼行數:22,代碼來源:PubSub2.java

示例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();
                }
            });
        }
    };
}
 
開發者ID:sejoung,項目名稱:ReactiveTest,代碼行數:23,代碼來源:PubSub2.java

示例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);
}
 
開發者ID:fengzhizi715,項目名稱:RxConditions,代碼行數:20,代碼來源:FlowableSwitchCase.java

示例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);
}
 
開發者ID:jetty-project,項目名稱:jetty-reactive-httpclient,代碼行數:9,代碼來源:ResponseListenerPublisher.java

示例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);
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:13,代碼來源:CommandReactiveBatchService.java

示例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);
        }
    });
}
 
開發者ID:danielshaya,項目名稱:reactivejournal,代碼行數:31,代碼來源:ReactiveRecorder.java

示例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);
    }

}
 
開發者ID:sejoung,項目名稱:ReactiveTest,代碼行數:29,代碼來源:PubSub2.java

示例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));
                }
            });
        }
    };
}
 
開發者ID:sejoung,項目名稱:ReactiveTest,代碼行數:14,代碼來源:PubSub2.java

示例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();
}
 
開發者ID:akaita,項目名稱:RxJava2Debug,代碼行數:10,代碼來源:BaseTest.java

示例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));
    }));
}
 
開發者ID:aws,項目名稱:aws-sdk-java-v2,代碼行數:9,代碼來源:RecordingResponseHandler.java

示例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);
        }
    }));
}
 
開發者ID:aws,項目名稱:aws-sdk-java-v2,代碼行數:12,代碼來源:SyncResponseHandlerAdapter.java

示例12: PublisherContent

import org.reactivestreams.Publisher; //導入方法依賴的package包/類
public PublisherContent(Publisher<ContentChunk> publisher, String contentType) {
    this.contentType = Objects.requireNonNull(contentType);
    publisher.subscribe(this);
}
 
開發者ID:jetty-project,項目名稱:jetty-reactive-httpclient,代碼行數:5,代碼來源:PublisherContent.java

示例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);
}
 
開發者ID:danielshaya,項目名稱:reactivejournal,代碼行數:44,代碼來源:HelloWorld.java

示例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();
        }
    });
}
 
開發者ID:graphql-java,項目名稱:graphql-java-subscription-example,代碼行數:63,代碼來源:StockTickerWebSocket.java

示例15: subscribe

import org.reactivestreams.Publisher; //導入方法依賴的package包/類
void subscribe(Publisher<? extends T> source1, Publisher<? extends T> source2) {
    source1.subscribe(first);
    source2.subscribe(second);
}
 
開發者ID:akarnokd,項目名稱:RxJava3-preview,代碼行數:5,代碼來源:FlowableSequenceEqualSingle.java


注:本文中的org.reactivestreams.Publisher.subscribe方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。