001package ca.uhn.fhir.jpa.subscription.channel.subscription;
002
003/*-
004 * #%L
005 * HAPI FHIR Subscription Server
006 * %%
007 * Copyright (C) 2014 - 2021 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                return myChannelFactory.getOrCreateProducer(theChannelName, ResourceDeliveryJsonMessage.class, config);
047        }
048
049        public IChannelReceiver newDeliveryReceivingChannel(String theChannelName, ChannelConsumerSettings theChannelSettings) {
050                ChannelConsumerSettings config = newConsumerConfigForDeliveryChannel(theChannelSettings);
051                IChannelReceiver channel = myChannelFactory.getOrCreateReceiver(theChannelName, ResourceDeliveryJsonMessage.class, config);
052                return new BroadcastingSubscribableChannelWrapper(channel);
053        }
054
055        public IChannelProducer newMatchingSendingChannel(String theChannelName, ChannelProducerSettings theChannelSettings) {
056                ChannelProducerSettings config = newProducerConfigForMatchingChannel(theChannelSettings);
057                return myChannelFactory.getOrCreateProducer(theChannelName, ResourceModifiedJsonMessage.class, config);
058        }
059
060        public IChannelReceiver newMatchingReceivingChannel(String theChannelName, ChannelConsumerSettings theChannelSettings) {
061                ChannelConsumerSettings config = newConsumerConfigForMatchingChannel(theChannelSettings);
062                IChannelReceiver channel = myChannelFactory.getOrCreateReceiver(theChannelName, ResourceModifiedJsonMessage.class, config);
063                return new BroadcastingSubscribableChannelWrapper(channel);
064        }
065
066        protected ChannelProducerSettings newProducerConfigForDeliveryChannel(ChannelProducerSettings theOptions) {
067                ChannelProducerSettings config = new ChannelProducerSettings();
068                config.setConcurrentConsumers(getDeliveryChannelConcurrentConsumers());
069                return config;
070        }
071
072        protected ChannelConsumerSettings newConsumerConfigForDeliveryChannel(ChannelConsumerSettings theOptions) {
073                ChannelConsumerSettings config = new ChannelConsumerSettings();
074                config.setConcurrentConsumers(getDeliveryChannelConcurrentConsumers());
075                return config;
076        }
077
078        protected ChannelProducerSettings newProducerConfigForMatchingChannel(ChannelProducerSettings theOptions) {
079                ChannelProducerSettings config = new ChannelProducerSettings();
080                config.setConcurrentConsumers(getMatchingChannelConcurrentConsumers());
081                return config;
082        }
083
084        protected ChannelConsumerSettings newConsumerConfigForMatchingChannel(ChannelConsumerSettings theOptions) {
085                ChannelConsumerSettings config = new ChannelConsumerSettings();
086                config.setConcurrentConsumers(getMatchingChannelConcurrentConsumers());
087                return config;
088        }
089
090        public int getDeliveryChannelConcurrentConsumers() {
091                return SubscriptionConstants.DELIVERY_CHANNEL_CONCURRENT_CONSUMERS;
092        }
093
094        public int getMatchingChannelConcurrentConsumers() {
095                return SubscriptionConstants.MATCHING_CHANNEL_CONCURRENT_CONSUMERS;
096        }
097
098        public IChannelFactory getChannelFactory() {
099                return myChannelFactory;
100        }
101
102}