001/*- 002 * #%L 003 * HAPI FHIR Storage api 004 * %% 005 * Copyright (C) 2014 - 2025 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.broker.impl; 021 022import ca.uhn.fhir.broker.api.BrokerListenerClosedException; 023import ca.uhn.fhir.broker.api.IMessageListener; 024import ca.uhn.fhir.broker.api.IRetryAwareMessageListener; 025import ca.uhn.fhir.context.ConfigurationException; 026import ca.uhn.fhir.i18n.Msg; 027import ca.uhn.fhir.rest.server.messaging.IMessage; 028import ca.uhn.fhir.rest.server.messaging.IMessageDeliveryContext; 029import ca.uhn.fhir.util.IoUtils; 030import com.google.common.annotations.VisibleForTesting; 031import jakarta.annotation.Nonnull; 032import jakarta.annotation.Nullable; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036import java.util.LinkedList; 037import java.util.List; 038 039/** 040 * This message listener delegates message handling to an ordered list of sub-listeners. It supports message handling 041 * within a retry-aware context. 042 * 043 * @param <T> the type of payload this message listener is expecting to receive 044 */ 045public class MultiplexingListener<T> implements IRetryAwareMessageListener<T>, AutoCloseable { 046 private static final Logger ourLog = LoggerFactory.getLogger(MultiplexingListener.class); 047 private final List<IMessageListener<T>> mySubListeners = new LinkedList<>(); 048 049 private final Class<T> myPayloadType; 050 private boolean myClosed; 051 052 public MultiplexingListener(Class<T> thePayloadType) { 053 myPayloadType = thePayloadType; 054 } 055 056 @Override 057 public void handleMessage( 058 @Nullable IMessageDeliveryContext theMessageDeliveryContext, @Nonnull IMessage<T> theMessage) { 059 checkState(); 060 061 Class<?> messageClass = theMessage.getPayload().getClass(); 062 if (!getPayloadType().isAssignableFrom(messageClass)) { 063 // Wrong message types should never happen. If it does, we should quietly fail so it doesn't 064 // clog up the channel. 065 ourLog.warn( 066 "Received unexpected payload type. Expecting payload of type {}, but received payload of type {}. Skipping message.", 067 getPayloadType(), 068 messageClass); 069 return; 070 } 071 mySubListeners.forEach( 072 listener -> IRetryAwareMessageListener.handleMessage(listener, theMessageDeliveryContext, theMessage)); 073 } 074 075 @Override 076 public Class<T> getPayloadType() { 077 return myPayloadType; 078 } 079 080 public boolean addListener(IMessageListener<T> theListener) { 081 checkState(); 082 083 if (!getPayloadType().isAssignableFrom(theListener.getPayloadType())) { 084 throw new ConfigurationException(Msg.code(2658) + "Expecting listener of type " + getPayloadType() 085 + ". But listener was for type: " + theListener.getPayloadType()); 086 } 087 return mySubListeners.add(theListener); 088 } 089 090 public boolean removeListener(IMessageListener<T> theListener) { 091 return mySubListeners.remove(theListener); 092 } 093 094 @Override 095 public void close() { 096 mySubListeners.forEach(this::closeQuietly); 097 mySubListeners.clear(); 098 myClosed = true; 099 } 100 101 private void closeQuietly(IMessageListener<T> theMessageListener) { 102 if (theMessageListener instanceof AutoCloseable) { 103 IoUtils.closeQuietly((AutoCloseable) theMessageListener, ourLog); 104 } 105 } 106 107 private void checkState() { 108 if (myClosed) { 109 throw new BrokerListenerClosedException( 110 Msg.code(2659) + "Attempted to use a closed " + MultiplexingListener.class.getSimpleName()); 111 } 112 } 113 114 @VisibleForTesting 115 public <L extends IMessageListener<T>> L getListenerOfTypeOrNull(Class<L> theMessageListenerClass) { 116 for (IMessageListener<T> next : mySubListeners) { 117 if (theMessageListenerClass.isAssignableFrom(next.getClass())) { 118 return (L) next; 119 } 120 } 121 return null; 122 } 123}