protected InstanceEventPublisher() { UnicastProcessor<InstanceEvent> unicastProcessor = UnicastProcessor.create(); this.publishedFlux = unicastProcessor.publish().autoConnect(0); this.sink = unicastProcessor.sink(); }
private SubmissionPublisher(Scheduler scheduler, int maxBufferCapacity) { if (scheduler == null){ throw new NullPointerException(); } if (maxBufferCapacity <= 0){ throw new IllegalArgumentException("capacity must be positive"); } UnicastProcessor<T> processor = UnicastProcessor.<T>create(); sink = processor.sink(); flux = processor .publish(maxBufferCapacity) .autoConnect() .subscribeOn(Schedulers.immediate()) .publishOn(scheduler); numberOfSubscribers = new AtomicInteger(0); }
@Test public void contextTest() { UnicastProcessor<Integer> p = UnicastProcessor.create(); p.subscriberContext(ctx -> ctx.put("foo", "bar")).subscribe(); assertThat(p.sink().currentContext().get("foo").toString()).isEqualTo("bar"); }
@Test public void multiThreadedProducer() { UnicastProcessor<Integer> processor = UnicastProcessor.create(); FluxSink<Integer> sink = processor.sink(); int nThreads = 5; int countPerThread = 10000; ExecutorService executor = Executors.newFixedThreadPool(nThreads); for (int i = 0; i < 5; i++) { Runnable generator = () -> { for (int j = 0; j < countPerThread; j++) { sink.next(j); } }; executor.submit(generator); } StepVerifier.create(processor) .expectNextCount(nThreads * countPerThread) .thenCancel() .verify(); executor.shutdownNow(); }
private SubmissionPublisher(Scheduler scheduler, int maxBufferCapacity) { if (scheduler == null){ throw new NullPointerException(); } if (maxBufferCapacity <= 0){ throw new IllegalArgumentException("capacity must be positive"); } UnicastProcessor<T> processor = UnicastProcessor.<T>create(); sink = processor.sink(); flux = processor .publish(maxBufferCapacity) .autoConnect() .subscribeOn(Schedulers.immediate()) .publishOn(scheduler); numberOfSubscribers = new AtomicInteger(0); }