static ExecutorService getConfiguredExecutorService(
            CamelContext camelContext, String name, DynamicRouterConfiguration cfg, boolean useDefault)
            throws IllegalArgumentException {
        ExecutorServiceManager manager = camelContext.getExecutorServiceManager();
        ObjectHelper.notNull(manager, ESM_NAME, camelContext);
        String exSvcRef = cfg.getExecutorService();
        ExecutorService exSvcBean = cfg.getExecutorServiceBean();
        String errorMessage = "ExecutorServiceRef '" + exSvcRef + "' not found in registry as an ExecutorService " +
                              "instance or as a thread pool profile";
        // The first (preferred) option is to use an explicitly-configured executor if the configuration has it
        return Optional.ofNullable(exSvcBean)
                // The second preference is to check for an executor service reference
                .or(() -> Optional.ofNullable(exSvcRef)
                        // Try to get the referenced executor service
                        .map(r -> lookupExecutorServiceRef(camelContext, name, cfg, r)
                                // But, if the reference is specified in the config,
                                // and could not be obtained, this is an error
                                .orElseThrow(() -> new IllegalArgumentException(errorMessage))))
                // The third and final option is to create a new "default" thread pool if the parameter
                // specifies to that the default thread pool should be used as a fallback
                .or(() -> useDefault ? Optional.of(manager.newDefaultThreadPool(cfg, name)) : Optional.empty())
                // failing the above options, then no executor service is configured
                .orElse(null);
    }
@Test
    void testGetConfiguredExecutorServiceWithExecutorServiceBean() {
        when(mockConfig.getExecutorServiceBean()).thenReturn(existingThreadPool);
        when(camelContext.getExecutorServiceManager()).thenReturn(manager);
        ExecutorService result
                = DynamicRouterRecipientListHelper.getConfiguredExecutorService(camelContext, "someName", mockConfig, true);
        assertEquals(existingThreadPool, result);
    }