/** * Runs the given runnable with the given ClassLoader as the thread's * {@link Thread#setContextClassLoader(ClassLoader) context class loader}. * * <p>The method will make sure to set the context class loader of the calling thread * back to what it was before after the runnable completed. */ public static <E extends Throwable> void withContextClassLoader( final ClassLoader cl, final ThrowingRunnable<E> r) throws E { final Thread currentThread = Thread.currentThread(); final ClassLoader oldClassLoader = currentThread.getContextClassLoader(); try { currentThread.setContextClassLoader(cl); r.run(); } finally { currentThread.setContextClassLoader(oldClassLoader); } }
private List<Runnable> createDownloadRunnables( Map<StateHandleID, StreamStateHandle> stateHandleMap, Path restoreInstancePath, CloseableRegistry closeableRegistry) { List<Runnable> runnables = new ArrayList<>(stateHandleMap.size()); for (Map.Entry<StateHandleID, StreamStateHandle> entry : stateHandleMap.entrySet()) { StateHandleID stateHandleID = entry.getKey(); StreamStateHandle remoteFileHandle = entry.getValue(); Path path = new Path(restoreInstancePath, stateHandleID.toString()); runnables.add(ThrowingRunnable.unchecked( () -> downloadDataForStateHandle(path, remoteFileHandle, closeableRegistry))); } return runnables; }
/** * Converts a {@link ThrowingRunnable} into a {@link Runnable} which throws all checked exceptions * as unchecked. * * @param throwingRunnable to convert into a {@link Runnable} * @return {@link Runnable} which throws all checked exceptions as unchecked. */ static Runnable unchecked(ThrowingRunnable<?> throwingRunnable) { return () -> { try { throwingRunnable.run(); } catch (Throwable t) { ExceptionUtils.rethrow(t); } }; } }
private void testNormalConsumerLifecycle(FlinkKafkaConsumerBase<String> testKafkaConsumer) throws Exception { setupConsumer(testKafkaConsumer); final CompletableFuture<Void> runFuture = CompletableFuture.runAsync(ThrowingRunnable.unchecked(() -> testKafkaConsumer.run(new TestSourceContext<>()))); testKafkaConsumer.close(); runFuture.get(); }
/** * Runs the given runnable with the given ClassLoader as the thread's * {@link Thread#setContextClassLoader(ClassLoader) context class loader}. * * <p>The method will make sure to set the context class loader of the calling thread * back to what it was before after the runnable completed. */ public static <E extends Throwable> void withContextClassLoader( final ClassLoader cl, final ThrowingRunnable<E> r) throws E { final Thread currentThread = Thread.currentThread(); final ClassLoader oldClassLoader = currentThread.getContextClassLoader(); try { currentThread.setContextClassLoader(cl); r.run(); } finally { currentThread.setContextClassLoader(oldClassLoader); } }
/** * Runs the given runnable with the given ClassLoader as the thread's * {@link Thread#setContextClassLoader(ClassLoader) context class loader}. * * <p>The method will make sure to set the context class loader of the calling thread * back to what it was before after the runnable completed. */ public static <E extends Throwable> void withContextClassLoader( final ClassLoader cl, final ThrowingRunnable<E> r) throws E { final Thread currentThread = Thread.currentThread(); final ClassLoader oldClassLoader = currentThread.getContextClassLoader(); try { currentThread.setContextClassLoader(cl); r.run(); } finally { currentThread.setContextClassLoader(oldClassLoader); } }
/** * Converts a {@link ThrowingRunnable} into a {@link Runnable} which throws all checked exceptions * as unchecked. * * @param throwingRunnable to convert into a {@link Runnable} * @return {@link Runnable} which throws all checked exceptions as unchecked. */ static Runnable unchecked(ThrowingRunnable<?> throwingRunnable) { return () -> { try { throwingRunnable.run(); } catch (Throwable t) { ExceptionUtils.rethrow(t); } }; } }
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> TtlValue<V> getWrappedWithTtlCheckAndUpdate( SupplierWithException<TtlValue<V>, SE> getter, ThrowingConsumer<TtlValue<V>, CE> updater, ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE { TtlValue<V> ttlValue = getter.get(); if (ttlValue == null) { return null; } else if (expired(ttlValue)) { stateClear.run(); if (!returnExpired) { return null; } } else if (updateTsOnRead) { updater.accept(rewrapWithNewTs(ttlValue)); } return ttlValue; } }
<SE extends Throwable, CE extends Throwable, CLE extends Throwable, V> TtlValue<V> getWrappedWithTtlCheckAndUpdate( SupplierWithException<TtlValue<V>, SE> getter, ThrowingConsumer<TtlValue<V>, CE> updater, ThrowingRunnable<CLE> stateClear) throws SE, CE, CLE { TtlValue<V> ttlValue = getter.get(); if (ttlValue == null) { return null; } else if (expired(ttlValue)) { stateClear.run(); if (!returnExpired) { return null; } } else if (updateTsOnRead) { updater.accept(rewrapWithNewTs(ttlValue)); } return ttlValue; } }