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.subscription.channel.impl;
021
022import ca.uhn.fhir.jpa.subscription.channel.api.ChannelConsumerSettings;
023import ca.uhn.fhir.jpa.subscription.channel.api.ChannelProducerSettings;
024import ca.uhn.fhir.jpa.subscription.channel.api.IChannelFactory;
025import ca.uhn.fhir.jpa.subscription.channel.api.IChannelProducer;
026import ca.uhn.fhir.jpa.subscription.channel.api.IChannelReceiver;
027import ca.uhn.fhir.jpa.subscription.channel.api.IChannelSettings;
028import ca.uhn.fhir.jpa.subscription.channel.subscription.IChannelNamer;
029import ca.uhn.fhir.subscription.SubscriptionConstants;
030import ca.uhn.fhir.util.ThreadPoolUtil;
031import jakarta.annotation.Nonnull;
032import jakarta.annotation.PreDestroy;
033import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
034
035import java.util.Collections;
036import java.util.HashMap;
037import java.util.Map;
038
039public class LinkedBlockingChannelFactory implements IChannelFactory {
040
041        private final IChannelNamer myChannelNamer;
042        private final Map<String, LinkedBlockingChannel> myChannels = Collections.synchronizedMap(new HashMap<>());
043
044        public LinkedBlockingChannelFactory(IChannelNamer theChannelNamer) {
045                myChannelNamer = theChannelNamer;
046        }
047
048        @Override
049        public IChannelReceiver getOrCreateReceiver(
050                        String theChannelName, Class<?> theMessageType, ChannelConsumerSettings theChannelSettings) {
051                return getOrCreateChannel(theChannelName, theChannelSettings.getConcurrentConsumers(), theChannelSettings);
052        }
053
054        @Override
055        public IChannelProducer getOrCreateProducer(
056                        String theChannelName, Class<?> theMessageType, ChannelProducerSettings theChannelSettings) {
057                return getOrCreateChannel(theChannelName, theChannelSettings.getConcurrentConsumers(), theChannelSettings);
058        }
059
060        @Override
061        public IChannelNamer getChannelNamer() {
062                return myChannelNamer;
063        }
064
065        private LinkedBlockingChannel getOrCreateChannel(
066                        String theChannelName, int theConcurrentConsumers, IChannelSettings theChannelSettings) {
067                // TODO - does this need retry settings?
068                final String channelName = myChannelNamer.getChannelName(theChannelName, theChannelSettings);
069
070                return myChannels.computeIfAbsent(
071                                channelName, t -> buildLinkedBlockingChannel(theConcurrentConsumers, channelName));
072        }
073
074        @Nonnull
075        private LinkedBlockingChannel buildLinkedBlockingChannel(int theConcurrentConsumers, String theChannelName) {
076                String threadNamePrefix = theChannelName + "-";
077                ThreadPoolTaskExecutor threadPoolExecutor = ThreadPoolUtil.newThreadPool(
078                                theConcurrentConsumers,
079                                theConcurrentConsumers,
080                                threadNamePrefix,
081                                SubscriptionConstants.DELIVERY_EXECUTOR_QUEUE_SIZE);
082
083                return new LinkedBlockingChannel(theChannelName, threadPoolExecutor, threadPoolExecutor::getQueueSize);
084        }
085
086        @PreDestroy
087        public void stop() {
088                myChannels.clear();
089        }
090}