001/*-
002 * #%L
003 * HAPI FHIR Storage api
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.util;
021
022import ca.uhn.fhir.jpa.search.reindex.BlockPolicy;
023import jakarta.annotation.Nonnull;
024import org.apache.commons.lang3.Validate;
025import org.springframework.core.task.TaskDecorator;
026import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
027
028public final class ThreadPoolUtil {
029        private ThreadPoolUtil() {}
030
031        @Nonnull
032        public static ThreadPoolTaskExecutor newThreadPool(
033                        int theCorePoolSize, int theMaxPoolSize, String theThreadNamePrefix) {
034                return newThreadPool(theCorePoolSize, theMaxPoolSize, theThreadNamePrefix, 0);
035        }
036
037        @Nonnull
038        public static ThreadPoolTaskExecutor newThreadPool(
039                        int theCorePoolSize, int theMaxPoolSize, String theThreadNamePrefix, int theQueueCapacity) {
040                return newThreadPool(theCorePoolSize, theMaxPoolSize, theThreadNamePrefix, theQueueCapacity, null);
041        }
042
043        @Nonnull
044        public static ThreadPoolTaskExecutor newThreadPool(
045                        int theCorePoolSize,
046                        int theMaxPoolSize,
047                        String theThreadNamePrefix,
048                        int theQueueCapacity,
049                        TaskDecorator taskDecorator) {
050                Validate.isTrue(
051                                theCorePoolSize == theMaxPoolSize || theQueueCapacity == 0,
052                                "If the queue capacity is greater than 0, core pool size needs to match max pool size or the system won't grow the queue");
053                Validate.isTrue(theThreadNamePrefix.endsWith("-"), "Thread pool prefix name must end with a hyphen");
054                ThreadPoolTaskExecutor asyncTaskExecutor = new ThreadPoolTaskExecutor();
055                asyncTaskExecutor.setCorePoolSize(theCorePoolSize);
056                asyncTaskExecutor.setMaxPoolSize(theMaxPoolSize);
057                asyncTaskExecutor.setQueueCapacity(theQueueCapacity);
058                asyncTaskExecutor.setAllowCoreThreadTimeOut(true);
059                asyncTaskExecutor.setThreadNamePrefix(theThreadNamePrefix);
060                asyncTaskExecutor.setRejectedExecutionHandler(new BlockPolicy());
061                asyncTaskExecutor.setTaskDecorator(taskDecorator);
062                asyncTaskExecutor.initialize();
063                return asyncTaskExecutor;
064        }
065}