Java Code Examples for java.util.concurrent.SubmissionPublisher#close()
The following examples show how to use
java.util.concurrent.SubmissionPublisher#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Lesson4.java From Java-Concurrency-Multithreading-in-Practice with MIT License | 7 votes |
public static void main(String[] args) { SubmissionPublisher<WeatherForecast> weatherForecastPublisher = new WeatherForecastPublisher(); weatherForecastPublisher.subscribe(new DatabaseSubscriber()); weatherForecastPublisher.subscribe(new TwitterSubscriber<WeatherForecast>()); Flow.Processor<WeatherForecast, MetricWeatherForecast> metricConverter = new UsToMetricProcessor(); weatherForecastPublisher.subscribe(metricConverter); metricConverter.subscribe(new TwitterSubscriber<MetricWeatherForecast>()); // close the publisher and associated resources after 10 seconds try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } weatherForecastPublisher.close(); }
Example 2
Source File: TestStockMaintain.java From Java-programming-methodology-Rxjava-articles with Apache License 2.0 | 6 votes |
@Test public void teststockRemoval() throws InterruptedException { Stock stock = new Stock(); SubmissionPublisher<Order> p = new SubmissionPublisher<>(); p.subscribe(new StockMaintain(stock)); Product product = new Product(); stock.store(product, 40); OrderItem item = new OrderItem(); item.setProduct(product); item.setAmount(10); Order order = new Order(); List<OrderItem> items = new LinkedList<>(); items.add(item); order.setItems(items); for (int i = 0; i < 10; i++) p.submit(order); log.info("所有订单已经提交完毕"); for (int j = 0; j < 10; j++) { log.info("Sleeping a bit..."); Thread.sleep(50); } p.close(); log.info("Publisher已关闭"); }
Example 3
Source File: TestStockMaintain.java From Java-9-Spring-Webflux with Apache License 2.0 | 6 votes |
@Test public void teststockRemoval() throws InterruptedException { Stock stock = new Stock(); SubmissionPublisher<Order> p = new SubmissionPublisher<>(); p.subscribe(new StockMaintain(stock)); Product product = new Product(); stock.store(product, 40); OrderItem item = new OrderItem(); item.setProduct(product); item.setAmount(10); Order order = new Order(); List<OrderItem> items = new LinkedList<>(); items.add(item); order.setItems(items); for (int i = 0; i < 10; i++) p.submit(order); log.info("所有订单已经提交完毕"); for (int j = 0; j < 10; j++) { log.info("Sleeping a bit..."); Thread.sleep(50); } p.close(); log.info("Publisher已关闭"); }
Example 4
Source File: ReactiveStreamsUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenPublisher_whenSubscribeToIt_thenShouldConsumeAllElements() throws InterruptedException { //given SubmissionPublisher<String> publisher = new SubmissionPublisher<>(); EndSubscriber<String> subscriber = new EndSubscriber<>(6); publisher.subscribe(subscriber); List<String> items = List.of("1", "x", "2", "x", "3", "x"); //when assertThat(publisher.getNumberOfSubscribers()).isEqualTo(1); items.forEach(publisher::submit); publisher.close(); //then await().atMost(1000, TimeUnit.MILLISECONDS).untilAsserted( () -> assertThat(subscriber.consumedElements).containsExactlyElementsOf(items) ); }
Example 5
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * A publisher closedExceptionally reports isClosed with the * closedException and throws ISE upon attempted submission; a * subsequent close or closeExceptionally has no additional * effect. */ public void testCloseExceptionally() { SubmissionPublisher<Integer> p = basicPublisher(); checkInitialState(p); Throwable ex = new SPException(); p.closeExceptionally(ex); assertTrue(p.isClosed()); assertSame(p.getClosedException(), ex); try { p.submit(1); shouldThrow(); } catch (IllegalStateException success) {} p.close(); assertTrue(p.isClosed()); assertSame(p.getClosedException(), ex); }
Example 6
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * submit returns number of lagged items, compatible with result * of estimateMaximumLag. */ public void testLaggedSubmit() { SubmissionPublisher<Integer> p = basicPublisher(); TestSubscriber s1 = new TestSubscriber(); s1.request = false; TestSubscriber s2 = new TestSubscriber(); s2.request = false; p.subscribe(s1); p.subscribe(s2); s2.awaitSubscribe(); s1.awaitSubscribe(); assertEquals(1, p.submit(1)); assertTrue(p.estimateMaximumLag() >= 1); assertTrue(p.submit(2) >= 2); assertTrue(p.estimateMaximumLag() >= 2); s1.sn.request(4); assertTrue(p.submit(3) >= 3); assertTrue(p.estimateMaximumLag() >= 3); s2.sn.request(4); p.submit(4); p.close(); s2.awaitComplete(); assertEquals(4, s2.nexts); s1.awaitComplete(); assertEquals(4, s2.nexts); }
Example 7
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Closing a publisher causes onComplete to subscribers */ public void testCloseCompletes() { SubmissionPublisher<Integer> p = basicPublisher(); TestSubscriber s1 = new TestSubscriber(); TestSubscriber s2 = new TestSubscriber(); p.subscribe(s1); p.subscribe(s2); p.submit(1); p.close(); assertTrue(p.isClosed()); assertNull(p.getClosedException()); s1.awaitComplete(); assertEquals(1, s1.nexts); assertEquals(1, s1.completes); s2.awaitComplete(); assertEquals(1, s2.nexts); assertEquals(1, s2.completes); }
Example 8
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * offer succeeds if drop handler forces request */ public void testRecoveredHandledDroppedOffer() { AtomicInteger calls = new AtomicInteger(); SubmissionPublisher<Integer> p = new SubmissionPublisher<>(basicExecutor, 4); TestSubscriber s1 = new TestSubscriber(); s1.request = false; TestSubscriber s2 = new TestSubscriber(); s2.request = false; p.subscribe(s1); p.subscribe(s2); s2.awaitSubscribe(); s1.awaitSubscribe(); int n = 0; for (int i = 1; i <= 8; ++i) { int d = p.offer(i, (s, x) -> reqHandle(calls, s)); n = n + 2 + (d < 0 ? d : 0); } p.close(); s2.awaitComplete(); s1.awaitComplete(); assertEquals(n, s1.nexts + s2.nexts); assertTrue(calls.get() >= 2); }
Example 9
Source File: MyReactiveApp.java From journaldev with MIT License | 6 votes |
public static void main(String args[]) throws InterruptedException { // Create Publisher SubmissionPublisher<Employee> publisher = new SubmissionPublisher<>(); // Register Subscriber MySubscriber subs = new MySubscriber(); publisher.subscribe(subs); List<Employee> emps = EmpHelper.getEmps(); // Publish items System.out.println("Publishing Items to Subscriber"); emps.stream().forEach(i -> publisher.submit(i)); // logic to wait till processing of all messages are over //while (emps.size() != subs.getCounter() || !publisher.isSubscribed(subs)) { Thread.sleep(1000); //} // close the Publisher publisher.close(); System.out.println("Exiting the app"); }
Example 10
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * If a handler is supplied in constructor, it is invoked when * subscriber throws an exception in onNext */ public void testThrowOnNextHandler() { AtomicInteger calls = new AtomicInteger(); SubmissionPublisher<Integer> p = new SubmissionPublisher<>( basicExecutor, 8, (s, e) -> calls.getAndIncrement()); TestSubscriber s1 = new TestSubscriber(); TestSubscriber s2 = new TestSubscriber(); p.subscribe(s1); p.subscribe(s2); s1.awaitSubscribe(); p.submit(1); s1.throwOnCall = true; p.submit(2); p.close(); s2.awaitComplete(); assertEquals(2, s2.nexts); assertEquals(1, s2.completes); s1.awaitError(); assertEquals(1, s1.errors); assertEquals(1, calls.get()); }
Example 11
Source File: ReactiveStreamsUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenPublisher_whenRequestForOnlyOneElement_thenShouldConsumeOnlyThatOne() throws InterruptedException { //given SubmissionPublisher<String> publisher = new SubmissionPublisher<>(); EndSubscriber<String> subscriber = new EndSubscriber<>(1); publisher.subscribe(subscriber); List<String> items = List.of("1", "x", "2", "x", "3", "x"); List<String> expected = List.of("1"); //when assertThat(publisher.getNumberOfSubscribers()).isEqualTo(1); items.forEach(publisher::submit); publisher.close(); //then await().atMost(1000, TimeUnit.MILLISECONDS).untilAsserted( () -> assertThat(subscriber.consumedElements).containsExactlyElementsOf(expected) ); }
Example 12
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * offer invokes drop handler if saturated */ public void testHandledDroppedOffer() { AtomicInteger calls = new AtomicInteger(); SubmissionPublisher<Integer> p = new SubmissionPublisher<>(basicExecutor, 4); TestSubscriber s1 = new TestSubscriber(); s1.request = false; TestSubscriber s2 = new TestSubscriber(); s2.request = false; p.subscribe(s1); p.subscribe(s2); s2.awaitSubscribe(); s1.awaitSubscribe(); for (int i = 1; i <= 4; ++i) assertTrue(p.offer(i, (s, x) -> noopHandle(calls)) >= 0); p.offer(4, (s, x) -> noopHandle(calls)); assertTrue(p.offer(6, (s, x) -> noopHandle(calls)) < 0); s1.sn.request(64); assertTrue(p.offer(7, (s, x) -> noopHandle(calls)) < 0); s2.sn.request(64); p.close(); s2.awaitComplete(); s1.awaitComplete(); assertTrue(calls.get() >= 4); }
Example 13
Source File: MyReactiveAppWithProcessor.java From journaldev with MIT License | 5 votes |
public static void main(String[] args) throws InterruptedException { // Create End Publisher SubmissionPublisher<Employee> publisher = new SubmissionPublisher<>(); // Create Processor MyProcessor transformProcessor = new MyProcessor(s -> { return new Freelancer(s.getId(), s.getId() + 100, s.getName()); }); //Create End Subscriber MyFreelancerSubscriber subs = new MyFreelancerSubscriber(); //Create chain of publisher, processor and subscriber publisher.subscribe(transformProcessor); // publisher to processor transformProcessor.subscribe(subs); // processor to subscriber List<Employee> emps = EmpHelper.getEmps(); // Publish items System.out.println("Publishing Items to Subscriber"); emps.stream().forEach(i -> publisher.submit(i)); // Logic to wait for messages processing to finish while (emps.size() != subs.getCounter()) { Thread.sleep(10); } // Closing publishers publisher.close(); transformProcessor.close(); System.out.println("Exiting the app"); }
Example 14
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * consume returns a CompletableFuture that is done when * publisher completes */ public void testConsume() { AtomicInteger sum = new AtomicInteger(); SubmissionPublisher<Integer> p = basicPublisher(); CompletableFuture<Void> f = p.consume((Integer x) -> sum.getAndAdd(x.intValue())); int n = 20; for (int i = 1; i <= n; ++i) p.submit(i); p.close(); f.join(); assertEquals((n * (n + 1)) / 2, sum.get()); }
Example 15
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Non-positive request causes error */ public void testRequest3() { SubmissionPublisher<Integer> p = basicPublisher(); TestSubscriber s1 = new TestSubscriber(); TestSubscriber s2 = new TestSubscriber(); TestSubscriber s3 = new TestSubscriber(); p.subscribe(s1); p.subscribe(s2); p.subscribe(s3); s3.awaitSubscribe(); s2.awaitSubscribe(); s1.awaitSubscribe(); s1.sn.request(-1L); s3.sn.request(0L); p.submit(1); p.submit(2); p.close(); s2.awaitComplete(); assertEquals(2, s2.nexts); assertEquals(1, s2.completes); s1.awaitError(); assertEquals(1, s1.errors); assertTrue(s1.lastError instanceof IllegalArgumentException); s3.awaitError(); assertEquals(1, s3.errors); assertTrue(s3.lastError instanceof IllegalArgumentException); }
Example 16
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Timed offer invokes drop handler if saturated */ public void testHandledDroppedTimedOffer() { AtomicInteger calls = new AtomicInteger(); SubmissionPublisher<Integer> p = new SubmissionPublisher<>(basicExecutor, 4); TestSubscriber s1 = new TestSubscriber(); s1.request = false; TestSubscriber s2 = new TestSubscriber(); s2.request = false; p.subscribe(s1); p.subscribe(s2); s2.awaitSubscribe(); s1.awaitSubscribe(); long delay = timeoutMillis(); for (int i = 1; i <= 4; ++i) assertTrue(p.offer(i, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) >= 0); long startTime = System.nanoTime(); assertTrue(p.offer(5, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0); s1.sn.request(64); assertTrue(p.offer(6, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0); assertTrue(millisElapsedSince(startTime) >= delay); s2.sn.request(64); p.close(); s2.awaitComplete(); s1.awaitComplete(); assertTrue(calls.get() >= 2); }
Example 17
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * If closed, upon subscription, the subscriber's onComplete * method is invoked */ public void testSubscribe2() { TestSubscriber s = new TestSubscriber(); SubmissionPublisher<Integer> p = basicPublisher(); p.close(); p.subscribe(s); s.awaitComplete(); assertEquals(0, s.nexts); assertEquals(0, s.errors); assertEquals(1, s.completes, 1); }
Example 18
Source File: SubmissionPublisherTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Upon subscription, the subscriber's onSubscribe is called, no * other Subscriber methods are invoked, the publisher * hasSubscribers, isSubscribed is true, and existing * subscriptions are unaffected. */ public void testSubscribe1() { TestSubscriber s = new TestSubscriber(); SubmissionPublisher<Integer> p = basicPublisher(); p.subscribe(s); assertTrue(p.hasSubscribers()); assertEquals(1, p.getNumberOfSubscribers()); assertTrue(p.getSubscribers().contains(s)); assertTrue(p.isSubscribed(s)); s.awaitSubscribe(); assertNotNull(s.sn); assertEquals(0, s.nexts); assertEquals(0, s.errors); assertEquals(0, s.completes); TestSubscriber s2 = new TestSubscriber(); p.subscribe(s2); assertTrue(p.hasSubscribers()); assertEquals(2, p.getNumberOfSubscribers()); assertTrue(p.getSubscribers().contains(s)); assertTrue(p.getSubscribers().contains(s2)); assertTrue(p.isSubscribed(s)); assertTrue(p.isSubscribed(s2)); s2.awaitSubscribe(); assertNotNull(s2.sn); assertEquals(0, s2.nexts); assertEquals(0, s2.errors); assertEquals(0, s2.completes); p.close(); }
Example 19
Source File: ProgMainReactiveStreamsWithProcessor.java From javase with MIT License | 4 votes |
public static void main(String[] args) throws InterruptedException { /* // Sample without transformation // Create Publisher SubmissionPublisher<Employee> publisher = new SubmissionPublisher<>(); // Register Subscriber MySubscriber subs = new MySubscriber(); publisher.subscribe(subs); List<Employee> emps = EmpHelper.getEmps(); // Publish items System.out.println("Publishing Items to Subscriber"); emps.stream().forEach(i -> publisher.submit(i)); // logic to wait till processing of all messages are over while (emps.size() != subs.getCounter()) { Thread.sleep(10); } // close the Publisher publisher.close(); System.out.println("Exiting the app"); */ // Sample with transformation // Create End Publisher SubmissionPublisher<Employee> publisher = new SubmissionPublisher<>(); // Create Processor MyProcessor transformProcessor = new MyProcessor(s -> { return new Freelancer(s.getId(), s.getId() + 100, s.getName()); }); //Create End Subscriber MyFreelancerSubscriber subs = new MyFreelancerSubscriber(); //Create chain of publisher, processor and subscriber publisher.subscribe(transformProcessor); // publisher to processor transformProcessor.subscribe(subs); // processor to subscriber List<Employee> emps = EmpHelper.getEmps(); // Publish items System.out.println("Publishing Items to Subscriber"); emps.stream().forEach(i -> publisher.submit(i)); // Logic to wait for messages processing to finish while (emps.size() != subs.getCounter()) { Thread.sleep(10); } // Closing publishers publisher.close(); transformProcessor.close(); System.out.println("Exiting the app"); }
Example 20
Source File: Main.java From jdk9-jigsaw with Creative Commons Zero v1.0 Universal | 3 votes |
public static void main(String[] args) throws InterruptedException { LOGGER.info("Checking prime numbers ..."); SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>(); // TODO: create subscriber Integer[] numbers = {4, 17, 19, 101, 105, 107, 201}; Arrays.asList(numbers).stream().forEach(i -> publisher.submit(i)); // add some wait time ... Thread.sleep(3000); publisher.close(); }