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.jpa.search.reindex;
021
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import java.util.concurrent.RejectedExecutionHandler;
026import java.util.concurrent.ThreadPoolExecutor;
027
028/**
029 * A handler for rejected tasks that will have the caller block until space is available.
030 * This was stolen from old hibernate search(5.X.X), as it has been removed in HS6. We can probably come up with a better solution though.
031 */
032// TODO KHS consolidate with the other BlockPolicy class this looks like it is a duplicate of
033public class BlockPolicy implements RejectedExecutionHandler {
034        private static final Logger ourLog = LoggerFactory.getLogger(BlockPolicy.class);
035
036        /**
037         * Puts the Runnable to the blocking queue, effectively blocking the delegating thread until space is available.
038         *
039         * @param r the runnable task requested to be executed
040         * @param e the executor attempting to execute this task
041         */
042        @Override
043        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
044                try {
045                        e.getQueue().put(r);
046                } catch (InterruptedException e1) {
047                        ourLog.error("Interrupted Execption for task: {}", r, e1);
048                        Thread.currentThread().interrupt();
049                }
050        }
051}