001package ca.uhn.fhir.jpa.search.reindex;
002
003/*-
004 * #%L
005 * HAPI FHIR Storage api
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import java.util.concurrent.RejectedExecutionHandler;
027import java.util.concurrent.ThreadPoolExecutor;
028
029/**
030 * A handler for rejected tasks that will have the caller block until space is available.
031 * 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.
032 */
033// TODO KHS consolidate with the other BlockPolicy class this looks like it is a duplicate of
034public class BlockPolicy implements RejectedExecutionHandler {
035        private static final Logger ourLog = LoggerFactory.getLogger(BlockPolicy.class);
036
037        /**
038         * Puts the Runnable to the blocking queue, effectively blocking the delegating thread until space is available.
039         *
040         * @param r the runnable task requested to be executed
041         * @param e the executor attempting to execute this task
042         */
043        @Override
044        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
045                try {
046                        e.getQueue().put(r);
047                } catch (InterruptedException e1) {
048                        ourLog.error("Interrupted Execption for task: {}", r, e1);
049                        Thread.currentThread().interrupt();
050                }
051        }
052}