From aa9ad7f5a5e2405e8082a542916c3d1fa7d0fa25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Wed, 7 Feb 2024 14:26:15 +0100 Subject: [PATCH] fix: ignore UnsupportedOperationException for virtual threads (#2866) The virtual threads util that tries to create a virtual thread factory on JVMs that support this would fail on Java 20, because: 1. Java 20 supports virtual threads as an experimental feature. This means that the code is present. 2. The feature is by default disabled, and throws an UnsupportedOperationException. This fix takes the above into account and returns null if a user tries to create a virtual threads factory on Java 20 with experimental features disabled. --- .../com/google/cloud/spanner/ThreadFactoryUtil.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ThreadFactoryUtil.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ThreadFactoryUtil.java index 67f4d3230d4..72d58e85be3 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ThreadFactoryUtil.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ThreadFactoryUtil.java @@ -69,6 +69,11 @@ public static ThreadFactory tryCreateVirtualThreadFactory(String baseNameFormat) } catch (ClassNotFoundException | NoSuchMethodException ignore) { return null; } catch (InvocationTargetException | IllegalAccessException e) { + // Java 20 supports virtual threads as an experimental feature. It will throw an + // UnsupportedOperationException if experimental features have not been enabled. + if (e.getCause() instanceof UnsupportedOperationException) { + return null; + } throw new RuntimeException(e); } } @@ -91,6 +96,11 @@ public static ExecutorService tryCreateVirtualThreadPerTaskExecutor(String baseN } catch (NoSuchMethodException ignore) { return null; } catch (InvocationTargetException | IllegalAccessException e) { + // Java 20 supports virtual threads as an experimental feature. It will throw an + // UnsupportedOperationException if experimental features have not been enabled. + if (e.getCause() instanceof UnsupportedOperationException) { + return null; + } throw new RuntimeException(e); } }