/** * Re-apply default factory to {@link Schedulers} */ public static void resetFactory(){ setFactory(DEFAULT); }
/** * Common method to enable a {@link VirtualTimeScheduler} in {@link Schedulers} * factories. The supplier is lazily called. Enabling the same scheduler twice is * also idempotent. * * @param schedulerSupplier the supplier executed to obtain a fresh {@link VirtualTimeScheduler} * @param exact whether or not to force the use of the supplier, even if there's a matching scheduler * @return the scheduler that is actually used after the operation. */ static VirtualTimeScheduler enable(Supplier<VirtualTimeScheduler> schedulerSupplier, boolean exact) { for (; ; ) { VirtualTimeScheduler s = CURRENT.get(); if (s != null && !exact) { return s; } VirtualTimeScheduler newS = schedulerSupplier.get(); if (newS == CURRENT.get()) { return newS; //nothing to do, it has already been set in the past } if (CURRENT.compareAndSet(s, newS)) { Schedulers.setFactory(new AllFactory(newS)); if (CURRENT.get() == newS) { return newS; } } } }
void setupHooks(BeanFactory beanFactory) { Hooks.onLastOperator( TraceReactorAutoConfiguration.TraceReactorConfiguration.SLEUTH_TRACE_REACTOR_KEY, ReactorSleuth.scopePassingSpanOperator(this.context)); Schedulers.setFactory(factoryInstance(beanFactory)); }
@Test @SuppressWarnings("deprecated") public void schedulerDecoratorEmptyDecoratorsButCustomFactory() { AtomicInteger factoryDecoratorCounter = new AtomicInteger(); Schedulers.setFactory(new Schedulers.Factory() { @Override public ScheduledExecutorService decorateExecutorService(String schedulerType, Supplier<? extends ScheduledExecutorService> actual) { factoryDecoratorCounter.incrementAndGet(); return actual.get(); } }); //trigger the decorators Schedulers.newSingle("foo").dispose(); assertThat(factoryDecoratorCounter).hasValue(1); }
@Test public void testOverride() throws InterruptedException { TestSchedulers ts = new TestSchedulers(true); Schedulers.setFactory(ts); Assert.assertEquals(ts.single, Schedulers.newSingle("unused")); Assert.assertEquals(ts.elastic, Schedulers.newElastic("unused")); Assert.assertEquals(ts.parallel, Schedulers.newParallel("unused")); Schedulers.resetFactory(); Scheduler s = Schedulers.newSingle("unused"); s.dispose(); Assert.assertNotSame(ts.single, s); }
@Test public void testShutdownOldOnSetFactory() { Schedulers.Factory ts1 = new Schedulers.Factory() { }; Schedulers.Factory ts2 = new TestSchedulers(false); Schedulers.setFactory(ts1); Scheduler cachedTimerOld = Schedulers.single(); Scheduler standaloneTimer = Schedulers.newSingle("standaloneTimer"); Assert.assertNotSame(cachedTimerOld, standaloneTimer); Assert.assertNotNull(cachedTimerOld.schedule(() -> {})); Assert.assertNotNull(standaloneTimer.schedule(() -> {})); Schedulers.setFactory(ts2); Scheduler cachedTimerNew = Schedulers.newSingle("unused"); Assert.assertEquals(cachedTimerNew, Schedulers.newSingle("unused")); Assert.assertNotSame(cachedTimerNew, cachedTimerOld); //assert that the old factory"s cached scheduler was shut down Assertions.assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> cachedTimerOld.schedule(() -> {})); //independently created schedulers are still the programmer"s responsibility Assert.assertNotNull(standaloneTimer.schedule(() -> {})); //new factory = new alive cached scheduler Assert.assertNotNull(cachedTimerNew.schedule(() -> {})); }
/** * Re-apply default factory to {@link Schedulers} */ public static void resetFactory(){ setFactory(DEFAULT); }
/** * Common method to enable a {@link VirtualTimeScheduler} in {@link Schedulers} * factories. The supplier is lazily called. Enabling the same scheduler twice is * also idempotent. * * @param schedulerSupplier the supplier executed to obtain a fresh {@link VirtualTimeScheduler} * @param exact whether or not to force the use of the supplier, even if there's a matching scheduler * @return the scheduler that is actually used after the operation. */ static VirtualTimeScheduler enable(Supplier<VirtualTimeScheduler> schedulerSupplier, boolean exact) { for (; ; ) { VirtualTimeScheduler s = CURRENT.get(); if (s != null && !exact) { return s; } VirtualTimeScheduler newS = schedulerSupplier.get(); if (newS == CURRENT.get()) { return newS; //nothing to do, it has already been set in the past } if (CURRENT.compareAndSet(s, newS)) { Schedulers.setFactory(new AllFactory(newS)); if (CURRENT.get() == newS) { return newS; } } } }
void setupHooks(BeanFactory beanFactory) { Hooks.onLastOperator( TraceReactorAutoConfiguration.TraceReactorConfiguration.SLEUTH_TRACE_REACTOR_KEY, ReactorSleuth.scopePassingSpanOperator(this.context)); Schedulers.setFactory(factoryInstance(beanFactory)); }
beanContext.getBeansOfType(BeanCreatedEventListener.class, Qualifiers.byTypeArguments(ScheduledExecutorService.class)); Schedulers.setFactory( new Schedulers.Factory() { @SuppressWarnings("unchecked")