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.subscription;
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.model.ResourceDeliveryJsonMessage;
028import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedJsonMessage;
029import ca.uhn.fhir.subscription.SubscriptionConstants;
030import org.apache.commons.lang3.Validate;
031
032public class SubscriptionChannelFactory {
033        private final IChannelFactory myChannelFactory;
034
035        /**
036         * Constructor
037         */
038        public SubscriptionChannelFactory(IChannelFactory theChannelFactory) {
039                Validate.notNull(theChannelFactory);
040                myChannelFactory = theChannelFactory;
041        }
042
043        public IChannelProducer newDeliverySendingChannel(
044                        String theChannelName, ChannelProducerSettings theChannelSettings) {
045                ChannelProducerSettings config = newProducerConfigForDeliveryChannel(theChannelSettings);
046                config.setRetryConfiguration(theChannelSettings.getRetryConfigurationParameters());
047                return myChannelFactory.getOrCreateProducer(theChannelName, ResourceDeliveryJsonMessage.class, config);
048        }
049
050        public IChannelReceiver newDeliveryReceivingChannel(
051                        String theChannelName, ChannelConsumerSettings theChannelSettings) {
052                ChannelConsumerSettings config = newConsumerConfigForDeliveryChannel(theChannelSettings);
053                IChannelReceiver channel =
054                                myChannelFactory.getOrCreateReceiver(theChannelName, ResourceDeliveryJsonMessage.class, config);
055                return new BroadcastingSubscribableChannelWrapper(channel);
056        }
057
058        public IChannelProducer newMatchingSendingChannel(
059                        String theChannelName, ChannelProducerSettings theChannelSettings) {
060                ChannelProducerSettings config = newProducerConfigForMatchingChannel(theChannelSettings);
061                return myChannelFactory.getOrCreateProducer(theChannelName, ResourceModifiedJsonMessage.class, config);
062        }
063
064        public IChannelReceiver newMatchingReceivingChannel(
065                        String theChannelName, ChannelConsumerSettings theChannelSettings) {
066                ChannelConsumerSettings config = newConsumerConfigForMatchingChannel(theChannelSettings);
067                IChannelReceiver channel =
068                                myChannelFactory.getOrCreateReceiver(theChannelName, ResourceModifiedJsonMessage.class, config);
069                return new BroadcastingSubscribableChannelWrapper(channel);
070        }
071
072        protected ChannelProducerSettings newProducerConfigForDeliveryChannel(ChannelProducerSettings theOptions) {
073                ChannelProducerSettings config = new ChannelProducerSettings();
074                config.setConcurrentConsumers(getDeliveryChannelConcurrentConsumers());
075                config.setRetryConfiguration(theOptions.getRetryConfigurationParameters());
076                return config;
077        }
078
079        protected ChannelConsumerSettings newConsumerConfigForDeliveryChannel(ChannelConsumerSettings theOptions) {
080                ChannelConsumerSettings config = new ChannelConsumerSettings();
081                config.setConcurrentConsumers(getDeliveryChannelConcurrentConsumers());
082                if (theOptions != null) {
083                        config.setRetryConfiguration(theOptions.getRetryConfigurationParameters());
084                }
085                return config;
086        }
087
088        protected ChannelProducerSettings newProducerConfigForMatchingChannel(ChannelProducerSettings theOptions) {
089                ChannelProducerSettings config = new ChannelProducerSettings();
090                if (theOptions != null) {
091                        config.setRetryConfiguration(theOptions.getRetryConfigurationParameters());
092                        config.setQualifyChannelName(theOptions.isQualifyChannelName());
093                }
094                config.setConcurrentConsumers(getMatchingChannelConcurrentConsumers());
095                return config;
096        }
097
098        protected ChannelConsumerSettings newConsumerConfigForMatchingChannel(ChannelConsumerSettings theOptions) {
099                ChannelConsumerSettings config = new ChannelConsumerSettings();
100                config.setConcurrentConsumers(getMatchingChannelConcurrentConsumers());
101                if (theOptions != null) {
102                        config.setQualifyChannelName(theOptions.isQualifyChannelName());
103                        config.setRetryConfiguration(theOptions.getRetryConfigurationParameters());
104                }
105                return config;
106        }
107
108        public int getDeliveryChannelConcurrentConsumers() {
109                return SubscriptionConstants.DELIVERY_CHANNEL_CONCURRENT_CONSUMERS;
110        }
111
112        public int getMatchingChannelConcurrentConsumers() {
113                return SubscriptionConstants.MATCHING_CHANNEL_CONCURRENT_CONSUMERS;
114        }
115
116        public IChannelFactory getChannelFactory() {
117                return myChannelFactory;
118        }
119}