001package ca.uhn.fhir.jpa.subscription.match.matcher.subscriber;
002
003import ca.uhn.fhir.jpa.subscription.channel.api.IChannelReceiver;
004import ca.uhn.fhir.jpa.subscription.channel.subscription.SubscriptionChannelFactory;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007import org.springframework.beans.factory.annotation.Autowired;
008import org.springframework.context.event.ContextRefreshedEvent;
009import org.springframework.context.event.EventListener;
010
011import javax.annotation.PreDestroy;
012
013import static ca.uhn.fhir.jpa.subscription.match.matcher.subscriber.SubscriptionMatchingSubscriber.SUBSCRIPTION_MATCHING_CHANNEL_NAME;
014
015/*-
016 * #%L
017 * HAPI FHIR Subscription Server
018 * %%
019 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
020 * %%
021 * Licensed under the Apache License, Version 2.0 (the "License");
022 * you may not use this file except in compliance with the License.
023 * You may obtain a copy of the License at
024 *
025 *      http://www.apache.org/licenses/LICENSE-2.0
026 *
027 * Unless required by applicable law or agreed to in writing, software
028 * distributed under the License is distributed on an "AS IS" BASIS,
029 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
030 * See the License for the specific language governing permissions and
031 * limitations under the License.
032 * #L%
033 */
034
035public class MatchingQueueSubscriberLoader {
036        protected IChannelReceiver myMatchingChannel;
037        private Logger ourLog = LoggerFactory.getLogger(MatchingQueueSubscriberLoader.class);
038        @Autowired
039        private SubscriptionMatchingSubscriber mySubscriptionMatchingSubscriber;
040        @Autowired
041        private SubscriptionChannelFactory mySubscriptionChannelFactory;
042        @Autowired
043        private SubscriptionRegisteringSubscriber mySubscriptionRegisteringSubscriber;
044        @Autowired
045        private SubscriptionActivatingSubscriber mySubscriptionActivatingSubscriber;
046
047        @EventListener(classes = {ContextRefreshedEvent.class})
048        public void handleContextRefreshEvent() {
049                if (myMatchingChannel == null) {
050                        myMatchingChannel = mySubscriptionChannelFactory.newMatchingReceivingChannel(SUBSCRIPTION_MATCHING_CHANNEL_NAME, null);
051                }
052                if (myMatchingChannel != null) {
053                        myMatchingChannel.subscribe(mySubscriptionMatchingSubscriber);
054                        myMatchingChannel.subscribe(mySubscriptionActivatingSubscriber);
055                        myMatchingChannel.subscribe(mySubscriptionRegisteringSubscriber);
056                        ourLog.info("Subscription Matching Subscriber subscribed to Matching Channel {} with name {}", myMatchingChannel.getClass().getName(), SUBSCRIPTION_MATCHING_CHANNEL_NAME);
057                }
058        }
059
060        @SuppressWarnings("unused")
061        @PreDestroy
062        public void stop() throws Exception {
063                if (myMatchingChannel != null) {
064                        ourLog.info("Destroying matching Channel {} with name {}", myMatchingChannel.getClass().getName(), SUBSCRIPTION_MATCHING_CHANNEL_NAME);
065                        myMatchingChannel.destroy();
066                        myMatchingChannel.unsubscribe(mySubscriptionMatchingSubscriber);
067                        myMatchingChannel.unsubscribe(mySubscriptionActivatingSubscriber);
068                        myMatchingChannel.unsubscribe(mySubscriptionRegisteringSubscriber);
069                }
070        }
071
072}