
001/*- 002 * #%L 003 * HAPI FHIR - Server Framework 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.rest.server.messaging; 021 022import ca.uhn.fhir.rest.server.messaging.json.BaseJsonMessage; 023import jakarta.annotation.Nonnull; 024 025import java.util.Map; 026import java.util.Optional; 027import java.util.UUID; 028import java.util.stream.Collectors; 029 030/** 031 * This interface is implemented by serializable "wrapper" message classes that are exchanged with Massage Brokers. HAPI-FHIR 032 * message classes implement both {@link org.springframework.messaging.Message} and {@link IMessage} so that they can 033 * be exchanged with both JMS and non-JMS brokers. These message wrappers wrap a serializable payload that is the main content 034 * of the message. This wrapper also contains meta-data about the message such as headers and a message key. The message key 035 * is used by non-JMS brokers for partition selection. 036 * 037 * @param <T> the type of the message payload. In most cases, T will be a subclass of {@link BaseJsonMessage} 038 */ 039public interface IMessage<T> { 040 /** 041 * The message key is used by brokers that support channel partitioning. The message key is used to determine which partition 042 * a message is stored on. If message order is important, then the same message key should be used for all messages that need 043 * to preserve their order. E.g. if a series of messages create, update, and delete a resource, the resource id would be a good 044 * candidate for the message key to ensure the order of operations is preserved on all messages concerning that resource. 045 * @return the key of the message. 046 */ 047 @Nonnull 048 default String getMessageKey() { 049 return UUID.randomUUID().toString(); 050 } 051 052 /** 053 * @return a map of message headers 054 */ 055 @Nonnull 056 Map<String, Object> getHeaders(); 057 058 /** 059 * @return a header value as an Optional 060 */ 061 default <H> Optional<H> getHeader(String theHeaderName) { 062 return (Optional<H>) Optional.ofNullable(getHeaders().get(theHeaderName)); 063 } 064 065 /** 066 * @return the de-serialized value of the message 067 */ 068 T getPayload(); 069 070 /** 071 * @return headers as a String to String map where all values are replaced with their toString() value 072 */ 073 default Map<String, String> getHeadersAsStrings() { 074 return getHeaders().entrySet().stream() 075 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().toString())); 076 } 077}