001package ca.uhn.fhir.jpa.mdm.broker; 002 003import ca.uhn.fhir.jpa.subscription.channel.api.ChannelConsumerSettings; 004import ca.uhn.fhir.jpa.subscription.channel.api.IChannelFactory; 005import ca.uhn.fhir.jpa.subscription.channel.api.IChannelReceiver; 006import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedJsonMessage; 007import ca.uhn.fhir.mdm.api.IMdmSettings; 008import ca.uhn.fhir.mdm.log.Logs; 009import com.google.common.annotations.VisibleForTesting; 010import org.slf4j.Logger; 011import org.springframework.beans.factory.annotation.Autowired; 012import org.springframework.stereotype.Service; 013 014import javax.annotation.PostConstruct; 015import javax.annotation.PreDestroy; 016 017/*- 018 * #%L 019 * HAPI FHIR JPA Server - Master Data Management 020 * %% 021 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 022 * %% 023 * Licensed under the Apache License, Version 2.0 (the "License"); 024 * you may not use this file except in compliance with the License. 025 * You may obtain a copy of the License at 026 * 027 * http://www.apache.org/licenses/LICENSE-2.0 028 * 029 * Unless required by applicable law or agreed to in writing, software 030 * distributed under the License is distributed on an "AS IS" BASIS, 031 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 032 * See the License for the specific language governing permissions and 033 * limitations under the License. 034 * #L% 035 */ 036 037@Service 038public class MdmQueueConsumerLoader { 039 private static final Logger ourLog = Logs.getMdmTroubleshootingLog(); 040 041 @Autowired 042 private MdmMessageHandler myMdmMessageHandler; 043 @Autowired 044 private IChannelFactory myChannelFactory; 045 @Autowired 046 private IMdmSettings myMdmSettings; 047 048 protected IChannelReceiver myMdmChannel; 049 050 @PostConstruct 051 public void startListeningToMdmChannel() { 052 if (myMdmChannel == null) { 053 ChannelConsumerSettings config = new ChannelConsumerSettings(); 054 055 config.setConcurrentConsumers(myMdmSettings.getConcurrentConsumers()); 056 057 myMdmChannel = myChannelFactory.getOrCreateReceiver(IMdmSettings.EMPI_CHANNEL_NAME, ResourceModifiedJsonMessage.class, config); 058 if (myMdmChannel == null) { 059 ourLog.error("Unable to create receiver for {}", IMdmSettings.EMPI_CHANNEL_NAME); 060 } else { 061 myMdmChannel.subscribe(myMdmMessageHandler); 062 ourLog.info("MDM Matching Consumer subscribed to Matching Channel {} with name {}", myMdmChannel.getClass().getName(), myMdmChannel.getName()); 063 } 064 } 065 } 066 067 @SuppressWarnings("unused") 068 @PreDestroy 069 public void stop() throws Exception { 070 if (myMdmChannel != null) { 071 // JMS channel needs to be destroyed to avoid dangling receivers 072 myMdmChannel.destroy(); 073 ourLog.info("MDM Matching Consumer unsubscribed from Matching Channel {} with name {}", myMdmChannel.getClass().getName(), myMdmChannel.getName()); 074 } 075 } 076 077 @VisibleForTesting 078 public IChannelReceiver getMdmChannelForUnitTest() { 079 return myMdmChannel; 080 } 081}