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