001/*-
002 * #%L
003 * HAPI FHIR JPA Server - Master Data Management
004 * %%
005 * Copyright (C) 2014 - 2023 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.mdm.broker;
021
022import ca.uhn.fhir.jpa.subscription.channel.api.ChannelConsumerSettings;
023import ca.uhn.fhir.jpa.subscription.channel.api.IChannelFactory;
024import ca.uhn.fhir.jpa.subscription.channel.api.IChannelReceiver;
025import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedJsonMessage;
026import ca.uhn.fhir.mdm.api.IMdmSettings;
027import ca.uhn.fhir.mdm.log.Logs;
028import com.google.common.annotations.VisibleForTesting;
029import org.slf4j.Logger;
030import org.springframework.stereotype.Service;
031
032import javax.annotation.PreDestroy;
033
034@Service
035public class MdmQueueConsumerLoader {
036        private static final Logger ourLog = Logs.getMdmTroubleshootingLog();
037
038        private final IChannelFactory myChannelFactory;
039        private final IMdmSettings myMdmSettings;
040        private final MdmMessageHandler myMdmMessageHandler;
041
042        protected IChannelReceiver myMdmChannel;
043
044        public MdmQueueConsumerLoader(
045                        IChannelFactory theChannelFactory, IMdmSettings theMdmSettings, MdmMessageHandler theMdmMessageHandler) {
046                myChannelFactory = theChannelFactory;
047                myMdmSettings = theMdmSettings;
048                myMdmMessageHandler = theMdmMessageHandler;
049
050                startListeningToMdmChannel();
051        }
052
053        private void startListeningToMdmChannel() {
054                if (myMdmChannel == null) {
055                        ChannelConsumerSettings config = new ChannelConsumerSettings();
056
057                        config.setConcurrentConsumers(myMdmSettings.getConcurrentConsumers());
058
059                        myMdmChannel = myChannelFactory.getOrCreateReceiver(
060                                        IMdmSettings.EMPI_CHANNEL_NAME, ResourceModifiedJsonMessage.class, config);
061                        if (myMdmChannel == null) {
062                                ourLog.error("Unable to create receiver for {}", IMdmSettings.EMPI_CHANNEL_NAME);
063                        } else {
064                                myMdmChannel.subscribe(myMdmMessageHandler);
065                                ourLog.info(
066                                                "MDM Matching Consumer subscribed to Matching Channel {} with name {}",
067                                                myMdmChannel.getClass().getName(),
068                                                myMdmChannel.getName());
069                        }
070                }
071        }
072
073        @SuppressWarnings("unused")
074        @PreDestroy
075        public void stop() throws Exception {
076                if (myMdmChannel != null) {
077                        // JMS channel needs to be destroyed to avoid dangling receivers
078                        myMdmChannel.destroy();
079                        ourLog.info(
080                                        "MDM Matching Consumer unsubscribed from Matching Channel {} with name {}",
081                                        myMdmChannel.getClass().getName(),
082                                        myMdmChannel.getName());
083                }
084        }
085
086        @VisibleForTesting
087        public IChannelReceiver getMdmChannelForUnitTest() {
088                return myMdmChannel;
089        }
090}