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.jms;
021
022import ca.uhn.fhir.broker.api.IMessageListener;
023import ca.uhn.fhir.rest.server.messaging.IMessage;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.springframework.messaging.Message;
027import org.springframework.messaging.MessageHandler;
028import org.springframework.messaging.MessagingException;
029
030/**
031 * Adapt a {@link IMessageListener} to Spring Messaging (JMS)
032 *
033 * @param <T> the type of payload this message handler is expecting to receive
034 */
035public class SpringMessagingMessageHandlerAdapter<T> implements MessageHandler {
036        private static final Logger ourLog = LoggerFactory.getLogger(SpringMessagingMessageHandlerAdapter.class);
037        private final IMessageListener<T> myMessageListener;
038        private final Class<? extends IMessage<T>> myMessageType;
039
040        public SpringMessagingMessageHandlerAdapter(
041                        Class<? extends IMessage<T>> theMessageType, IMessageListener<T> theMessageListener) {
042                myMessageListener = theMessageListener;
043                myMessageType = theMessageType;
044        }
045
046        @Override
047        public void handleMessage(Message<?> theMessage) throws MessagingException {
048                if (!IMessage.class.isAssignableFrom(theMessage.getClass())) {
049                        // Wrong message types should never happen.  If it does, we should quietly fail so it doesn't
050                        // clog up the channel.
051                        ourLog.warn(
052                                        "Received unexpected message type. Expecting message of type {}, but received message of type {}. Skipping message.",
053                                        IMessage.class,
054                                        theMessage.getClass());
055                        return;
056                }
057
058                if (!getMessageType().isAssignableFrom(theMessage.getClass())) {
059                        // Wrong message types should never happen.  If it does, we should quietly fail so it doesn't
060                        // clog up the channel.
061                        ourLog.warn(
062                                        "Received unexpected message type. Expecting message of type {}, but received message of type {}. Skipping message.",
063                                        getMessageType(),
064                                        theMessage.getClass());
065                        return;
066                }
067
068                IMessage<T> message = (IMessage<T>) theMessage;
069                myMessageListener.handleMessage(message);
070        }
071
072        private Class<? extends IMessage<T>> getMessageType() {
073                return myMessageType;
074        }
075}